0% found this document useful (0 votes)
14 views62 pages

Unit 2 Notes

The document covers the concepts of Queues and Linked Lists, detailing their operations, implementations, and applications. It explains various types of queues such as Circular Queue, DeQueue, and Priority Queue, along with algorithms for insertion and deletion. Additionally, it discusses the implementation of queues using arrays and stacks, and provides C programming examples for practical understanding.

Uploaded by

Sankalp Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views62 pages

Unit 2 Notes

The document covers the concepts of Queues and Linked Lists, detailing their operations, implementations, and applications. It explains various types of queues such as Circular Queue, DeQueue, and Priority Queue, along with algorithms for insertion and deletion. Additionally, it discusses the implementation of queues using arrays and stacks, and provides C programming examples for practical understanding.

Uploaded by

Sankalp Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Queues

and
Linked List (Unit-2)
Queue
Data Structure
Queues
• Basic Queue operations (insertion, deletion)
• Representation of Queue using array
• Implementation of Queue operations using Stack
• Applications of Queue
• Circular Queue, DeQueue, Priority Queue
Linked List:

• Single linked list, Different operations on single linked list, Reversing single linked list
• Circular linked list
• Double linked list
• Header linked list
Queue:
It is FIFO(First In First Out) linear data structure, in which the element which is inserted first will be the first
element to be removed.
In Queue insertion and deletion take place at separate ends: FRONT and REAR.
Insertion takes place at REAR end and deletion take place at FRONT end.

e.g. People waiting at ticket window

FRONT REAR
Features of Queue:
1. Like stack, it is also ordered list of items
2. It is FIFO structure
3. Once an element in queue, It is removed only when all the elements already in queue are removed

Applications of Queue
1. Serving request on a single shared resource like Printer, disk, CPU
2. Call center phone system uses queue to hold people calling them in an order until a service representative
is free

Manoj Kataria BKBIET, pilani


Implementation of Queue:
A queue can be implemented by using an array , stack or linked list
Static Dynamic
In array representation we take an array of fixed size and two variables : front and rear

0 1 2 3 4 5 6 0 1 2 3 4 5 6
5
initially Queue is empty front=-1, rear=-1 Insert first element front=0, rear=0

0 1 2 3 4 5 6 0 1 2 3 4 5 6
5 9 5 9 8
Insert second element front=0, rear=1 Insert third element front=0, rear=2

0 1 2 3 4 5 6 0 1 2 3 4 5 6 MAX-1
9 8 9 8 6
Delete first element front=1, rear=2 Insert next element front=1, rear=3

Manoj Kataria BKBIET, pilani


Queue operations: /*insert function*/
Insert operation: void insert()
Algorithm to insert an element in Queue {
1. IF (REAR = MAX-1) int item;
print “Queue is Full” if(rear==MAX-1)
2. EXIT printf(“Queue is Full”);
3. Else else
4. IF (FRONT=-1) and (REAR=-1) {
FRONT=0 if(front==-1)
ENDIF front=0;
5. REAR=REAR+1 printf(“Enter item to insert:”);
6. Q[REAR]=ITEM scanf(“%d”,&item);
7. ENDIF rear++;
8. END q[rear]=item;
}
}

Manoj Kataria BKBIET, pilani


