Lecture 3
Lecture 3
Syntax:
struct node
{
int data;
struct node *next;
};
Example
⚫ next pointer of the last node is NULL, so if the next current node is
⚫ In all of the examples, we will assume that the linked list has three
Code: Output:
struct node *temp = head; List elements are -
printf("\n\nList elements are - \n"); 1 --->2 --->3 --->
while(temp != NULL) {
printf("%d --->",temp->data);
temp = temp->next;
}
Insert at the Beginning
Algorithm:
1. Allocate memory for new node
2. Store data
3. Change next of new node to point to head
4. Change head to point to recently created node
Code:
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;
Insert at the Middle
Code:
struct node *newNode;
Algorithm: newNode = malloc(sizeof(struct node));
1. Allocate memory and store data for newNode->data = 4;
new node struct node *temp = head;
2. Traverse to node just before the for(int i=2; i < position; i++) {
required position of new node if(temp->next != NULL) {
3. Change next pointers to include temp = temp->next;
new node in between
}
}
newNode->next = temp->next;
temp->next = newNode;
Insert at the End
Code:
struct node *newNode;
Algorithm:
newNode = malloc(sizeof(struct node));
1. Allocate memory for new node
newNode->data = 4;
2. Store data
newNode->next = NULL;
3. Traverse to last node
struct node *temp = head;
4. Change next of last node to
while(temp->next != NULL){
recently created node
temp = temp->next;
}
temp->next = newNode;
Delete from Beginning
Algorithm:
1. Point head to the second node
Code:
head = head->next;
Delete from Middle
Code:
Algorithm: for(int i=2; i< position; i++) {
1. Traverse to element before the if(temp->next!=NULL) {
element to be deleted temp = temp->next;
2. Change next pointers to exclude the }
node from the chain }
temp->next = temp->next->next;
Delete from End
Code:
Algorithm: struct node* temp = head;
1. Traverse to second last element while(temp->next->next!=NULL){
2. Change its next pointer to null temp = temp->next;
}
temp->next = NULL;
Search
Algorithm: Code:
1. Make head as the current node. bool searchNode(struct Node** head_ref, int key) {
2. Run a loop until the current node is struct Node* current = *head_ref;
NULL because the last element points while (current != NULL) {
to NULL. if (current->data == key)
3. In each iteration, check if the key of return true;
the node is equal to item. If it the key
current = current->next;
matches the item, return true
}
otherwise return false.
return false;
}
Sort
Algorithm:
1. Make the head as the current node and create another node index for later use.
2. If head is null, return.
3. Else, run a loop till the last node (i.e., NULL).
4. In each iteration, follow the following step 5-6.
5. Store the next node of current in index.
6. Check if the data of the current node is greater than the next node. If it is greater, swap
current and index.
Sort
Code:
void sortLinkedList(struct Node** head_ref) { while (index != NULL) {
} }
Syntax:
struct node {
int data;
struct node *next;
struct node *prev;
};
Example