DS UNIT - 2 Linked List
DS UNIT - 2 Linked List
o The list is not required to be contiguously present in the memory. The node can reside
anywhere in the memory and linked together to make a list. This achieves optimized
utilization of space.
o list size is limited to the memory size and doesn't need to be declared in advance.
o Empty node cannot be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.
Limitations of Array
Arrays can be used to store linear data of similar types, but arrays have the following
limitations.
1) The size of the arrays is fixed: So we must know the upper limit on the number of elements in
advance
2) Insertion and deletion operations are expensive because of extra shifting operation.
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 cannot traverse the list in the reverse direction.
There are various operations which can be performed on singly linked list. A list of all such operations is
given below.
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.
SN Operation Description
3 Insertion It involves insertion after the specified node of the linked list.
after
specified
node
The Deletion of a node from a singly linked list can be performed at different positions. Based on the
position of the node being deleted, the operation is categorized into the following categories.
SN Operation Description
3 Deletion It involves deleting the node after the specified node in the list.
after
specified
node
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. .
C program to implement single linked list operations
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
}*head= NULL;
void main()
int ch;
void add_begin();
void add_pos();
void add_end();
void del_begin();
void del_pos();
void del_end();
void display();
void search();
clrscr();
do
printf("7. Display\n");
scanf("%d",&ch);
switch(ch)
case 9: exit(0);
default:
printf("Invalid option\n");
} while(ch<=9);
} // end of main
int item;
printf("Enter element:");
scanf("%d",&item);
temp->data = item;
temp->next=NULL; \
return temp;
void add_begin()
int item;
head = temp;
temp->next = head;
head = temp;
void add_end()
{
int item;
head = temp;
right=right->next;
void add_pos()
int item,pos,i;
struct node *temp,*right,*left;
temp = create_node();
scanf("%d",&pos);
left = right;
right = right->next;
left->next= temp;
temp->next=right;
void del_begin()
if(head == NULL)
else
{
temp=head;
head=head->next;
free(temp);
void del_end()
if(head == NULL)
else
right=head;
while(right->next->next!=NULL)
right=right->next;
free(right->next);
right->next=NULL;
}
void del_pos()
int pos,i;
if(head == NULL)
else
scanf("%d",&pos);
left=right = head;
left = right;
right = right->next;
}
left->next= right->next;;
free(right);
void display()
right = head;
if(head == NULL)
else
while(right != NULL)
printf("%d \t",right->data);
right = right->next;
}
}
void search()
int key;
if(head == NULL)
else
scanf(“%d”,&key);
if (key == right->data)
printf(“Element found”);
berak;
right = right->next;
}
if(right==NULL)
Therefore, in a doubly linked list, a node consists of three parts: pointer to the previous node , node data,
pointer to the next node in sequence.
struct node
{
struct node *prev;
int data;
struct node *next;
}
Operations
SN Operation Description
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after Adding the node into the linked list after the specified node.
specified node
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the Removing the node which is present just after the node containing
node having given the given data.
data
7 Searching Comparing each node data with the item to be searched and
return the location of the item in the list if the item found else
return null.
• Update head node's left pointer to point to the new node and make new node as head.
In this case, traverse the list till the end and insert the new node.
• New node right pointer points to NULL and left pointer points to the end of the list
• Update right of pointer of last node to point to new node
Traverse the list till the position node and insert the new node.
• New node right pointer points to the next node of the position node where we want to
insert the new node. Also, new node left pointer points to the position node.
• Position node right pointer points to the new node and the next node of position nodes
left pointer points to new node
In this case, first node (current node head) is removed from the list. It can be done in two
steps:
• Create a temporary node which will point to same node as that of the head.
• Now, move the head node's pointer to the next node and change the head's left pointer
to NULL. Then, dispose the temporary node.
This operation is a bit trickier, because the algorithm has to find a node, which is previous to
• Traverse the list and while traversing maintain the previous node address also. Ny the
time we reach the end of list, we will have two pointers, one pointing to the NULL
(tail) and other pointing to the node before tail node.
In this case, node to be removed is always located between two nodes. Head and tail links are
not updated in this case. Such a removal can be done in two steps:
• As similar to previous case, maintain previous node also while traversing the list.
Once we found the node to be deleted, change the previous node's next pointer to the
next node of the node to be deleted.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
int data;
} *head = NULL;
void main()
int ch;
void add_begin();
void add_pos();
void add_end();
void del_begin();
void del_pos();
void del_end();
void display();
void search();
clrscr();
do {
printf("2. append\n");
printf("3. add at specific position\n");
printf("7. display\n");
printf("8. search\n");
printf("9. exit\n");
scanf("%d", &ch);
switch(ch)
case 9: exit(0);
default:
printf("Invalid option\n");
int item;
scanf("%d", &item);
temp->prev = NULL;
temp->data = item;
temp->next = NULL;
return temp;
void add_begin()
int item;
temp = create_node();
if (head == NULL)
head = temp;
else
temp->next = head;
head->prev = temp;
head = temp;
}
void add_end()
int item;
temp = create_node();
if (head == NULL)
head = temp;
else
right = head;
right = right->next;
right->next = temp;
temp->prev = right;
void add_pos()
temp = create_node();
scanf("%d", &pos);
left = right;
right = right->next;
left->next = temp;
temp->prev = left;
temp->next = right;
right->prev = temp;
void del_begin()
if (head == NULL)
printf("List is empty\n");
else
temp = head;
head = head->next;
free(temp);
head->prev = NULL;
}
void del_end()
if (head == NULL)
printf("List is empty\n");
else
right = head;
right = right->next;
free(right->next);
right->next = NULL;
void del_pos()
int pos, i;
if (head == NULL)
printf("List is empty\n");
else
scanf("%d", &pos);
left = right;
right = right->next;
left ->next=right->next;
right->next->prev = left;
free(right);
void display()
right = head;
if (head == NULL)
printf("List is empty\n");
else
right = head;
printf("%d\t", right->data);
last = right;
right = right->next;
}
printf("Elements in reverse direction\n");
printf("%d\t", last->data);
last = last->prev;
void search()
right = head;
if (head == NULL)
printf("List is empty\n");
else
scanf("%d", &key);
right = head;
If(key==right->data)
flag=1;
break;
}
else
right=right->next;
If(flag==1)
Printf(“element found”);
else