Queue operations: /*delete function*/
Delete operation: void delete()
Algorithm to delete an element from Queue {
1. IF (FRONT = -1) int item;
print “Queue is Empty” if(front=-1)
2. EXIT printf(“Queue is Empty”);
3. ElSE else
ITEM=Q[FRONT] {
IF(FRONT=REAR) if(front==rear)
REAR=-1 {
FRONT=-1 item=q[front];
ELSE front=rear=-1;
FRONT=FRONT+1 printf(“Deleted element is: %d”,item);
ENDIF }
4. ENDIF else
5. END {
item=q[front];
front++;
printf(“Deleted element is: %d”,item);
}
}
Manoj Kataria BKBIET, pilani
}
/*C program for array implementation of queue*/ case 2:
#include<stdio.h> delete();
#include<conio.h> break;
#define MAX 10 case 3:
void insert(); display();
void delete(); break;
void display(); default:”
int q[MAX], rear=-1, front=-1; printf(“\nChoice is wrong”);
void main() }
{ printf(“\nContinue 1 for yes:”);
int k=1,choice; scanf(“%d”,&k);
clrscr(); }
while(k==1) }
{ /*display function*/
printf(“\n1. Insert \n2. Delete \n3. Display”); void display()
printf(“\nEnter you choice:”); {
scanf(“%d”,&choice; int i;
switch(choice) if(front==-1)
{ printf(“\nQueue empty”);
case 1: else
insert(); {
break; for(i=front;i<=rear;i++)
printf(“%d\t”,q[i]);
}
Manoj Kataria BKBIET, pilani
0 1 2 3 4 5 6 MAX-1
0 1 2 3 4 5 6 MAX-1
8 6
6
delete next element front=2, rear=3
delete next element front=3, rear=3

0 1 2 3 4 5 6 MAX-1 0 1 2 3 4 5 6 MAX-1
6 4 6 4 3
insert next element front=3, rear=4 insert next element front=3, rear=5

0 1 2 3 4 5 6 MAX-1
6 4 3 7
insert next element front=3, rear=6 Queue is Full

Manoj Kataria BKBIET, pilani


Implementation of Queue operations using Stack:
Queue is FIFO data structure in which the elements are removed in the same order in which they are added to
the Queue. So we also implement queue operations using stack.(two stacks – stack1 and stack2)

To implement queue using stack, there are two approaches:


1. To insert(enqueue) an item in the queue ,we first move all elements from the first stack to second stack,
then push item into first stack and finally move all elements back to first stack.
This ensures that the new item lies at bottom of stack and hence be the last to be removed.

A B
A B A B C A
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
Insert ‘A’ Insert ‘B’ Insert ‘C’

This is the way we will maintain the right order of elements in the stack, with first element always staying at the
top of stack. This make removing an element from the queue very simple.

*PUSH operation will be done on stack1 and POP operation will be done on stack2
Manoj Kataria BKBIET, pilani
2. To delete(dequeue) an item from queue we simply return the top item from the second stack.
Move all the elements from stack1 to stack2 one by one. Then remove the top element from stack2 and then
move back all the elements from the second stack to the first stack.
The purpose of moving all the elements from stack1 to stack2 is to reverse the order of elements ( the first
element inserted into the queue is positioned at the top of second stack.

C C A
B B B B
A A C C
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
delete(dequeue) top element from stack2 removed

C B
B C C C
stack1 stack2 stack1 stack2 stack1 stack2 stack1 stack2
delete(dequeue) top element from stack2 removed
Manoj Kataria BKBIET, pilani
Applications of Queue:
1. Queue is useful in Round robin algorithm: It is a CPU scheduling algorithm where each process is
assigned a fixed time slot in a cyclic way. It deals with all process without any priority. All process get a
fair allocation of CPU.

P1 P2 P3 P4 Queue of four process

Time slot
P1
0 2 4 6 8 10

P1 P2
0 2 4 6 8 10

P1 P2 P3
0 2 4 6 8 10

P1 P2 P3 P4
0 2 4 6 8 10Kataria BKBIET, pilani
Manoj
Circular Queue:
In linear queue we can insert elements until queue becomes full.
0 1 2 3 4 5
10 85 9 64 15 30
front=0, rear=MAX-1 Queue is Full

Delete two elements


0 1 2 3 4 5
9 64 15 30
front=2, rear=MAX-1 Still Queue becomes full though two elements are deleted

0 1 2 3 4 5
30
front=MAX-1, rear=MAX-1 Still Queue becomes because rear at last position

We have empty positions in the queue , we cannot make use of them to insert new element. This is the
problem in a Linear queue.
To overcome this problem we use a Circular queue data structure.
Manoj Kataria BKBIET, pilani
A circular queue is a linear data structure in which operations are performed based on FIFO basis and the last
position connected back to the first position to make a circle.
0 1 2 3 4 5 0 1 2 3 4 5
90 10 15 54 64 91

front=-1, rear=-1 Queue is empty front=0 rear=5(MAX-1)


(Queue full)

0 1 2 3 4 5 0 1 2 3 4 5
15 54 64 91 54 64 91

front =2 rear=5 front =3 rear=5(MAX-1)

0 1 2 3 4 5 0 1 2 3 4 5
20 54 64 91 20 25 30 54 64 91

rear=0 front=3 rear=2 front=3 (Queue full)


Manoj Kataria BKBIET, pilani
Algorithm (Insertion) /*insert function*/
1. IF(FRONT=0 and REAR=MAX-1) OR void insert()
FRONT=REAR+1 {
int item;
then print “Queue is Full”
if((front==0 && rear==MAX-1)||(front==rear+1)
EXIT
printf(“\nQueue is Full”);
2. IF(REAR=-1) else
FRONT=0 {
REAR=0 if(rear==-1)
3. IF(REAR=MAX-1) and FRONT!=0 front=rear=0;
REAR=0 else
ELSE if(front!=0 && rear==MAX-1)
REAR=REAR+1 rear=0;
else
4. Q[REAR]=ITEM
rear++;
5. EXIT
printf(“\nEnter item to insert:”);
scanf(“%d”,&item);
q[rear]=item;
}
}

Manoj Kataria BKBIET, pilani


Algorithm (Deletion) /*delete function*/
void delete()
1. IF(FRONT=-1)
{
then print “Queue is Empty” int item;
EXIT if(front==-1)
2. IF(REAR=FRONT) printf(“\nQueue is empty”);
else
FRONT=-1
{
REAR=-1 if(front==rear) /*last element*/
3. IF(FRONT=MAX-1) {
FRONT=0 item=q[front];
printf(“\nDeleted element is %d”,item);
ELSE
front=rear=-1;
FRONT=FRONT+1 }
4. EXIT else
if(front==MAX-1)
{
item=q[front];
printf(“\nDeleted element is %d”,item);
front=0;
}
else
{
item=q[front];
printf(“\nDeleted element is %d”,item);
front++;
Manoj Kataria BKBIET, pilani
}
/*C program for the implementation of circular queue*/ case 2:
#include<stdio.h> delete();
#include<conio.h> break;
#define MAX 10 case 3:
void insert(); display();
void delete(); break;
void display(); default:”
int q[MAX], rear=-1, front=-1; printf(“\nChoice is wrong”);
void main() }
{ printf(“\nContinue 1 for yes:”);
int k=1,choice; scanf(“%d”,&k);
clrscr(); }
while(k==1) }
{
printf(“\n1. Insert \n2. Delete \n3. Display”);
printf(“\nEnter you choice:”);
scanf(“%d”,&choice;
switch(choice)
{
case 1:
insert();
break;

Manoj Kataria BKBIET, pilani


/*display function*/
void display()
{
int i;
if(f==-1)
printf(“\nQueue empty”);
else
if(front>rear)
{
for(i=0;i<=rear;i++)
printf(“%d\t”,q[i]);
for(i=front;i<=MAX-1;i++)
printf(“%d\t”,q[i]);
}
else
{
for(i=front;i<=rear;i++)
printf(“%d\t”,q[i]);
}
}

Manoj Kataria BKBIET, pilani


DeQueue:
It is double ended queue in which insertion and deletion take place at either end (i.e. front or rear)
insertion insertion

deletion deletion
front rear
DeQueue can be represented in two ways : a). Input restricted b). Output restricted
In input restricted, the insertion is performed at only one end and deletion operation is performed at both
ends.
In output restricted, the deletion is performed at only one end and insertion operation is performed at both
ends.

Insert from FRONT 0 1 2 3 4 5


0 1 2 3 4 5
10
FRONT=0 REAR=0 because FRONT=0
FRONT=-1 REAR=-1 Queue is Empty
not possible to insert from FRONT

0 1 2 3 4 5 0 1 2 3 4 5
30 40 60 15 30 40 60
FRONT=2 REAR=4 Manoj Kataria BKBIET,FRONT=1
pilani REAR=4
FRONT !=0 then FRONT is decremented
Insert from REAR:
0 1 2 3 4 5 0 1 2 3 4 5
20 30 40 50 20 30
FRONT=2 REAR=5 (MAX-1) because REAR=MAX-1 FRONT=2 REAR=3 REAR!=MAX-1
not possible to insert from REAR then REAR is incremented

0 1 2 3 4 5
20 30 45

FRONT=2 REAR=4

Delete from REAR:


0 1 2 3 4 5 0 1 2 3 4 5
20 20 30 45
FRONT= REAR only one element
FRONT=2 REAR=4 REAR is decremented
FRONT=REAR=-1
Delete from FRONT:
0 1 2 3 4 5 0 1 2 3 4 5
20 20 30 45
FRONT= REAR only one element FRONT=2 REAR=4 FRONT is incremented
FRONT=REAR=-1
Manoj Kataria BKBIET, pilani
/*function to insert from front end*/ /*function to insert from rear end*/
void insertf() void insertr()
{ {
int item; int item;
if(front==0) if(rear==MAX-1)
printf(“\nNot possible to insert from front”); printf(“\nNot possible to insert from rear”);
else else
{ {
if(front==-1) if(rear==-1)
front=rear=0; front=rear=0;
else else
front--; rear++;
printf(“\nEnter item to insert from front:”) printf(“\nEnter item to insert from rear:”)
scanf(“%d”,&item); scanf(“%d”,&item);
q[front]=item; q[rear]=item;
} }
} }

Manoj Kataria BKBIET, pilani


/*function to delete from front end*/ /*function to delete from rear end*/
void deletef() void deleter()
{ {
int item; int item;
if(front==-1) if(rear==-1)
printf(“\nQueue is empty”); printf(“\nQueue is empty”);
else else
{ {
if(front==rear) /*last element*/ if(front==rear) /*last element*/
{ {
item=q[front]; item=q[rear];
printf(“\nDeleted element : %d”,item); printf(“\nDeleted element : %d”,item);
front=rear=-1; front=rear=-1;
} }
else else
{ {
item=q[front]; item=q[rear];
printf(“\nDeleted element : %d”,item); printf(“\nDeleted element : %d”,item);
front++; rear--;
} }
} }
Manoj Kataria BKBIET, pilani
} }
/*program for DeQueue*/ case 2:
#include<conio.h> insertr();
#include<stdio.h> break;
void insertf(); case 3:
void insertr(); deletef();
void deletef(); break;
void delete(); case 4:
void display(); deleter();
void main() break;
{ case 5:
int k=1,choice; display();
clrscr(); break;
while(k==1) default:
{ printf(“\nChoice is wrong”);
printf(“\n1. Insert at front \n2. Insert at rear } /*end switch*/
\n3. Delete from front \n 4. Delete from rear printf(“\nContinue 1 for yes:”);
\n5. Display”); scanf(“%d”,&k);
printf(“\nEnter your choice:”); } /*end while*/
scanf(“%d”,&choice); } /*end main*/
switch(choice) void display()
{ {
case 1: int k;
insertf(); for(k=front;k<=rear;k++
break; Manoj Kataria BKBIET, pilani printf(“%d \t”,q[k]);
}
Priority Queue :
A priority queue is linear data structure in which element has a priority based on the urgency of the need. The
priority of the element will be used to determine the order in which elements will be processed.
It is not based on FIFO basis.

An element with higher priority is processed before any element of lower priority.
Two elements with same priority are processed in the order in which they are inserted in the queue
(FCFS- First Come First Serve basis).

e.g. Hospital waiting room, A patient having more problem would be admitted before other patients

*Priority queues are widely used in operating systems to execute the highest priority process first.
Suppose there are three processes, where the first process need 5 seconds to complete , second process needs
4 seconds and the third process needs 7 seconds, then the second process will have the highest priority and
will thus be the first to be executed.

Array representation of a Priority Queue:


In this a separate queue for each priority number is maintained. Each of these queues will be implemented
using circular arrays or circular queue.
Every individual queue will have its own FRONT and REAR pointers.
Manoj Kataria BKBIET, pilani
We use a two-dimensional array for priority queue where each queue will be allocated the same amount space.

Two dimensional representation of Priority queue: 1 2 3 4 5 FRONT REAR


1 A First Queue 2 2
2 B C second Queue 3 4
3 D Third Queue 1 1
4 E F G Fourth Queue 4 1
FRONT and REAR
of each queue
FRONT[K] and REAR[K] contains the front and rear of row K, where K is priority number.
To insert a new element with priority K in the priority queue, add the element at the rear end of row K , where K
is the row number as well as the priority number of that element.
If we insert an element H with priority 2, then the priority queue will be given as shown:
1 2 3 4 5 FRONT REAR
1 A First Queue 2 2
2 B C H second Queue 3 5
3 D Third Queue 1 1
4 E F G Fourth Queue 4 1
Manoj Kataria BKBIET, pilani
Linked list
Data Structure
Linked list:
In array once memory is allocated it cannot be extended. The elements of array are stored in contiguous
memory locations. Also memory space is wasted, as memory remains allocated to the array even few elements
are stored and their storage can’t be used for any purpose.

A linked list is an ordered collection of data elements called nodes. Each node is divided into two parts, the first
part contains the information of the element and second part contains the address of next node in the list.
Node
INFO Address part(pointer) contains address of next node in the list
START

Pointer contains address of first node in the list

In linked list memory can be allocated for only one node at a time and can be extended at any time. So for
linked list memory is dynamically allocated.

Manoj Kataria BKBIET, pilani


Single linked list(Linear linked list):
In single linked each node is divided into two parts, the first part is the informational part (contains information)
and second part is the pointer which contains the address of next node in the list.
INFO NEXT
START 1000

25 1008 35 1026 45 0

Address of next node


Advantages:
1. Linked list are dynamic data structure they can grow or shrink during the execution of program
2. The size of linked list is not fixed
3. Insertion and deletion of node are easier and efficient
4. Data is stored in non-contiguous memory blocks

Disadvantages:
1. Extra memory space is required to store address
2. No element can be accessed randomly, access each node sequentially

Manoj Kataria BKBIET, pilani


Memory allocation:
1. Static memory allocation
2. Dynamic memory allocation

Functions for dynamic allocation:


1. malloc() -> allocate requested size of bytes and returns a pointer to the first byte of allocated space
2. free() -> deallocates the previously allocated space

Syntax:
ptr = (int *) malloc (byte size);
Type cast
Pointer to cast type

e.g.
ptr = (int *) malloc (8*sizeof(int));

Pointer to first byte 8*2=16 bytes


of memory

free(ptr); Manoj Kataria BKBIET, pilani


Linked list operations:
1. Creation : It is used to create constituent node as and when required
2. Insertion : It is used to insert a new node in the linked list (at the beginning, certain position or at the end)
3. Deletion : Deleting an existing node from linked list (first node, last node or any other node)
4. Traversing : It is the process of going through all the nodes of linked list
5. Concatenation : It is the process of appending one list at the end of another list

Structure of Single linked list:

struct list
{
It is a self referential
int info; structure, it contains a
struct list *next; pointer points to itself
};

info next

Manoj Kataria BKBIET, pilani


Representation of linked list in memory:
START 1006

25 1008 35 1002 45 0

info field next field

info next
START 1006
1000
1001
1002 45 0
1003
1004
1005
1006 25 1008
1007
Memory 1008 35 1002
locations
1009
1010
Manoj Kataria BKBIET, pilani
Single linked list creation:
struct list
{
int info;
struct list *next;
};
typedef struct list node; /*node is variable of structure*/
node *start; /*start pointer contains address of first node*/
1. start = (node *)malloc(sizeof(node)); /*allocate space for first node*/
start->info=25;
start->next=NULL; start 1006

25 0

2. node *newnode; /*nownode pointer*/


newnode = (node *)malloc(sizeof(node)); /*allocate space for next node*/
newnode->info=35;
newnode
newnode->next=NULL; start->next=newnode; /*link new node with first node*/
1008
start 1006
35 0
Manoj Kataria BKBIET, pilani 25 1008 35 0
3. newnode = (node *)malloc(sizeof(node)); /*allocate space for next node*/
newnode->info=35;
newnode->next=NULL; newnode
1002 start 1006
temp1 next
45 0 25 1008 35 0

node *temp1;
temp1=start; start 1006
while(temp1->next!=NULL) next
{ 25 1008 35 0
temp1=temp1->next; temp1
}
temp1->next=newnode;

start 1006

25 1008 35 1002 45 0

Manoj Kataria BKBIET, pilani


Insertion operation (single linked list):
1. Insert new node at the beginning: start 1006
Allocate space for new node
node *newnode; /*pointer to new node*/
25 1008 35 1002 45 0
newnode=(node *)malloc(sideof(node));

newnode 1024
info next

newnode->info=58; start
newnode->next=start; 1024
start=newnode;
58 1006 25 1008 35 1002 45 0

2. Insert new node at the end:


Allocate space for new node newnode 1014
node *newnode; /*pointer to new node*/ info next
newnode=(node *)malloc(sideof(node));
newnode 1014
newnode->info=44; info next
newnode->next=NULL; Manoj Kataria BKBIET, pilani
44 0
start
node *temp1;
1024 temp1 temp1 temp1 temp1
temp1=start;
while(temp1->next!=NULL)
58 1006 25 1008 35 1002 45 0
{
temp1=temp1->next;
} newnode 1014
temp1->next=newnode;
44 0
start
1024

58 1006 25 1008 35 1002 45 1014 44 0

3. Insert new node at a certain position:


start 1006 before this node
insert new node before the node
with info 35 (item=35)
25 1008 35 1002 45 0
Allocate space for new node
node *newnode; /*pointer to new node*/ newnode 1014
newnode=(node *)malloc(sideof(node)); info next
Manoj Kataria BKBIET, pilani
newnode start
newnode->info=58; 1014 1006 temp1

/*search node with info 35*/ 58 25 1008 35 1002 45 0


node *temp1;
temp1=start;
while(temp1->next!=NULL)
{ start
if(temp1->next->info==item) 1006 temp1
break;
else 25 1008 35 1002 45 0
temp1=temp1->next;
}
newnode->next=temp1->next; newnode
temp1->next=newnode; 1014

58
start
1006

25 1014 58 1008 35 1002 45 0

Manoj Kataria BKBIET, pilani


Deletion operation (single linked list):
1. Delete first node : start 1006 becomes first node and start points to this node

25 1008 35 1002 45 0

start =start->next;
start 1008

35 1002 45 0

2. Delete last node: temp


start 1006 becomes last node next is NULL

25 1008 35 1002 45 0
temp
temp=start;
while(temp->next->next!=NULL) start 1006
temp=temp->next;
temp->next=NULL; 25 1008 35 0

Manoj Kataria BKBIET, pilani


3. Delete intermediate node:
start
temp 1024
temp
58 1006 25 1008 35 1002 45 1014 44 0
item=35;
temp=start;
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
break;
}
temp=temp->next;
} start
1024

58 1006 25 1002 45 1014 44 0

Manoj Kataria BKBIET, pilani


Display operation:(single linked list)
Display the content of info part of each node.
start
1006
info next
25 1014 58 1008 35 1002 45 0

temp=start;
while(temp->next!=NULL) start
{ 1006 temp
printf("%d\t",temp->info); temp
temp=temp->next; 25 1014 58 1008 35 1002 45 0
} temp
printf("%d",temp->info);

Manoj Kataria BKBIET, pilani


Reversing a single linked list:
Reversing a SLL means reverse the order of nodes of given linked list.
start
1006

25 1014 58 1008 35 1002 45 0

After reversing
start
1002

45 1008 35 1014 58 1006 25 0

Steps:
1. Create two pointers:
*pnode -> holds the reference of previous node
*cnode -> holds the reference of current node

Manoj Kataria BKBIET, pilani


1002
start
45 1008 35 1014 58 1006 25 0
pnode cnode
pnode -> points to first node start -> points to its next node(second node start=start->next)
cnode -> points to second node

2. Now disconnect the pnode from others (make sure it points to none)

1002
start
45 0 35 1014 58 1006 25 0
pnode cnode
3. Move start to its next node start=start->next

45 0 35 1014 58 1006 25 0
pnode cnode start

Manoj Kataria BKBIET, pilani


4. Now reconnect the current node to its previous node
cnode->next=pnode
45 0 35 1002 58 1006 25 0
pnode cnode start

5. Point the previous node to current node and current to start node
pnode=cnode cnode=start
start
45 0 35 1002 58 1006 25 0
pnode cnode

6. Repeat steps 3 to 5 till start becomes NULL


start

45 0 35 1002 58 1006 25 0
pnode cnode
start

45 0 35 1008 58 1006 25 0
pnode cnode
Manoj Kataria BKBIET, pilani
//function for reversing linked list
void rev()
{
node *pnode,*cnode;
pnode=start;
cnode=start->next;
start=pnode->next;
if(start==NULL)
printf("\nOnly one node in list");
else
{
pnode->next=NULL;
while(start!=NULL)
{
start=start->next;
cnode->next=pnode;
pnode=cnode;
cnode=start;
}
start=pnode;
} Manoj Kataria BKBIET, pilani
}
Circular Linked list:
It is a linked list in which the last node points back to the first node to form a circle. In this every node has a link
to its next element in the sequence.
start 1006

25 1014 58 1008 35 1002 45 1006

Advantages:
1. Every node is accessible from a given node. All nodes can be reached by merely chaining through the list.
2. Useful for the implementation of circular queue
3. It is useful in applications to repeatedly go through the list
4. Multiplayer games ,all the players kept in circular linked list and pointer keeps on moving forward as a player’s
chance ends

Disadvantages:
1. Requires an extra care to detect the end of the list. It may be possible to get into an infinite loop

Manoj Kataria BKBIET, pilani


Operations:
In circular linked list, we perform the following operations
1. Insertion
2. Deletion
3. Display

Insertion operation:
Insertion operation can be performed in three ways:
1. Inserting at the beginning
2. Inserting at the end
3. Inserting at specific location

Inserting at the beginning of list


start 1006

25 1014 58 1008 35 1002 45 1006

Manoj Kataria BKBIET, pilani


//allocate space for new node newnode 1024
newnode=(node *)malloc(sizeof(node)); info next
newnode->info=58 58 1006
newnode->next=start;
temp=start; temp
while(temp->next!=start) start 1006
temp temp temp
temp=temp->next;
25 1014 58 1008 35 1002 45 1006

start=newnode;
temp->next=start;

1024

58 1006 25 1014 58 1008 35 1002 45 1024

Manoj Kataria BKBIET, pilani


Inserting at the end:
start 1006

25 1014 58 1008 35 1002 45 1006

//allocate space for new node


newnode=(node *)malloc(sizeof(node)); newnode 1012
newnode->info=26; info next
newnode->next=start; 26 1006
temp
temp=start; start 1006
while(temp->next!=start) temp temp temp
temp=temp->next; 25 1014 58 1008 35 1002 45 1006
temp->next=newnode;

start 1006

25 1014 58 1008 35 1002 45 1012 26 1006

Manoj Kataria BKBIET, pilani


Deletion operation:
Deletion operation can be performed in three ways:
1. Deleting first node
2. Deleting last node
3. Deleting intermediate node

Deleting first node: start 1006

25 1014 58 1008 35 1002 45 1006

temp=start;
printf("\nDeleted node:%d",start->info);
while(temp->next!=start) temp
temp=temp->next; start 1006
start=start->next; temp temp temp
temp->next=start; 25 1014 58 1008 35 1002 45 1006

start 1014

58 1008 35 1002 45 1014


Manoj Kataria BKBIET, pilani
Deleting last node:
start 1006

25 1014 58 1008 35 1002 45 1006

temp=start;
while(temp->next->next!=start)
temp=temp->next;
printf("\nDeleted node:%d",temp->next->info);
temp->next=start;
temp
start 1006
temp temp
25 1014 58 1008 35 1002 45 1006

start 1006

25 1014 58 1008 35 1006

Manoj Kataria BKBIET, pilani


Double Linked List:
In singly linked list we can traverse the list in one direction i.e. in forward direction (from first to last node),
because we can store address of next successor.
DLL is a variation of linked list in which navigation is possible in both ways i.e. either forward and backward.
It is a two way list in which every element has link to its previous element and next element in the sequence.
Each node has a link to its previous node and next node.

Each node is divided into three parts:

PREV NEXT
(address of previous node) INFO
(address of next node)

LAST
START

Manoj Kataria BKBIET, pilani


Memory representation of DLL:
start 102 106 last

0 50 108 102 21 106 108 65 0


prev next prev next prev next

INFO PREV NEXT


start 102
101
102 50 0 108
103
104
105
106 65 108 0
107
108 21 102 106
last 103
109
Manoj Kataria BKBIET, pilani
Advantages of DLL:
1. A DLL can be traversed in both forward and backward direction
2. The delete operation in DLL is more efficient as compare to SLL
3. It is easy to reverse the linked list

Disadvantages of DLL:
1. A DLL occupy more space, to store two pointers
2. All operations require an extra pointer to be maintained. So insertion and deletion take more time as
compare to SLL

Structure of DLL:
struct list
{
struct list *prev;
int info;
struct list *next;
};

Manoj Kataria BKBIET, pilani


Operations:
In doubly linked list, we perform the following operations
1. Creation
2. Insertion
3. Deletion
4. Display

Creation:
A linked list can be created node by node

newnode=(node *)malloc(sizeof(node));
newnode->info=50;
start=last=newnode; newnode
newnode->prev=NULL; start 102 102 last
newnode->next=NULL;
0 50 0
prev next

Manoj Kataria BKBIET, pilani


do newnode
start 102 102 last
{
printf("More node 1 for yes:");
0 50 0
scanf("%d",&k);
prev next
if(k==1)
{
newnode->next=(node *)malloc(sizeof(node));
newnode->next->prev=newnode;
newnode=newnode->next;
}
else
{
newnode->next=NULL;
last=newnode;
}
}while(k==1); newnode
} start 102 newnode 106 last

0 50 106 102 58 0
prev next prev next
Manoj Kataria BKBIET, pilani
Insertion Operation:
Insertion operation can be performed in three ways:
1. Inserting at the beginning
2. Inserting at the end
3. Inserting at specific location
start 102 106 last
Inserting at the beginning of list:
0 50 106 102 58 0
//allocate space for new node prev next
newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:"); newnode
scanf("%d",&newnode->info); 112
newnode->prev=NULL;
newnode->next=start; 0 33 102
start->prev=newnode; prev next
start=newnode;

112 106 last

0 33 102 112 50 106 102 58 0


prev next Manoj Kataria BKBIET, pilani
Inserting at the end: start 102 106 last

0 50 106 102 58 0
prev next
//allocate space for new node
newnode=(node *)malloc(sizeof(node));
newnode
printf("Enter info for new node:");
112
scanf("%d",&newnode->info);
newnode->next=NULL; 106 33 0
newnode->prev=last; prev next

last->next=newnode;
last
last=newnode;
start 102 112

0 50 106 102 58 112 106 33 0

Manoj Kataria BKBIET, pilani


Inserting at specific location: last

start 102 112

//allocate space for new node 0 50 106 102 58 112 106 33 0


newnode=(node *)malloc(sizeof(node));
printf("Enter info for new node:"); newnode
scanf("%d",&newnode->info);
printf("\nEnter info of target node:");
110
scanf("%d",&t);
temp=start;
18
while(temp->next!=NULL) prev next
{
if(temp->next->info==t)
break; newnode
else
110
temp=temp->next;
}
102 18 106
if(temp->next==NULL) last
printf("\nNode not fund"); temp new link new link 112
else 102
{
newnode->next=temp->next; 0 50 110 110 58 112 106 33 0
newnode->prev=temp;
temp->next->prev=newnode; old link
temp->next=newnode;
Manoj Kataria BKBIET, pilani
}
Deletion Operation:
Deletion operation can be performed in three ways:
1. Deleting first node
2. Deleting last node
3. Deleting intermediate node

Deleting first node: last


start 102 112
temp
0 50 106 102 50 118 106 58 112 118 33 0

temp=start->next;
printf("\nDeleted node:%d",start->info);
temp->prev=NULL;
start=temp;
last

start 106 112

0 50 118 106 58 112 118 33 0

Manoj Kataria BKBIET, pilani


Deleting last node: last
start 102 112
temp
0 50 106 102 50 118 106 58 112 118 33 0
prev next

temp=last->prev;
printf("\nDeleted node:%d",last->info);
temp->next=NULL;
last=temp;
last
start 102 118

0 50 106 102 50 118 106 58 0

Manoj Kataria BKBIET, pilani


Deleting intermediate node: last
start 102 temp 112
temp
0 50 106 102 50 118 106 58 112 118 33 0
temp=start;
printf("\nWhich node to be deleted:");
scanf("%d",&item);
while(temp->next->next!=NULL)
{
if(temp->next->info==item)
{
printf("\nDeleted element is:%d",temp->next->info);
temp->next=temp->next->next;
temp->next->prev=temp;
break; last
} start 102 112
temp=temp->next;
} 0 50 106 102 50 112 106 33 0

Manoj Kataria BKBIET, pilani


Header Linked List:
It is a special type of linked list in which a special node present at the beginning of the list called Header node.
The header node is used to store number of nodes in the linked list. In other linked list , if we want to know the
size of linked list we use traversal method.
The count in the header linked list must be adjusted after adding or deleting the item from the list.

start
1024

03 1006 25 1008 35 1002 45 0

header node
(helps in getting list count without traversing)

Manoj Kataria BKBIET, pilani

You might also like