Linked List

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 50

Linked List

A linked list is a collection of elements called nodes. Each node is divided into 2
parts as follows-

Node

Data *Next

a) Data or Info or Item part


This part contains the information
b) Next or Link or Address part
This part contains the address of next node

Where head is a pointer that contains the address of first node.


Linked List is a linear data structure like an array. However, the elements of
linked list are not in a contiguous manner and we use pointers to create
linearity in linked lists.

Why we use Linked List in Data Structure?


Generally, for storage arrays are used. But there are many disadvantages with

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

int a[] = {1, 2, 3, 4, 5};

We can’t declare an array without declaring the size.

int a[]; //Not Ok

Memory wastage (Not Optimized)

 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.

Need for Contiguous Memory

 Arrays need contiguous memory, which sometimes is bad.

Insertion in Array is expensive

 Let us say you have an array a[] = {10, 20, 40, 60, 80} and we need to add

50 in a same sorted format.

 Then, we have to move all elements after 40 to one additional position

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

1. Linked list is a dynamic data structure i.e memory is allocated to the

elements of a linked list at run time. Thus, the size of a linked list is not fixed

and hence it can grow or shrink during the execution of a program.

2. There is no memory wastage since here the memory is not pre-allocated for

the elements. Memory is allocated whenever it is required and deallocated

when it is no longer needed.

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.

Disadvantages of using Linked List


1) We have to search elements linearly, and can’t do random access.
2) We can’t do efficient searches like binary search
3) Extra space for pointers is needed
4) Loss of data threat is there, if we lose one pointer location the rest of
linked list can’t be accessed.

Types of Linked List


There are four types of linked list, that are mentioned in following ways-

1. Singly linked list

2. Circular linked list

3. Doubly linked list

4. Circular doubly linked list

Singly linked list


In a singly list there is the two field on the list.

 Data: This part contains some information.

 Next (pointer): This part contains the address of next node.

 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.

Representation of node in singly linked list

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.

Program to Create a Singly List

//We are creating program for linked list creation


#include<stdio.h>
#include<stdlib.h>
//stdlib is used to provide a declaration of ‘malloc()’ func
// structure of linked list
struct node
{
int data;
struct node *next; // Pointer pointing towards next node
};

struct node* CREATE_LIST(struct node *head)


{
struct node *node,*last;
int item;
char ans;

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;
}

//function to print the linked list


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;

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.

Insertion in singly linked list at beginning


Algorithm

INS_BEG(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Insert new node at the front of list]
set node->next = head
Step 5: [Make the new node as the first node of the list]
set head = node
Step 6: return head
Insertion in singly linked list at end
Algorithm

