12 - Data Structure and Algorithms - Linked List
12 - Data Structure and Algorithms - Linked List
Linked List
Singly Linked List − The nodes only point to the address of the next
node in the list.
Doubly Linked List − The nodes point to the addresses of both previous
and next nodes.
Circular Linked List − The last node in the list will point to the first node
in the list. It can either be singly linked or doubly linked.
Linked list can be visualized as a chain of nodes, where every node points to
the next node.
As per the above illustration, following are the important points to be
considered.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Singly linked lists contain two “buckets” in one node; one bucket holds the
data and the other bucket holds the address of the next node of the list.
Traversals can be done in one direction only as there is only a single link
between two nodes of the same list.
Doubly Linked Lists
Doubly Linked Lists contain three “buckets” in one node; one bucket holds
the data and the other buckets hold the addresses of the previous and next
nodes in the list. The list is traversed twice as the nodes in the list are
connected to each other from both sides.
Circular linked lists can exist in both singly linked list and doubly linked list.
Since the last node and the first node of the circular linked list are connected,
the traversal in this linked list will go on forever until it is broken.
The basic operations in the linked lists are insertion, deletion, searching,
display, and deleting an element at a given key. These operations are
performed on Singly Linked Lists as given below −
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall
learn this with diagrams here. First, create a node using the same structure
and find the location where it has to be inserted.
Now, the next node at the left should point to the new node.
Insertion in linked list can be done in three different ways. They are explained
as follows −
Insertion at Beginning
Algorithm
1. START
4. If the list is empty, add the data to the node and assign the head pointer to
it.
5 If the list is not empty, add the data to a node and link to the current head.
Assign the head to the newly added node.
6. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
//create a link
lk->data = data;
lk->next = head;
head = lk;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(44);
insertatbegin(50);
// print list
printList();
Output
Linked List:
[ 50 44 30 22 12 ]
Insertion at Ending
Algorithm
1. START
5. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
//create a link
lk->data = data;
lk->next = head;
//point first to new first node
head = lk;
//create a link
lk->data = data;
while(linkedlist->next != NULL)
linkedlist = linkedlist->next;
linkedlist->next = lk;
void main(){
int k=0;
insertatbegin(12);
insertatend(22);
insertatend(30);
insertatend(44);
insertatend(50);
// print list
printList();
Output
Linked List:
[ 12 22 30 44 50 ]
In this operation, we are adding an element at any position within the list.
Algorithm
1. START
5. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
//create a link
lk->data = data;
lk->next = head;
//point first to new first node
head = lk;
lk->data = data;
lk->next = list->next;
list->next = lk;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertafternode(head->next, 30);
// print list
printList();
}
Output
Linked List:
[ 22 12 30 ]
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial
representation. First, locate the target node to be removed, by using searching
algorithms.
The left (previous) node of the target node now should point to the next node
of the target node −
This will remove the link that was pointing to the target node. Now, using the
following code, we will remove what the target node is pointing at.
Similar steps should be taken if the node is being inserted at the beginning of
the list. While inserting it at the end, the second last node of the list should
point to the new node and the new node will point to NULL.
Deletion in linked lists is also performed in three different ways. They are as
follows −
Deletion at Beginning
In this deletion operation of the linked, we are deleting an element from the
beginning of the list. For this, we point the head to the second node.
Algorithm
1. START
3. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
printf("\n[");
printf(" %d ",p->data);
p = p->next;
printf("]");
//create a link
lk->data = data;
lk->next = head;
head = lk;
}
void deleteatbegin(){
head = head->next;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(40);
insertatbegin(55);
// print list
printList();
deleteatbegin();
// print list
printList();
Output
Linked List:
[ 55 40 30 22 12 ]
[ 40 30 22 12 ]
Deletion at Ending
In this deletion operation of the linked, we are deleting an element from the
ending of the list.
Algorithm
1. START
2. Iterate until you find the second last element in the list.
4. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
}
printf("]");
//create a link
lk->data = data;
lk->next = head;
head = lk;
void deleteatend(){
linkedlist->next = NULL;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(40);
insertatbegin(55);
// print list
printList();
deleteatend();
// print list
printList();
}
Output
Linked List:
[ 55 40 30 22 12 ]
[ 55 40 30 22 ]
Algorithm
1. START
3. Assign the adjacent node of current node in the list to its previous node.
4. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
}
//create a link
lk->data = data;
lk->next = head;
head = lk;
head = temp->next;
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(40);
insertatbegin(55);
// print list
printList();
deletenode(30);
// print list
printList();
Output
Linked List:
[ 55 40 30 22 12 ]
[ 55 40 22 12 ]
Reverse Operation
This operation is a thorough one. We need to make the last node to be pointed
by the head node and reverse the whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we
shall make it point to its previous node −
We have to make sure that the last node is not the last node. So we'll have
some temp node, which looks like the head node pointing to the last node.
Now, we shall make all left side nodes point to their previous nodes one by
one.
Except the node (first node) pointed by the head node, all nodes should point
to their predecessor, making them their new successor. The first node will
point to NULL.
We'll make the head node point to the new first node by using the temp node.
Algorithm
1 START
3. Point the current node to head and assign its next value to the prev node.
4. Iteratively repeat the step 3 for all the nodes in the list.
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
lk->data = data;
lk->next = head;
head = lk;
while(cur!= NULL) {
tmp = cur->next;
cur->next = prev;
prev = cur;
cur = tmp;
}
*head = prev;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(40);
insertatbegin(55);
// print list
printList();
reverseList(&head);
printList();
Output
Linked List:
[ 55 40 30 22 12 ]
[ 12 22 30 40 55 ]
Search Operation
Searching for an element in the list using a key element. This operation is
done in the same way as array search; comparing every element in the list with
the key element given.
Algorithm
1 START
2 If the list is not empty, iteratively check if the list contains the key
4 END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
//create a link
lk->data = data;
lk->next = head;
head = lk;
while(temp != NULL) {
if (temp->data == key) {
return 1;
temp=temp->next;
}
return 0;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(40);
insertatbegin(55);
// print list
printList();
k = searchlist(30);
if (k == 1)
printf("\nElement is found");
else
Output
Linked List:
[ 55 40 30 22 12 ]
Element is found
Traversal Operation
The traversal operation walks through all the elements of the list in an order
and displays the elements in that order.
Algorithm
1. START
2. While the list is not empty and did not reach the end of the list, print the data
in each node
3. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
};
void printList(){
printf("\n[");
while(p != NULL) {
printf(" %d ",p->data);
p = p->next;
printf("]");
}
//insertion at the beginning
//create a link
lk->data = data;
lk->next = head;
head = lk;
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
printList();
Output
Linked List:
[ 30 22 12 ]