Applications of Artificial Intelligence
Applications of Artificial Intelligence
One way chain or singly linked list can be traversed only in one direction.
In other words, we can say that each node contains only next pointer,
therefore we can’t traverse the list in the reverse direction.
In the above figure, the arrow represents the links. The data part of every
node contains the marks obtained by the student in the different subject.
The last node in the list is identified by the null pointer which is present in
the address part of the last node. We can have as many elements we
require, in the data part of the list.
COMPLEXITY:
Singly θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
Linked
List
OPERATIONS ON SINGLY LINKED LIST:
There are various operations which can be performed on singly linked list.
A list of all such operations is given below.
NODE CREATION
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *) malloc(sizeof(struct node *));
INSERTION:
The insertion into a singly linked list can be performed at different
positions. Based on the position of the new node being inserted, the
insertion is categorized into the following categories.
S Operation Description
N
1 Insertion at It involves inserting any element at the front of the list. We just
beginning need to a few link adjustments to make the new node as the
head of the list.
2 Insertion at the It involves insertion at the last of the linked list. The new node
end of the list can be inserted as the only node in the list or it can be inserted
as the last one. Different logics are implemented in each
scenario.
3 Insertion after It involves insertion after the specified node of the linked list. We
specified node need to skip the desired number of nodes in order to reach the
node after which the new node will be inserted. .
SN Operation Description
1 Deletion at It involves deletion of a node from the beginning of the list. This is
beginning the simplest operation among all. It just need a few adjustments in
the node pointers.
2 Deletion at the It involves deleting the last node of the list. The list can either be
end of the list empty or full. Different logic is implemented for the different
scenarios.
3 Deletion after It involves deleting the node after the specified node in the list. we
specified node need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through
the list.
4 Traversing In traversing, we simply visit each node of the list at least once in
order to perform some specific operation on it, for example,
printing data part of each node present in the list.
5 Searching In searching, we match each element of the list with the given
element. If the element is found on any of the location then
location of that element is returned otherwise null is returned. .
// Node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
if (head == NULL) {
return newNode;
}
current->next = newNode;
return head;
}
free(current);
return head;
}
// Main function
int main() {
struct Node* head = NULL;
// Insert nodes at the beginning
head = insertAtBeginning(head, 3);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 1);
// Delete a node
head = deleteNode(head, 3);
// Free memory
freeList(head);
return 0;
}
OUTPUT:
Linked List: 1 -> 2 -> 3 -> NULL
Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Linked List: 1 -> 2 -> 4 -> 5 -> NULL