INS_LAST(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: set node->next = NULL
Step 5: [Check for empty list?]
If head=NULL then
set head = node
return head
[End of If structure]
Step 6: [Traverse the list till the last node]
set q = head
repeat while q->next≠Null
set q = q->next
Step 7: [Insert the node at the end]
set q->next = node
Step 8: return head

Insertion in singly linked list at specific position


Algorithm

INS_SP_POS(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Check for empty list?]
If head=NULL then
set node->next=NULL
set head = node
return head
[End of If structure]
Step 5: [Ask for position to insert]
write “Enter position to insert node”
read pos
Step 6: [Check for invalid position]
If pos<0 then
write “Invalid position”
return head
[End of If structure]
Step 7: If pos=0 then
set head = Call INS_BEG(head,item)
return head
[End of If structure]

Step 8: [Traverse the list for specific position]


set q = head
Step 9: repeat for i = 0 to less than pos-1
set q = q->next
[Check for invalid position]
If q = NULL then
write “Invalid position”
return head
[End of If structure]
[End of Step 9 loop]
Step 10: set node->next=q->next
Step 11: set q->next=node
Step 12: return head

/*A PROGRAM TO PERFORM INSERTION OPERATIONS ON SINGLY LINKED


LIST*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node* INS_BEG(struct node *head,int item)


{
struct node *node;

node=(struct node*)malloc(sizeof(struct node));


if(node==NULL)
{
printf("\nOverflow error");
return head;
}

node->data=item;
node->next=head;
head=node;
return head;
}

struct node* INS_LAST(struct node *head,int item)


{
struct node *node,*q;
int i;

node=(struct node*)malloc(sizeof(struct node));


if(node==NULL)
{
printf("\nOverflow error");
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;
}

struct node* INS_SP_POS(struct node* head,int item)


{
struct node *node,*q;
int pos,i;

node=(struct node*)malloc(sizeof(struct node));


if(node==NULL)
{
printf("\nOverflow error");
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;
}

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 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;
}

Deletion in singly linked list from begining


Algorithm

DEL_BEG(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of first node]
set q=head
Step 3: [Assigning address of second node to head]
set head=head->next
Step 4: [Delete the node at the front of list]
free(q)
Step 5: return head

Deletion in singly linked list from end


Algorithm

DEL_LAST(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of last node and its previous node]
set q=head
repeat while q->next≠NULL
set p=q
set q=q->next
[End of Step 2 loop]
Step 3: [Check for only one node]
If q=head then
set head=NULL
else
set p->next=q->next
[End of If structure]
Step 4: [Delete the node at the rear of list]
free(q)
Step 5: return head

Deletion in singly linked list from specific position


Algorithm

DEL_SP_POS(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]

Step 2: [Ask for position to delete]


write “Enter position to delete node”
read pos
Step 3: [Check for invalid position]
If pos<0 then
write “Invalid position.”
return head
[End of If structure]
Step 4: If pos=0 then
set head = Call DEL_BEG(head,item)
return head
[End of If structure]

Step 5: [Traverse the list for specific position]


set q = head
Step 6: repeat for i = 0 to less than pos
set p = q
set q = q->next
[Check for invalid position]
If q = NULL then
write “Invalid position.”
return head
[End of If structure]
[End of Step 6 loop]
Step 7: set p->next=q->next
Step 8: [Delete the node from specific position of list]
free(q)
Step 9: return head

/*A PROGRAM TO PERFORM DELETION OPERATIONS ON SINGLY LINKED


LIST*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* CREATE_LIST(struct node *head)
{
struct node *node,*q;
int item;
char ans;

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;
}

struct node* DEL_BEG(struct node *head)


{
struct node *q;

if(head==NULL)
{
printf("\nList is empty.");
return head;
}
q=head;
head=head->next;
free(q);
return head;
}

struct node* DEL_LAST(struct node *head)


{
struct node *p,*q;

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;
}

struct node* DEL_SP_POS(struct node *head)


{
struct node *p,*q;
int pos,i;

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;
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;
}

Merging of two singly linked list


Algorithm

MERGE_LIST(P,Q,R)

Step 1: [Assigning address of first node of both list to p and q


respectively]
set p = P and q = Q
Step 2: repeat while p≠NULL and q≠NULL
[Allocating memory for a node]
set node=(struct node*)malloc(sizeof(struct node))
[Compare]
If p->data<=q->data then
[Assigning data from first list]
set node->data=p->data
set p=p->next
else
[Assigning data from second list]
set node->data=q->data
set q=q->next
[End of If structure]
Step 3: set node->next=NULL
Step 4: [Check for empty list?]
If R=NULL then
set R=node
else
set last->next=node
[End of If structure]
Step 5: set last=node
[End of Step 2 loop]
Step 6: [Assign remaining data from first list]
repeat while p≠NULL
[Allocating memory for a node]
set node=(struct node*)malloc(sizeof(struct node))
set node->data=p->data
set node->next=NULL
set last->next=node
set last=node
set p=p->next
[End of Step 6 loop]
Step 7: [Assign remaining data from second list]
repeat while q≠NULL
[Allocating memory for a node]
set node=(struct node*)malloc(sizeof(struct node))
set node->data=q->data
set node->next=NULL
set last->next=node
set last=node
set q=q->next
[End of Step 7 loop]

Step 8: return R

/*A PROGRAM TO MERGE TWO SINGLY LINKED LIST*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node* CREATE_LIST(struct node *head)


{
struct node *node,*last;
int item;
char ans;

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 ;
}

struct node* MERGE_LIST(struct node *P,struct node *Q,struct node *R)


{
struct node *node,*last;
struct node *p,*q;

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);
}

void DISPLAY(struct node *head)


{
struct node *q;

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;

printf("\n\n\t\t***Creating First List***\n");


P=CREATE_LIST(P);
printf("\n\n\t\t***Creating Second List***\n");
Q=CREATE_LIST(Q);
R=MERGE_LIST(P,Q,R);
printf("\nFirst list is\n");
DISPLAY(P);
printf("\nSecond list is\n");
DISPLAY(Q);
printf("\nResultant list is\n");
DISPLAY(R);
return 0;
}

/*A PROGRAM TO COPY ONE SINGLY LINKED LIST INTO ANOTHER*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node* CREATE_LIST(struct node *P)


{
struct node *node,*last;
int item;
char ans;

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);
}

struct node* COPY_LIST(struct node *Q,struct node *P)


{
struct node *node,*last,*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);
}

void DISPLAY(struct node *start)


{
struct node *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;
}

Stack Using Linked List


We know that stack is a special type of data structure where elements are inserted
and deleted from the same end. That is, if an element is inserted at front end, an
element has to be deleted from front end. If an element is inserted at rear end, an
element has to be deleted from the rear end. Thus, a stack can be implemented
using the following functions:

 INS_BEG()
 DEL_BEG()
 DISPLAY()
OR
 INST_LAST()
 DEL_LAST()
 DISPLAY()

/*A PROGRAM TO PERFORM OPERATIONS ON STACK USING LINKED LIST*/


#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};
struct node *top=NULL;

void PUSH(int item)


{
struct node *node;
node=(struct node*)malloc(sizeof(struct node));
if(node==NULL)
printf("\nStack overflow error");
else
{
node->data=item;
node->next=top;
top=node;
printf("\n%d is pushed in stack",item);
}
}

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;
}

