Linked List
Linked List
Linked List
A linked list is a collection of elements called nodes. Each node is divided into 2
parts as follows-
Node
Data *Next
arrays –
Fixed Size
Array is a static data structure i.e memory is pre-allocated to all the elements
of an array at compile time. Thus the size of an array is fixed and needs to be pre-
defined.
Ex
int a[5];
or
If you declare an array of size 10 and just assign values to first two elements
The memory is allocated to all of them even though, we may not end up
using them.
Let us say you have an array a[] = {10, 20, 40, 60, 80} and we need to add
Also, same goes for deletion if we want to delete 20. Then all elements after
it have to be moved to one position in the opposite direction.
Advantages of using Linked List
elements of a linked list at run time. Thus, the size of a linked list is not fixed
2. There is no memory wastage since here the memory is not pre-allocated for
3. Linked List does not need contiguous memory i.e. if one node has an address
of 1000 than the next node may have the address 2000.
4. Insertion and deletion operation in Linked Lists are pretty easy and less
complicated.
The Next part of last node of linked list contains NULL which means end of
the list
In a Singly linked list, each node only contains the address of the next node and not
the previous node, so we can only traverse it in forward direction but not in
backward direction.
struct node {
int data;
struct node* next;
};
This code will create a new node in a Linked List which will be storing integer type of
data.
do
{
printf("\nEnter the item:");
scanf("%d",&item);
//Allocating memory for a node in heap
node=(struct node*)malloc(sizeof(struct node));
if(node==NULL)//Check for overflow error
{
printf("Insufficient Memory");
return head;
}
node->data=item;
node->next=NULL;
if(head==NULL)
head=node;
else
last->next=node;
last=node;
printf("\nWant to add another node(y/n)?:");
getchar();//To clear the buffer
scanf(“%c”,&ans);
}while(ans=='Y'||ans=='y');
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=NULL);
}
}
int main()
{
struct node *head=NULL;
head=CREATE_LIST(head);
DISPLAY(head);
return 0;
}
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.
INS_BEG(head,item)
INS_LAST(head,item)
INS_SP_POS(head,item)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
node->data=item;
node->next=head;
head=node;
return head;
}
node->data=item;
node->next=NULL;
if(head==NULL)
{
head=node;
return head;
}
q=head;
while(q->next!=NULL)
q=q->next;
q->next=node;
return head;
}
node->data=item;
if(head==NULL)
{
node->next=NULL;
head=node;
return head;
}
printf("\nEnter position to insert node:");
scanf("%d",&pos);
if(pos<0)
{
printf("\nInvalid position. Can't insert");
return head;
}
if(pos==0)
{
head=INS_BEG(head,item);
return head;
}
q=head;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("\nInvalid position. Can't insert");
return head;
}
}
node->next=q->next;
q->next=node;
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=NULL);
}
}
int main()
{
struct node *head=NULL;
int item,choice;
while(1)
{
printf("\n\t\t\t*****OPERATION ON LINKED LIST*****");
printf("\n\t\t1.Insertion of node at begining.");
printf("\n\t\t2.Insertion of node at last.");
printf("\n\t\t3.Insertion of node at specific position");
printf("\n\t\t4.Display");
printf("\n\t\t5.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
if(choice>=1&&choice<=3)
{
printf("\nEnter the item:");
scanf("%d",&item);
}
switch(choice)
{
case 1:
head=INS_BEG(head,item);
break;
case 2:
head=INS_LAST(head,item);
break;
case 3:
head=INS_SP_POS(head,item);
break;
case 4:
DISPLAY(head);
break;
case 5:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\n\nYou entered a wrong choice.");
}
}
return 0;
}
DEL_BEG(head)
DEL_LAST(head)
DEL_SP_POS(head)
do
{
printf("\nEnter item:");
scanf("%d",&item);
node=(struct node*)malloc(sizeof(struct node));
node->data=item;
node->next=NULL;
if(head==NULL)
head=node;
else
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=node;
}
printf("\nWant to add another[y/n]:");
getchar();
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
head=head->next;
free(q);
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
while(q->next!=NULL)
{
p=q;
q=q->next;
}
if(q==head)
head=NULL;
else
p->next=q->next;
free(q);
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
for(i=0;i<pos;i++)
{
p=q;
q=q->next;
if(q==NULL)
{
printf("\nCan't delete");
return head;
}
}
p->next=q->next;
free(q);
return head;
}
void DISPLAY(struct node *head)
{
struct node *q;
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=NULL);
}
}
int main()
{
struct node *head=NULL;
int choice;
while(1)
{
printf("\n\t\t\t*****OPERATION ON LINEAR LINKED LIST*****");
printf("\n\t\t1.Insertion of nodes in linked list.");
printf("\n\t\t2.Deletion of node from begining.");
printf("\n\t\t3.Deletion of node from end.");
printf("\n\t\t4.Deletion of node from specific position.");
printf("\n\t\t5.Display");
printf("\n\t\t6.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=CREATE_LIST(head);
break;
case 2:
head=DEL_BEG(head);
break;
case 3:
head=DEL_LAST(head);
break;
case 4:
head=DEL_SP_POS(head);
break;
case 5:
DISPLAY(head);
break;
case 6:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\nWrong choice.");
}
}
return 0;
}
MERGE_LIST(P,Q,R)
Step 8: return R
struct node
{
int data;
struct node *next;
};
do
{
printf("\nEnter item:");
scanf("%d",&item);
node=(struct node*)malloc(sizeof(struct node));
node->data=item;
node->next=NULL;
if(head==NULL)
head=node;
else
last->next=node;
last=node;
printf("\nDo you want to add another node(y/n):");
getchar();
scanf("%c",&ans);
}while(ans=='Y'||ans=='y');
return head ;
}
p=P;
q=Q;
while(p!=NULL&&q!=NULL)
{
node=(struct node*)malloc(sizeof(struct node));
if(p->data<=q->data)
{
node->data=p->data;
p=p->next;
}
else
{
node->data=q->data;
q=q->next;
}
node->next=NULL;
if(R==NULL)
R=node;
else
last->next=node;
last=node;
}
while(p!=NULL)
{
node=(struct node*)malloc(sizeof(struct node));
node->data=p->data;
node->next=NULL;
last->next=node;
last=node;
p=p->next;
}
while(q!=NULL)
{
node=(struct node*)malloc(sizeof(struct node));
node->data=q->data;
node->next=NULL;
last->next=node;
last=node;
q=q->next;
}
return(R);
}
if(head==NULL)
printf("\nList is empty");
else
{
q=head;
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
}
int main()
{
struct node *P=NULL,*Q=NULL,*R=NULL;
struct node
{
int data;
struct node *next;
};
do
{
printf("\nEnter the item:");
scanf("%d",&item);
node=(struct node*)malloc(sizeof(struct node));
node->data=item;
node->next=NULL;
if(P==NULL)
P=node;
else
last->next=node;
last=node;
printf("\nDo you want to add another node(y/n):");
getchar();
scanf("%c",&ans);
}while(ans=='Y'||ans=='y');
return(P);
}
p=P;
while(p!=NULL)
{
node=(struct node*)malloc(sizeof(struct node));
node->data=p->data;
node->next=NULL;
if(Q==NULL)
Q=node;
else
last->next=node;
last=node;
p=p->next;
}
return(Q);
}
q=start;
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
int main()
{
struct node *P=NULL,*Q=NULL;
struct node* CREATE_LIST(struct node *P);
struct node* COPY_LIST(struct node *Q,struct node *P);
void DISPLAY(struct node *start);
P=CREATE_LIST(P);
Q=COPY_LIST(Q,P);
printf("\nOriginal list is\n");
DISPLAY(P);
printf("\nCopy of original list is\n");
DISPLAY(Q);
return 0;
}
INS_BEG()
DEL_BEG()
DISPLAY()
OR
INST_LAST()
DEL_LAST()
DISPLAY()
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void POP(void)
{
struct node *q;
if(top==NULL)
printf("\nstack underflow error.");
else
{
q=top;
top=top->next;
printf("\n%d is popped from stack",q->data);
free(q);
}
}
void DISPLAY(void)
{
struct node *q;
if(top==NULL)
printf("\nstack is empty.");
else
{
printf("\nStack items are as follows:-\n\n");
q=top;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
int main()
{
int item,choice;
while(1)
{
printf("\n\t\t\t*****OPERATION ON STACK*****\n\n\t\t\t\t");
printf("1.PUSH\n\n\t\t\t\t");
printf("2.POP\n\n\t\t\t\t");
printf("3.DISPLAY\n\n\t\t\t\t");
printf("4.EXIT\n\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item:");
scanf("%d",&item);
PUSH(item);
break;
case 2:
POP();
break;
case 3:
DISPLAY();
break;
case 4:
exit(0);
default:
printf("\n\nYou entered a wrong choice.");
}
}
return 0;
}
We traverse a circular singly linked list until we reach the same node where we
started. The circular singly liked list has no beginning and no ending. There is no
null value present in the next part of any of the nodes.
INS_BEG(head,item)
INS_LAST(head,item)
INS_SP_POS(head,item)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
node->data=item;
if(head==NULL)
{
head=node;
node->next=head;
return head;
}
q=head;
while(q->next!=head)
q=q->next;
q->next=node;
node->next=head;
head=node;
return(head);
}
if(node==NULL)
{
printf("\nOverflow error");
return head;
}
node->data=item;
if(head==NULL)
{
head=node;
node->next=head;
return head;
}
q=head;
while(q->next!=head)
q=q->next;
q->next=node;
node->next=head;
return head;
}
node->data=item;
if(head==NULL)
{
head=node;
node->next=head;
return head;
}
printf("\nEnter position to insert node:");
scanf("%d",&pos);
if(pos<0)
{
printf("\nInvalid position. Can't insert");
return head;
}
if(pos==0)
{
head=INS_BEG(head,item);
return head;
}
q=head;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==head)
{
printf("\nInvalid position. Can't insert");
return head;
}
}
node->next=q->next;
q->next=node;
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=head);
}
}
int main()
{
struct node *head=NULL;
int item,choice;
while(1)
{
printf("\n\t\t\t*****OPERATION ON LINKED LIST*****");
printf("\n\t\t1.Insertion of node at begining.");
printf("\n\t\t2.Insertion of node at last.");
printf("\n\t\t3.Insertion of node at specific position");
printf("\n\t\t4.Display");
printf("\n\t\t5.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
if(choice>=1&&choice<=3)
{
printf("\nEnter the item:");
scanf("%d",&item);
}
switch(choice)
{
case 1:
head=INS_BEG(head,item);
break;
case 2:
head=INS_LAST(head,item);
break;
case 3:
head=INS_SP_POS(head,item);
break;
case 4:
DISPLAY(head);
break;
case 5:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\n\nYou entered a wrong choice.");
}
}
return 0;
}
DEL_BEG(head)
DEL_LAST(head)
DEL_SP_POS(head)
do
{
printf("\nEnter item:");
scanf("%d",&item);
node=(struct node*)malloc(sizeof(struct node));
node->data=item;
if(head==NULL)
{
head=node;
node->next=head;
}
else
{
q=head;
while(q->next!=head)
q=q->next;
q->next=node;
node->next=head;
}
printf("\nWant to add another[y/n]:");
getchar();
scanf("%c",&ans);
}while(ans=='y'||ans=='Y');
return head;
}
struct node* DEL_BEG(struct node *head)
{
struct node *p,*q;
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
p=q;
while(p->next!=head)
p=p->next;
if(p==head)
head=NULL;
else
{
p->next=q->next;
head=head->next;
}
free(q);
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
while(q->next!=head)
{
p=q;
q=q->next;
}
if(q==head)
head=NULL;
else
p->next=q->next;
free(q);
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
if(pos==0)
{
head=DEL_BEG(head);
return head;
}
q=head;
for(i=0;i<pos;i++)
{
p=q;
q=q->next;
if(q==head)
{
printf("\nCan't delete");
return head;
}
}
p->next=q->next;
free(q);
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=head);
}
}
int main()
{
struct node *head=NULL;
int choice;
while(1)
{
printf("\n\t\t\t*****DELETION OPERATION ON CIRCULAR LINKED LIST*****");
printf("\n\t\t1.Insertion of nodes in linked list.");
printf("\n\t\t2.Deletion of node from begining.");
printf("\n\t\t3.Deletion of node from last.");
printf("\n\t\t4.Deletion of node from specific position.");
printf("\n\t\t5.Display");
printf("\n\t\t6.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=CREATE_LIST(head);
break;
case 2:
head=DEL_BEG(head);
break;
case 3:
head=DEL_LAST(head);
break;
case 4:
head=DEL_SP_POS(head);
break;
case 5:
DISPLAY(head);
break;
case 6:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\nWrong choice.");
}
}
return 0;
}
A doubly linked list is a collection of nodes in which each node is divided into 3 parts
as follows-
Where
In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous
nodes. However, doubly linked list overcome this limitation of singly linked list. Due
to the fact that, each node of the list contains the address of its previous node, we
can find all the details about the previous node as well by using the previous
address stored inside the previous part of each node.
int data;
INS_BEG(head,item)
INS_SP_POS(head,item)
if(pos==0)
{
head=INS_BEG(head,item);
return head;
}
q=head;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("\nInvalid position. Can't insert");
return head;
}
}
node->next=q->next;
node->prev=q;
q->next=node;
if(node->next!=NULL)
node->next->prev=q->next;
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
do
{
printf("%d\t",q->data);
q=q->next;
}while(q!=NULL);
}
}
int main()
{
struct node *head=NULL;
int item,choice;
while(1)
{
printf("\n\t\t\t*****INSERTION OPERATIONS ON DOUBLY LINKED LIST*****");
printf("\n\t\t1.Insertion of node at begining.");
printf("\n\t\t2.Insertion of node at last.");
printf("\n\t\t3.Insertion of node at specific position");
printf("\n\t\t4.Display");
printf("\n\t\t5.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
if(choice>=1&&choice<=3)
{
printf("\nEnter the item:");
scanf("%d",&item);
}
switch(choice)
{
case 1:
head=INS_BEG(head,item);
break;
case 2:
head=INS_LAST(head,item);
break;
case 3:
head=INS_SP_POS(head,item);
break;
case 4:
DISPLAY(head);
break;
case 5:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\n\nYou entered a wrong choice.");
}
}
return 0;
}
DEL_BEG(head)
DEL_LAST(head)
DEL_SP_POS(head)
do
{
printf("\nEnter item:");
scanf("%d",&item);
node=(struct node*)malloc(sizeof(struct node));
node->data=item;
node->next=NULL;
if(head==NULL)
{
node->prev=NULL;
head=node;
}
else
{
q=head;
while(q->next!=NULL)
q=q->next;
q->next=node;
node->prev=q;
}
return head;
}
struct node* DEL_BEG(struct node *head)
{
struct node *q;
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
head=head->next;
if(head!=NULL)
head->prev=NULL;
free(q);
return(head);
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
while(q->next!=NULL)
q=q->next;
if(q==head)
head=NULL;
else
q->prev->next=q->next;
free(q);
return head;
}
if(head==NULL)
{
printf("\nList is empty.");
return head;
}
printf("\nEnter position to delete:");
scanf("%d",&pos);
if(pos<0)
{
printf("\nCan't delete");
return head;
}
if(pos==0)
{
head=DEL_BEG(head);
return head;
}
q=head;
for(i=0;i<pos;i++)
{
p=q;
q=q->next;
if(q==NULL)
{
printf("\nCan't delete");
return head;
}
}
p->next=q->next;
if(q->next!=NULL)
q->next->prev=p;
free(q);
return head;
}
if(head==NULL)
printf("\nList is empty.");
else
{
printf("\nData in nodes are as follows:-\n\n");
q=head;
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->next;
}
}
}
int main()
{
struct node *head=NULL;
int choice;
while(1)
{
printf("\n\t\t\t*****DELETION OPERATION ON DOUBLY LINKED LIST*****");
printf("\n\t\t1.Insertion of nodes in linked list.");
printf("\n\t\t2.Deletion of node from begining.");
printf("\n\t\t3.Deletion of node from last.");
printf("\n\t\t4.Deletion of node from specific position.");
printf("\n\t\t5.Display");
printf("\n\t\t6.Exit");
printf("\n\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=CREATE_LIST(head);
break;
case 2:
head=DEL_BEG(head);
break;
case 3:
head=DEL_LAST(head);
break;
case 4:
head=DEL_SP_POS(head);
break;
case 5:
DISPLAY(head);
break;
case 6:
printf("\n\t\t***GOOD BYE***");
exit(0);
default:
printf("\nWrong choice.");
}
}
return 0;
}