Module 2- Linked List
Module 2- Linked List
a b c d
Start/Head
start
Creating a node
struct node * create(){
struct node *start;
start = (struct node *)malloc(sizeof(struct node));
if(start == NULL)
{
printf("Error in creating the node");
exit(0);
}
else{
printf("Enter a value:")
scanf("%d",&start->data); //data entered is 10
start->next = NULL;
}
return start;
}
Start 10
Basic Operations on a list
• Traversal
• Insertion
• Deletion
• Search
• Merge
Traversal
Start 11 12 13 14
ptr
traverse(){
struct node *ptr = start;
11->12->13->14->NULL
while(ptr != NULL){
printf(“%d->”,ptr->data);
ptr = ptr->next;
}
printf(“NULL”);
}
Insertion
temp Start 11 12 13 14
10
Insertion at the beginning
Start 11 12 13 14
temp
10
10
temp
Insertion at the end
start 11 12 13 14
temp
10
temp = (struct node *)malloc(sizeof(struct node));
temp->data = 10;
temp->next = NULL;
start
11 12 13 14
temp
ptr 10
Insertion at the end
start 11 12 13 14
ptr temp
10
start
11 12 13 14
temp
ptr 10
Insertion at the end
start 11 12 13 14
ptr temp
10
start
11 12 13 14
ptr 10
temp
Insertion at other locations
start 11 12 13 14
temp
10
start
11 12 13 14
ptr temp 10
Insertion at other locations
Move ptr such that it points to the node to the immediate left of the location of
insertion.
i = 1;
while(i < LOC-1){
ptr = ptr->next;
i++;
}
LOC = 4
start 11 12 13 14
temp
10
ptr
Insertion at other locations
Update links
temp->next = ptr->next
start 11 12 13 14
10
temp
ptr
ptr->next = temp
start 11 12 13 14
10
temp
ptr
struct node *insert_linkedlist(struct node *start){
struct node *temp, *ptr = start;
int loc, i = 1;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = 99;
temp->next = NULL;
loc = 4;
if(loc == 1){ //insertion at the beginning
temp->next = start;
start = temp;
}
else{ //insertion at the end and other locations
while(i < loc - 1){
ptr = ptr->next;
i++;
}
temp->next = ptr->next;
ptr->next = temp;
}
return start;
}
Deletion
• Deleting the first node
start 11 12 13 14
ptr
Deleting the first node
Move start to the next node.
start = start->next;
ptr 11 12 13 14
start
12 13 14
start
Deleting the last node
start 11 12 13 14
start 11 12 13 14
ptr
Deleting the last node
Move ptr to the node to the immediate left of the node to be deleted.
While(ptr != NULL){
if(ptr->next->data != ITEM)
ptr = ptr->next;
}
start 11 12 13 14
ptr
start 11 12 13 14
ptr p
Deleting the last node
Update links
ptr->next = p->next;
start 11 12 13 14
ptr p
start 11 12 13
ptr
Deleting any other node
start
11 12 13 14 15
start
11 12 13 14 15
ptr
Deleting any other node
Move ptr to the node which is immediately to the left of the node to be deleted.
While(ptr != NULL){
if(ptr->next->data != ITEM)
ptr = ptr->next;
}
start
11 12 13 14 15
ptr
ptr p
Deleting any other node
Update links
ptr->next = p->next
start
11 12 13 14 15
ptr p
ptr
struct node* delete_linkedlist(struct node *start){
struct node *ptr = start, *p;
int value, flag = 0;
printf("\nEnter the element to be deleted:");
scanf("%d",&value);
if(ptr->data == value){ //first node
start = start->next;
flag = 1;
free(ptr);
}
else{ // for the last and other nodes
while(ptr != NULL){
if(ptr->next->data == value){
p = ptr->next;
flag = 1;
break;
}
ptr = ptr->next;
}
ptr->next = p->next; //update links
free(p);
}
if(flag == 0)
printf("\nData not found\n");
return start;
}
Searching in the Linked List
Linear searching is the only search technique which is possible in
linked list.
Binary search is too complex for Linked list.
start 11 12 13 14
start 11 12 13 14
ptr
Searching in the linked list
Move ptr to the next node until the element is found or last node is reached
ptr = ptr --> next;
start 11 12 13 14
ptr
start 11 12 13 14
ptr
start 11 12 13 14
ptr
Searching in the linked list
struct node* Linear_Search_LinkedList (struct node *ptr, int KEY)
{
while (ptr != NULL)
{
if (KEY == ptr->data)
{
return ptr;
}
ptr = ptr->next;
}
return NULL;
}
Merging Linked Lists
Two or more lists can be merges by simply assigning the link of one list
to last of another list.
start1 11 12 13 14
ptr
Set another pointer to also point to the first node of first list.
ptr = start1;
start2 22 44 11 21
Merging Linked Lists
start1 11 12 13 14
ptr
Move ptr to the next node until last node is reached
ptr = ptr --> next;
After reaching the last node of first list assign link last node of first list to first
node of second list as
prt ->next = start2;
start2 22 44 11 21
Merging Linked Lists
start1 11 12 13 14
ptr
start2 22 44 11 21
Merging Linked List
struct node* LinkedList_Merge (struct node *start1,struct node *start2)
{
ptr=start1;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next=start2;
}
return start1;
}
Reversing linked list
start 11 12 13 14 X
current
start
11 12 13 14
before
struct node *before = NULL; after
struct node *after = NULL;
struct node *ptr = start;
while(ptr != NULL){
after = ptr->next;
ptr->next = before;
before = ptr;
ptr = after;
}
start = before;
Bubble sort on a linked list
1st iteration
start 78 98 44 14
start 78 98 44 14
ptr
start 78 44 98 14
start 78 44 14 98
start 78 44 14 98
start 44 78 14 98
start 44 14 78 98
Arrays Vs Linked Lists
Arrays Linked list