Circular Singly Linked List


In a circular singly linked list, the last node of the list contains a pointer to the first
node of the list.

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.

The following image shows a circular singly linked list.

Insertion in circular linked list at begining


Algorithm

INS_BEG(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Check for empty list?]
If head=NULL then
set head=node
set node->next=head
return head
[End of If structure]
Step 4: [Traverse the list]
set q=head
repeat while q->next≠head
set q = q->next
[End of loop]
Step 5: [Insert the node at the front of list]
set q->next=node
set node->next=head
set head=node
Step 6: return head

Insertion in circular linked list at last


Algorithm

INS_LAST(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Check for empty list?]
If head=NULL then
set head = node
set node->next=head
return head
[End of If structure]
Step 5: [Traverse the list]
set q = head
repeat while q->next≠head
set q = q->next
Step 6: [Insert the node at the end of list]
set q->next = node
set node->next=head
Step 7: return head
Insertion in circular linked list at specific position
Algorithm

INS_SP_POS(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Check for empty list?]
If head=NULL then
set head = node
set node->next=head
return head
[End of If structure]
Step 5: [Ask for position to insert]
write “Enter position to insert node”
read pos
Step 6: [Check for invalid position]
If pos<0 then
write “Invalid position”
return head
[End of If structure]
Step 7: If pos=0 then
set head = Call INS_BEG(head,item)
return head
[End of If structure]

Step 8: [Traverse the list for specific position]


set q = head
Step 9: repeat for i = 0 to less than pos-1
set q = q->next
[Check for invalid position]
If q = head then
write “Invalid position”
return head
[End of If structure]
[End of Step 9 loop]
Step 10: set node->next=q->next
Step 11: set q->next=node
Step 12: return head

