Data Structures - UNIT 2
Data Structures - UNIT 2
Chapter 2
Linked List
Linked list is a linear collection of data elements called nodes. Each node is divided into 2
fields. The first part is for info or data field and second part for pointer or link field to the
next node. The general representation is
INFO LINK
Where INFO is used to store the data value, LINK is used to store the address of next node.
Example:
START
5 8 3 NULL
Here START is an external pointer it stores the address of the first node. NULL indicates it is
the end of linked list.
Advantages of Linked List
struct node
{
int info;
struct node *link;
};
It is a self-referential structure in C. Here link is the pointer that points the next data of same
structure.
Types of Linked List
Linked List are classified into 5 types:
INFO LINK
The second step get the data item and place in INFO field
START
50
INFO LINK
The third step place NULL value in Link fields
START
50 NULL
INFO LINK
The fourth step will return to main program.
Traversal
The process of visiting all nodes in Linked List is known as traversing. The following algorithm
traverse the Linked List
Algorithm
1. Is list is empty?
If(START==NULL)
Display “Linked list is empty”
Return
End if
2. Assign start value to CURPTR
CURPTR=start
3. Repeat while (CURPTR != NULL)
Print CURPTR→INFO
CURPTR=CURPTR→LINK
4. Return
Example
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL
Insertion
This operation is used to insert a new node in the Linked List. The new node can be inserted in
3 ways:
1) Insert at the beginning of a Linked List
2) Insert at the end of a Linked List
3) Insert at the specified position in Linked List
1) Insert at the beginning of a Linked List
The following algorithm insert a node at the beginning.
Algorithm:
1. Create a node using malloc
NEWNODE=(NODE*) malloc(sizeof (NODE))
2. Store the data
NEWNODE→INFO=ITEM
3. Assign the start value to the link of NEWNODE
NEWNODE→LINK=start
4. Assign NEWNODE as START
Start=NEWNODE
5. Exit
Explanation
Consider the following linked list contain 3 nodes
START
2010 2010 215 1003
10 90 24 NULL
2025
NEWNODE=2025
The second step stores data in INFO field
2025
50
The third step assign NEWNODE link to START
2025
50 2010
START
2010 2010 215 1003
10 90 24 NULL
The following code illustrates inserting a node in the beginning of the Linked List
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
The first step check START is NULL, if it is NULL it call Insert at beginning otherwise
it assign Start value to CURPTR
The third step is traverse to point the last node
START
2010 2010 215 1003
10 90 24 NULL
2025
NEWNODE=2025
The fifth step stores data in INFO field
2025
50
The sixth step assign NEWNODE link to CURPTR → LINK and NEWNODE → LINK
= NULL. The pictorial representation is
START
2010 2010 215 1003 2025
10 90 24 50 NULL
The following code illustrates inserting a node at the end of the Linked List
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space\n");
return;
}
printf("\nEnter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}
2025
NEWNODE=2025
In fifth step it assigns INFO Value to NEWNODE
2025
50
In sixth step it creates link between CURPTR and NEWNODE. Consider pos is 2.The
pictorial representation is as follows.
START
2010 2010 2025 215 1003
10 50 90 24 NULL
The following code illustrates inserting a node at a specific position of the Linked List
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\nPosition not found\n");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
DELETION
This operation is used to delete a node from the Linked List. A node can be deleted using three
ways:
1) Delete a node from the beginning of a Linked List
2) Delete a node from end of a Linked List
3) Delete a node from a specified position in Linked List
1) Delete a node from the beginning of a Linked List
This operation deletes a node from the beginning of Linked List.
Algorithm
1. Is List is Empty?
if (START= = NULL)
print “List is Empty”
else
go to step 2
2. Assign Start value to CURPTR
CURPTR = Start
3. Assign Link field of CURPTR to Start
The first step check Start is NULL. If it is print list is empty otherwise step2
In second step assign CURPTR by Start (Here Start = 2002 therefore, CURPTR= 2002)
In third step set the CURPTR → LINK to Start
In fourth step free the CURPTR
The pictorial representation after deletion is
START
2015 2015 198
58 24 NULL
The following code illustrates delete a node from the beginning of the Linked List
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty\n");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is : %d",ptr->info);
free(ptr);
}
}
The first step check Start is NULL. If it is print list is empty otherwise step2
In second step it check is list contain one element if it is set Start is NULL otherwise
step4
In third step delete start
In fourth step Assign start to CURPTR
In fifth step set PREPTR to NULL
In sixth step traverse the list until CURPTR points the last node.
In seventh step delete CURPTR
In eight step set PREPTR → LINK as NULL
START
2002 2002 2015
10 58 NULL
The following code illustrates delete a node from end of the Linked List
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nList is Empty\n");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is: %d",ptr->info);
free(ptr);
}
}
Explanation
Consider a Linked List with 3 nodes are as shown below:
START
2002 2002 2015 198
10 58 24 NULL
The first step check POS= = 1 if it call delete_beginning otherwise goto step2
In second step Assign start to CURPTR
In third step set PREPTR to NULL
In fourth step CURPTR as specified position using loop.
In fifth step set link between PREPTR and next node of CURPTR. Then delete the
CURPTR.
The following code illustrates delete a node from a specific position of the Linked List
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\nThe List is Empty\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\nThe deleted element is: %d",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++)
{
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL)
{
printf("\nPosition not Found\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d",ptr->info );
free(ptr);
}
}
}
Length
This operation is used to find the number of nodes in Linked List
Algorithm
1. Initialize len=0
2. If (Start = = NULL)
{
Print “List is Empty”
Return
}
3. Set CURPTR=Start
4. while(CURPTR != NULL)
{
len++
CURPTR= CURPTR → LINK
}
5. return len
Explanation
Consider a Linked List with the following nodes:
START
2002 2002 2015 198
10 58 24 NULL
In the first step it initializes the variable len as 0
In the second step it checks whether the Start is NULL , Here Start =2002
In the third step it assigns the Start value to CURPTR that is CURPTR =2002
In the fourth step it checks CURPTR is NULL if it is not then it increment Len value by 1
and set CURPTR= CURPTR → LINK. Repeat until CURPTR is NULL
In fifth step it returns len value , Here len=3
Searching
In circular linked list, only one pointer is used for insertion and deletion.
Circular linked list contains the following operations
i. Creation
ii. Insertion
iii. Deletion
iv. Traversal
v. Searching
vi. Sorting
The following program creates a node in circular linked list and display its data.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *last=NULL;
void create_clist()
{
char ch;
int i=0;
NODE *NEWNODE;
NEWNODE=(NODE*)malloc(sizeof(NODE));
last=NEWNODE;
NEWNODE →LINK=last;
while(1)
{
printf("\n Enter the node %d",i+1);
scanf("%d",&NEWNODE→INFO);
printf("Do you wish to add one more node (Y/N): \n");
ch=getche();
if(toupper(ch)=='Y')
{
NEWNODE=(NODE*) malloc(sizeof(NODE));
NEWNODE→LINK=last→LINK;
last→LINK=NEWNODE;
last=NEWNODE;
i++;
}
else
break;
}
}
void display()
{
NODE *CURPTR;
if(last= = NULL)
{
printf("List is Empty \n");
return;
}
CURPTR=last→LINK;
printf("\n");
while(CURPTR!=last)
{
printf("%d",CURPTR→INFO);
printf("→");
CURPTR=CURPTR→LINK;
}
printf("%d",last→INFO);
}
void main()
{
clrscr();
printf("\nCircular Linked List Demonstration\n");
create_clist();
display();
getch();
}
OUTPUT
Circular Linked List Demonstration
Enter the node 1
50
Do you wish to add one more node (Y/N): Y
Enter the node 2
45
Do you wish to add one more node (Y/N): Y
Enter the node 3
67
Do you wish to add one more node (Y/N): N
50-->45-->67
• INFO
• BACK
• FORW
The INFO field stores the data and the BACK field stores the address of the previous node and
the FORW field stores the address of the next node.
The node in doubly linked list is represented below:
NODE *start=NULL;
void create_dlist()
{
char ch;
int i=0;
NODE *NEWNODE, *CURPTR;
CURPTR=(NODE*)malloc(sizeof(NODE));
start=CURPTR;
CURPTR →FORW=NULL;
CURPTR →BACK=NULL;
while(1)
{
printf("\n Enter the node %d",i+1);
scanf("%d",&CURPTR→INFO);
printf("Do you wish to add one more node (Y/N): \n");
ch=getche();
if(toupper(ch)=='Y')
{
NEWNODE=(NODE*) malloc(sizeof(NODE));
CURPTR→FORW=NEWNODE;
NEWNODE→BACK=CURPTR;
NEWNODE→FORW=NULL;
CURPTR=NEWNODE;
}
else
{
CURPTR →FORW=NULL;
break;
}
i++;
}
}
void display()
{
NODE *CURPTR =start;
if(start= = NULL)
printf("List is Empty \n");
else
{
printf("\n");
while(CURPTR!=NULL)
{
printf("%d",CURPTR→INFO);
printf("→");
CURPTR=CURPTR→FORW;
}
printf("NULL");
}
}
void main()
{
clrscr();
printf("\nDoubly Linked List Demonstration\n");
create_dlist();
display();
getch();
}
OUTPUT
Doubly Linked List Demonstration
Enter the node 1
50
Do you wish to add one more node (Y/N): Y
Enter the node 2
45
Do you wish to add one more node (Y/N): Y
Enter the node 3
67
Do you wish to add one more node (Y/N): N
50→45→67→NULL
Advantages of Doubly Linked List
1. It can be traversed in forward and backward direction
2. Insertion and deletion are easy because every node has forward and backward link
3. It is used to represent the trees effectively
4. This simplifies list management
Disadvantages of Doubly Linked List
1. Extra memory is required to store the back pointer.
2. Extra effort is required to deal with the extra link.
Header Linked List
• A special node in the beginning of the list is known as header node.
• This node does not contain actual data but contains useful information about the entire
linked list such as total number of nodes, pointer to the last node etc.
• The start points to the header node in the header linked list.
• Link field of header node points the first node in the list.
• The header linked list will always contain a header node even if list is empty.
Header linked list are of two types:
STACK
Definition
Stack is a collection of homogeneous data items where the insertion and deletion operations
take place at one end called TOP of the stack. Stack works on LIFO (Last In First Out) Fashion
5 TOP
8
1
StackADT
A set of stack data member and a set of stack operations that perform on stack data. The
stack data members are TOP, MAXSTK. The stack operations such as
1. Createstack()- Create stack with n elements.
2. push()- push an element on to the TOP of the stack
3. pop()- POP an element from the TOP of the stack.
4. Isempty()- returns true if the stack is empty otherwise false
5. Traverse()- accessing all the elements in the stack.
6. Count()- count the number of elements in the stack.
PUSH operation
It is used to insert an element on the TOP of the stack. If stack is full, PUSH operation is not
possible.
Algorithm: PUSH(S,TOP, MAXSTK, item)
1. check Stack is overflow
if TOP= = MAXSTK - 1 then
print "Stack Overflow"
return
2. Increment TOP value
TOP=TOP+1
3. Insert the item at the TOP
S[TOP]=item
4. return
Explanation
Consider the following stack contain 3 element and its MAXSTK value is 5
5 TOP
8
1
TOP
5
8
1
10 TOP
5
8
1
4. return
POP operation
This operation deletes an element from the stack i.e., it deletes the TOP element from the stack.
If stack is empty the pop operation is not possible.
Algorithm: POP(S,TOP, item)
1. Check underflow
if TOP== -1 then
print "stack underflow"
return
2. Delete the item
item=S[TOP]
3. Decrement TOP value
TOP=TOP-1
4. return
Explanation
Consider a stack contain 3 elements and its MAXSTK value is 5.
5 TOP
8
1
1. Check stack is underflow or not, here TOP=2and MAXSTK=5. Therefore, go to step 2
TOP
8
1
8 TOP
1
4. Return
Traverse operation
Accessing all elements in the stack is known as traversal operation.
Algorithm: DISPLAY(S, TOP)
1. Check underflow
if TOP== -1 then
print "stack underflow"
return
2. Access all elements using loop
for i=TOP to 0
print S[i]
end for
3. return
Explanation
Consider a stack contain 3 elements and its MAXSTK value is 5.
5 TOP
8
1
Advantages of Recursion:
1. Reduces the complexity of the problem
2. It allows user to write simple and elegant program
3. Programs are smaller in length
4. Programs can have any no. of nesting levels
5. Top-Down Programming model
Recursion : Example 1 - Finding factorial using Recursive method
1 𝑖𝑓 𝑁 = 0
FACTORIAL(N) = { }
𝑁 ∗ 𝐹𝐴𝐶𝑇𝑂𝑅𝐼𝐴𝐿 (𝑁 − 1) 𝑖𝑓 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Algorithm: FACTORIAL(N)
Step 1: if (N= = 0) then
fact=1
Step 2: else
Fact=N * FACTORIAL (N-1)
End if
Step 3: return(fact)
Step 4: Stop
Consider the following represents 4! Using recursive function
4!=4*3!
3!=3*2!
2!=2*1!
1!=1*0!
1!=1
2!=2
3!=6
4!=24
The whole process is implemented in the stack as shown below
0 𝑖𝑓 𝑁 = 0 𝑜𝑟 𝑁 = 1
Fib(N)={ 1 𝑖𝑓 𝑁 = 2 }
𝑓𝑖𝑏(𝑁 − 1) + 𝑓𝑖𝑏(𝑁 − 2) 𝑖𝑓 𝑁 > 2
Algorithm: fibon(N)
if(N==0) || (N==1) then
fib=0
else if (N==2) then
fib=1
else if (N>2) then
fib=fibon(N-1) +fibon(N-2)
end if
return fib
stop
Algorithm Steps
fibon(6)=fibon(5)+fibon(4)
fibon(5)=fibon(4)+fibon(3)
fibon(4)=fibon(3)+fibon(2)
fibon(3)=fibon(2)+fibon(1)
fibon(2)=1, fibon(1)=0
fibon(3)=1
fibon(4)=2
fibon(5)=
3
fibon(6)=5
Algorithm: move(n, S, T, D)
{
move(n-1, S, D, T) //move (n-1) disks from S to T
move(1, S, T, D) //move 1 disk from S to D
move(n-1, T, S, D) //move (n-1) disks from T to D
}
Consider the Tower of Hanoi has three disks (n=3)
Push()
( (
Push()
( ( (
1 ( (
+ ( (
2 ( (
Pop()
) (
* (
3 (
Pop()
)
Example
Consider the following expression Q=A + (B + C)
To covert infix to postfix push a ‘(‘ on to the stack and add a ‘)’ at the end of the infix
expression
Q= A + (B + C))
Line Scanned Stack Postfix Expression
Symbol
A A
1 (
+ A
2 ( +
3 ( A
( + (
B AB
4 ( + (
+ + AB
5 ( + (
C + ABC
6 ( + (
) ABC+
7 ( +
) ABC++
8
Push Operation
The push operation is used to insert an element into the stack. The new element is added at the
topmost position of the stack.
Algorithm: PUSH()
step 1: Allocate memory for the new node and name it as NEWNODE
step 2: set NEWNODE→ INFO = ITEM
step 3: if TOP= = NULL
set NEWNODE→ LINK = NULL
set TOP= NEWNODE
else
set NEWNODE→LINK= TOP
set TOP= NEWNODE
end if
step 4: End
Consider the linked stack as shown below:
TOP
2025 2025 2010 215
50 10 90 NULL
To insert an element with value 80, we first check if TOP= = NULL, then we allocate memory
for a new node, store the value 80 in its INFO part and NULL in its LINK part. We add the
new node at the beginning of the linked stack.
The updated linked stack is shown below:
TOP
1020 1020 2025 2010 215
80 50 10 90 NULL
Pop operation
The pop operation is used to delete the topmost element from a stack.
Algorithm: POP()
Step 1: If TOP= = NULL
print "UNDERFLOW"
go to step 5
end if
Step 2: set PTR= TOP
Step 3: set TOP= TOP→NEXT
Step 4: free PTR
Step 5: End
Consider the linked stack as shown below:
TOP
2025 2025 2010 215
50 10 90 NULL
Before deleting the values, we first check TOP = = NULL if so, the stack is empty otherwise
it deletes a value from a stack
TOP
2010 2010 215
10 90 NULL
QUEUE
Queue is defined as an ordered collection of items from which, the items may be deleted at one
end called FRONT end and the items may be inserted at the other end called REAR end.
Queue works on FIFO (First In First Out) fashion.
Classification of Queue
Queue
Linear Queue
It is also known as sequential queue. It is implemented using linear array or linear list. This
linear queue is represented using linear array with 2 variables FRONT & REAR.
0 1 2 3 4 5 6 7
26 9 23 34 45 67 45 34
FRONT REAR
Deletion Insertion
Queue ADT
A collection of queue data members and queue operations that preform on queue data. The data
members or variables are FRONT and REAR. Queue operations such as
1. Create()- Create queue with n elements.
2. Insert()- insert an element at the end of the queue
3. Delete()- delete an element from the beginning of the queue.
4. Isempty()- returns true if the queue is empty otherwise false
5. Traverse()- accessing all the elements in the queue.
6. Count()- count the number of elements in the queue.
Insertion()
This operation inserts an element on the end of a queue. If queue is full, insertion is not possible.
Algorithm: Qinsert(Q, FRONT, REAR, N, item)
Step 1: check overflow
if REAR= = N-1 then
print “Overflow”
return
Step 2: Increment REAR pointer
REAR= REAR+1
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check queue overflow condition if it is print overflow. Here, REAR=4 so it is false.
2. Increment REAR=REAR + 1
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
3. Insert the item
Q[REAR]= item i.e., Q[5]=86
0 1 2 3 4 5 6 7
26 9 23 34 45 86
FRONT REAR
4. Return
The following C function illustrates Queue insertion
void Qinsert()
{
if(REAR == N-1)
printf("\n Queue Overflow");
else
{
printf("\n Enter an item:");
scanf("%d", &item);
REAR++;
QUEUE[REAR]= item;
}
}
Deletion
This operation deletes an item from the beginning of the queue. If Queue is Empty, deletion
operation is impossible.
Algorithm: Qdelete(Q, FRONT, REAR, N, item)
Step 1: Check underflow
if REAR= = FRONT-1 then
print "Underflow"
return
Step 2: Delete the item
item = Q[FRONT]
Step 3: if FRONT= = REAR
FRONT=0, REAR=-1
else
FRONT=FRONT+1
`Step 4: return
Explanation
Consider a queue contain 5 elements and Max is 8
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check queue is underflow. Here, FRONT=0 and REAR=4, so it is false
2. Delete the item. i.e., item= Q [0]⟹item =26
0 1 2 3 4 5 6 7
9 23 34 45
FRONT REAR
3. Check FRONT and REAR value is equal. Here FRONT=0 and REAR =4. So, it is
false, increment FRONT=FRONT+1
0 1 2 3 4 5 6 7
9 23 34 45
FRONT REAR
4. Return
The following C function illustrates Queue deletion
void Qdelete()
{
if (REAR= = FRONT - 1)
printf("\nQueue Underflow");
else if(REAR= = FRONT)
{
printf("This is the last element in the Queue");
printf("\n The Last element deleted is : %d", QUEUE[FRONT]);
FRONT=0;
REAR=-1;
}
else
{
printf("\n Deleted item is %d", QUEUE[FRONT]);
FRONT++;
}
}
Qempty Operation
This operation returns true if the queue is empty otherwise false.
A Queue is empty only when REAR=-1 and FRONT=0
The following function check Queue is empty
int Qempty()
{
if(REAR= = FRONT-1)
return 1;
else
return 0;
}
Qfull Operation
This operation returns true, if the queue is full otherwise false. A Queue is full only when
REAR== N-1. The following function check Queue is full.
int Qfull()
{
if(REAR= = N-1)
return 1;
else
return 0;
}
Traversal operation
This operation access all the elements in the queue.
Algorithm: Qdisplay(Q, FRONT, REAR, N)
Step 1: Check underflow
if REAR== FRONT-1 then
print "Underflow"
return
Step 2: Repeatedly access the queue elements
for (i=FRONT; i<= REAR; i++)
print Q[i];
Step 3: return
Explanation
Consider the following Queue Max Size is 8 and it contain 5 elements.
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check underflow, here REAR=4, FRONT=0 so it is false therefore Go to step 2
2. Repeatedly print the value of Queue 26 9 23 34 45
3. Return
Linked Representation of Queues
In linked list implementation, every queue element has two parts, one that stores the
data and the other that stores the address of the next element.
The FRONT pointer points the first element and the REAR pointer points the last
element.
All insertions will be done at the REAR end and all insertions at the FRONT end
FRONT = REAR = NULL indicates the queue is empty
Insert Operation
The insert operation is used to insert an element into a queue. The new element is added as
the last element of the queue.
Algorithm: QINSERT()
Step 1: Allocate memory for the new node and name it as NEW_NODE
Step 2: Set NEW_NODE → INFO=item
Step 3: If FRONT = NULL
Set FRONT=REAR=NEW_NODE
Set FRONT→ LINK = REAR→ LINK =NULL
Else
Set REAR→> LINK=NEW_NODE
Set REAR = NEW_NODE
Set REAR→NEXT=NULL
End if
Step 4: END
Consider the following figure shows linked queue.
FRONT REAR
Delete Operation
The delete operation is used to delete the FRONT element from the queue.
Algorithm QDELETE()
Step 1: If FRONT = NULL
Print "Underflow"
goto Step 5
Step 2: Set PTR = FRONT
Step 3: Set FRONT = FRONT → LINK
Step 4: Free PTR
Step 5 End
Consider the linked Queue as shown below:
FRONT REAR
Before deleting the values, we first check FRONT = = NULL if so, the queue is empty
otherwise it deletes a value from a queue.
FRONT REAR
CIRCULAR QUEUE
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle. It is also called ‘Ring Buffer’. The representation of circular queue is shown below:
Insert Operation
This operation inserts an element on the REAR position of a queue. If queue is full, insertion
is not possible.
Algorithm CQINSERT(Q,FRONT,REAR,N,ITEM)
Step 1: If FRONT == (REAR+1) %N
display “overflow”
Step 2: IF FRONT == -1
FRONT = REAR = 0
Step 3: ELSE REAR = (REAR + 1)%N
Step 4: Q[REAR] = ITEM
Step 5: END
Delete Operation
This operation deletes an item from the FRONT position of the queue. If Queue is Empty,
deletion operation is impossible.
Algorithm QDELETE (Q, FRONT, REAR, N, ITEM)
Step 1: If FRONT == -1
Write “underflow”
Return
Step 2: ITEM = Q[FRONT]
Step 3: IF FRONT = REAR
FRONT = REAR = -1
ELSE
FRONT = (FRONT + 1)%N
Step 4 : END
The following figure illustrates insertion and deletion of a queue
PRIORITY QUEUE
A priority queue is a queue such that element of the queue has been assigned with a
priority such that the order in which elements are processed comes from the following
rules:
o An element of higher priority is processed before any element of lower
priority
o If two elements have same priority, then the element which come first will be
processed first.
Types of priority queue
There are two types of priority queue. They are:
Priority Queue
In Deque or Double Ended Queue, insertion and deletion can be done from both ends of
the queue either from the front or rear.
Types of DEQUE
The 2 variations of deque is as follows:
Operations on Deque:
The basic operations are:
insertFront(): Adds an item at the front of Deque
insertLast(): Adds an item at the rear of Deque
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
Consider an input restricted deque and now we can add element only on the REAR side of the
queue but we can delete from both sides.
Applications of Queues:
Queues are used as waiting lists for a single shared resource like printer, disk, CPU.
Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
Queue are used to maintain the play list in media players in order to add and remove
the songs from the play-list.
Queues are used in operating systems for handling interrupts.