Linked List
Another Linear Data Structure
What is a Linked List?
• A linked list is a linear data
structure.
• Nodes make up linked lists.
• Nodes are structures made
up of data and a pointer to
another node.
• Usually, the pointer is called
next.
Types of lists
• Singly Linked list
• Circular Linked list
• Doubly linked list
Singly Linked List
Each node has only one link part.
Each link part contains the address of the next node in
the list.
Link part of the last node contains NULL value which
signifies the end of the linked list.
Singly linked list
a b c d
Start/Head
• Each node contains a data and a pointer to the next node.
• Start/head is the header pointer which points at the first
node in the list
Creating a node
int node;
char node[2];
In C, a structure should be used to design a node of a
linked list.
struct node{
int data; / / A simple node of a linked list
struct node *next;
}*start; //start points at the first node
start=NULL ; initialised to NULL at beginning
Creating a node
struct node{
int data;
struct node *next;
}*start;
start=NULL ;
The next step is to allocate memory to a node.
start = (struct node *)malloc(sizeof(struct node));
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
• Insertion at the beginning
• Insertion at the end
• Insertion at other locations
Insertion at the beginning
Start 11 12 13 14
temp
10
temp = (struct node *)malloc(sizeof(struct node));
temp->data = 10;
temp->next = NULL;
temp->next = start;
temp Start 11 12 13 14
10
Insertion at the beginning
Start 11 12 13 14
temp
10
Update start pointer.
start = temp
11 12 13 14
Start
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;
Set a new pointer to point to the first node.
ptr = start;
start
11 12 13 14
temp
ptr 10
Insertion at the end
start 11 12 13 14
ptr temp
10
Move ptr to the last node.
while(ptr->next != NULL)
ptr = ptr->next;
start
11 12 13 14
temp
ptr 10
Insertion at the end
start 11 12 13 14
ptr temp
10
Update link field of last node
ptr->next = temp;
start
11 12 13 14
ptr 10
temp
Insertion at other locations
start 11 12 13 14
temp
10
temp = (struct node *)malloc(sizeof(struct node));
temp->data = 10;
temp->next = NULL;
Set a new pointer to point to the first node.
ptr = start;
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
• Deleting the last node
• Deleting any other node
Deleting the first node
start 11 12 13 14
Set another pointer to also point to the first node.
ptr = start;
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
Deallocate space for the first node.
free(ptr)
12 13 14
start
Deleting the last node
start 11 12 13 14
Set another pointer to also point to the first node.
ptr = start;
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
Set another pointer to point to the node to be deleted (last node)
p = ptr->next;
start 11 12 13 14
ptr p
Deleting the last node
Update links
ptr->next = p->next;
start 11 12 13 14
ptr p
Deallocate space for the last node
free(p);
start 11 12 13
ptr
Deleting any other node
start
11 12 13 14 15
Set another pointer to also point to the first node.
ptr = start;
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
Set a pointer to the node to be deleted: p = ptr->next
start
11 12 13 14 15
ptr p
Deleting any other node
Update links
ptr->next = p->next
start
11 12 13 14 15
ptr p
Deallocate space for the node to be deleted.
free(p)
start
11 12 13 15
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
Set another pointer to also point to the first node.
ptr = start;
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 is pointing to the beginning of the first list
start2 is pointing to the beginning of the second 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
If(ptr->data > ptr->next->data){ int temp = 98
temp = ptr->data;
ptr->data = ptr->next->data;
ptr->next->data = temp;
}
Bubble sort on a linked list
2nd iteration
start 78 44 14 98
start 44 78 14 98
start 44 14 78 98
Arrays Vs Linked Lists
Arrays Linked list
Fixed size: Resizing is expensive Dynamic size
Insertions and Deletions are Insertions and Deletions are efficient:
inefficient: No
Elements are usually shifted shifting
Random access i.e., efficient indexing No random access
→ Not suitable for operations
requiring accessing elements by
index such as sorting
No memory waste if the array is Since memory is allocated
full or almost full; otherwise may dynamically(acc. to our need)
result in much memory waste. there is no waste of memory.
Sequential access is faster [Reason: Sequential access is slow [Reason:
Elements in contiguous memory Elements not in contiguous
locations] memory locations]