/*A PROGRAM TO PERFORM INSERTION OPERATIONS ON CIRCULAR LINKED


LIST*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
};

struct node* INS_BEG(struct node *head,int item)


{
struct node *node,*q;

node=(struct node*)malloc(sizeof(struct node));


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;
head=node;
return(head);
}

struct node* INS_LAST(struct node *head,int item)


{
struct node *node,*q;

node=(struct node*)malloc(sizeof(struct node));

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;
}

struct node* INS_SP_POS(struct node* head,int item)


{
struct node *node,*q;
int pos,i;

node=(struct node*)malloc(sizeof(struct node));


if(node==NULL)
{
printf("\nOverflow error");
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;
}

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!=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;
}

Deletion in circular linked list from begining


Algorithm

DEL_BEG(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of first and last node]
set q=head
set p=q
repeat while p->next≠head
set p=p->next
[End of Step 2 loop]
Step 3: [Check for only one node]
If p=head then
set head=NULL
else
set p->next=q->next
set head=head->next
[End of If structure]
Step 4: [Delete the node from the front of list]
free(q)
Step 5: return head

Deletion in circular linked list from last


Algorithm

DEL_LAST(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of last node and its previous node]
set q=head
repeat while q->next≠head
set p=q
set q=q->next
[End of Step 2 loop]
Step 3: [Check for only one node]
If q=head then
set head=NULL
else
set p->next=q->next
[End of If structure]
Step 4: [Delete the node from the rear of list]
free(q)
Step 5: return head

Deletion in circular linked list from specific position


Algorithm

DEL_SP_POS(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]

Step 2: [Ask for position to delete]


write “Enter position to delete node”
read pos
Step 3: [Check for invalid position]
If pos<0 then
write “Invalid position.”
return head
[End of If structure]
Step 4: If pos=0 then
set head = Call DEL_BEG(head,item)
return head
[End of If structure]

Step 5: [Traverse the list for specific position]


set q = head
Step 6: Repeat for i = 0 to less than pos
set p = q
set q = q->next
[Check for invalid position]
If q = head then
write “Invalid position.”
return head
[End of If structure]
[End of Step 6 loop]
Step 7: set p->next=q->next
Step 8: [Delete the node from specific position of list]
free(q)
Step 9: return head

/*A PROGRAM TO PERFORM DELETION OPERATIONS ON CIRCULAR LINKED


LIST*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node* CREATE_LIST(struct node *head)
{
struct node *node,*q;
int item;
char ans;

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;
}

struct node* DEL_LAST(struct node *head)


{
struct node *p,*q;

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;
}

struct node* DEL_SP_POS(struct node *head)


{
struct node *p,*q;
int pos,i;

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==head)
{
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!=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;
}

Doubly linked list

A doubly linked list is a collection of nodes in which each node is divided into 3 parts
as follows-
Where

*Prev part stores the address of its previous(predecessor) node

Data part stores some information and

Next part stores the address of next(Successor) node

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.

Representation of node in doubly linked list


struct node

int data;

struct node *prev;

struct node *next;


} ;

Insertion in doubly linked list at begining


Algorithm

INS_BEG(head,item)

Step 1: [Allocate memory for the new node]


Set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Insert new node at the front of list]
set node->prev=NULL
set node->next = head
Step 5: If head≠NULL then
set head->prev=node
Step 6: [Make the new node as the first node of the list]
set head = node
Step 7: return head

Insertion in doubly linked list at end


Algorithm
INS_LAST(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: set node->next = NULL
Step 5: [Check for empty list?]
If head=NULL then
set node->prev = NULL
set head=node
return head
[End of If structure]
Step 6: [Traverse the list till the last node]
set q = head
repeat while q->next≠NULL
set q = q->next
Step 7: [Insert the node at the end]
set q->next = node
set node->prev=q
Step 8: return head

Insertion in doubly linked list at specific position


Algorithm

INS_SP_POS(head,item)

Step 1: [Allocate memory for the new node]


set node = (struct node*)malloc(sizeof(struct node))
Step 2: [Check for overflow error?]
If node = NULL then
write “Overflow Error”
return head
[End of If structure]
Step 3: [Insert an item into new node]
set node->data = item
Step 4: [Check for empty list?]
If head=NULL then
set node->prev=NULL
set node->next=NULL
set head = node
return head
[End of If structure]
Step 5: [Ask for position to insert]
write “Enter position to insert node”
read pos
Step 6: [Check for invalid position]
If pos<0 then
write “Invalid position”
return head
[End of If structure]
Step 7: If pos=0 then
set head = Call INS_BEG(head,item)
return head
[End of If structure]

Step 8: [Traverse the list for specific position]


set q = head
Step 9: repeat for i = 0 to less than pos-1
set q = q->next
[Check for invalid position]
If q = NULL then
write “Invalid position”
return head
[End of If structure]
[End of Step 9 loop]
Step 10: set node->next=q->next
Step 11: set node->prev=q
Step 12: set q->next=node
Step 13: If node->next≠NULL then
set node->next->prev=q->next
[End of If structure]
Step 14: return head

/*A PROGRAM TO PERFORM INSERTION OPERATIONS ON DOUBLY LINKED


LIST*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* INS_BEG(struct node *head, int item)
{
struct node *node;
node=(struct node*)malloc(sizeof(struct node));
if(node==NULL)
{
printf("\nOverflow error");
return head;
}
node->data=item;
node->prev=NULL;
node->next=head;
if(head!=NULL)
head->prev=node;
head=node;
return head;
}

struct node* INS_LAST(struct node *head, int item)


{
struct node *node,*q;
node=(struct node*)malloc(sizeof(struct node));
if(node==NULL)
{
printf("\nOverflow error");
return head;
}
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* INS_SP_POS(struct node *head, int item)


{
struct node *node,*q;
int pos,i;
node=(struct node*)malloc(sizeof(struct node));
if(node==NULL)
{
printf("\nOverflow error");
return head;
}
node->data=item;
if(head==NULL)
{
node->prev=NULL;
node->next= NULL;
head=node;
return head;
}
printf("\nEnter position to insert:");
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;
node->prev=q;
q->next=node;
if(node->next!=NULL)
node->next->prev=q->next;

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 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;
}

Deletion in doubly linked list from begining


Algorithm

DEL_BEG(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of first node]
set q=head
Step 3: [Assigning address of second node to head]
set head=head->next

Step 4: If head≠NULL then


head->prev=NULL
[End of If structure]
Step 5: [Delete the node from the front of list]
free(q)
Step 6: return head

Deletion in doubly linked list from end


Algorithm

DEL_LAST(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]
Step 2: [Getting address of last node]
set q=head
repeat while q->next≠NULL
set q=q->next
[End of Step 2 loop]
Step 3: [Check for only one node]
If q=head then
set head=NULL
else
set q->prev->next=q->next
[End of If structure]
Step 4: [Delete the node from the rear of list]
free(q)
Step 5: return head

Deletion in singly linked list from specific position


Algorithm

DEL_SP_POS(head)

Step 1: [Check for empty list?]


If head=NULL then
write “List is empty”
return head
[End of If structure]

Step 2: [Ask for position to delete]


write “Enter position to delete node”
read pos
Step 3: [Check for invalid position]
If pos<0 then
write “Invalid position.”
return head
[End of If structure]
Step 4: If pos=0 then
set head = Call DEL_BEG(head,item)
return head
[End of If structure]

Step 5: [Traverse the list for specific position]


set q = head
Step 6: repeat for i = 0 to less than pos
set p = q
set q = q->next
[Check for invalid position]
If q = NULL then
write “Invalid position.”
return head
[End of If structure]
[End of Step 6 loop]
Step 7: set p->next=q->next
Step 8: If q->next≠NULL then
set q->next->prev=p
[End of If structure]

Step 9: [Delete the node from specific position of list]


free(q)
Step 10: return head

/*A PROGRAM TO PERFORM DELETION OPERATIONS ON DOUBLY LINKED


LIST*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};

struct node* CREATE_LIST(struct node *head)


{
struct node *node,*q;
int item;
char ans;

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;
}

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 *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);
}

struct node* DEL_LAST(struct node *head)


{
struct node *q;

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;
}

struct node* DEL_SP_POS(struct node *head)


{
struct node *p,*q;
int pos,i;

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;
}

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;
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;
}

You might also like