Professional Documents
Culture Documents
Queues
• A queue is a linear data structure.
• A queue is an ordered list in which all insertion take place at one end, the
rear, whereas all deletions take place at the other end, the front.
• Works on principle first in first out (FIFO).
• It means: the first element inserted is the first one to be removed.
• Adding an item
• Removing an item
Deletion Insertion
front Rear
• Real life examples
– Waiting in line
– Waiting on hold for tech support
• Applications related to Computer Science
– Threads
– Job scheduling (e.g. Round-Robin algorithm for CPU allocation)
Implementation of Queue
• Queue can be implemented based on
–Array (static implementation)
–Linked list (dynamic implementation)
Add(23)
Increment rear by 1 and add 23 at rear end
23
-1 0 1 2 3 4
0 1 2 3 4
q1.rear
q1.rear q1.front
q1.front
36 64 64
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
99 99 5
0 1 2 3 4 0 1 2 3 4
q1.front q1.front
q1.rear q1.rear
Increment rear by 1 and add 99 Increment rear by 1 and add 5 at rear end
at rear end
Add(75)
But rear is Queue full condition is q1.rear=MAX-1
pointing to last
location, so
cannot add
new element.
Array-based Queue Implementation
Algorithm :
1.Define structure containing array for queue and two variables
as rear & front.
2.Initialize rear to -1 and front to 0.
3.Perform add (enqueue) operation,
Before adding data to queue , check queue is full or not
as rear==MAX-1
If full, print as queue is full.
else
Enter the data to be added from user.
Increment the counter i.e. rear struct queue
add that data to queue from the rear side. {
int a[size];
int rear, front;
}q1;
Add Operation & Queue full condition using
function
void add()
{
int n,i,x;
if(full()==1)
printf("\n\tTHE QUEUE IS FULL");
int full()
else {
{ if(q1.rear==MAX-1)
printf("\n\tENTER THE ELEMENT: "); return 1;
scanf("%d",&x);
else
q1.rear++;
q1.a[q1.rear]=x;
return 0;
}
}
}
Array-based Queue Implementation
Algorithm :
}
}
Display Operation
void display()
{
int i;
if(empty()==1)
printf("\n\tTHE QUEUE IS EMPTY!");
else
{
for(i=q1.front;i<=q1.rear;i++)
{
printf("\t%d",q1.a[i]);
}
}
}
Linked list-based Queue Implementation
Add (10)
Create the node using malloc function,
p
100
Enter the data as 10 in number field, as
p->number=10 and make next field as NULL and this is first node
in queue so front and rear must point to p as front=rear=p;
10 NULL
p
100
front
rear
Linked list-based Queue Implementation
add(30)
Create the node using malloc function,
p
150
Enter the data as 30 in number field, as
p->number=30 and make next field as NULL
p
10 30 NULL
front
100 150
rear
Linked list-based Queue Implementation
add(45)
Create the node using malloc function,
p
150
Enter the data as 45 in number field, as
p->number=45 and make next field as NULL
45 NULL
p
350
front 45 NULL
30
150 350
rear
Linked list-based queue Implementation continue…..
1.Perform deletion operation,
a. initialize one variable as int x.
b. initialize one structure variable as q. and make q=front
c. check for queue is empty or not. i.e. front==NULL or not.
d. if not,
take data of front in the variable x, x=front->number;
move variable front to next node as front=front->next
free q node.
print which data is removed from the queue.
Queue empty
front==rear+1 front == NULL
condition
Note:
Note that the container of items is an array. Array is stored in main memory.
Main memory is linear. So this circularity is only logical.
There can not be physical circularity in main memory.
60
add 20
Circular Queue
⚫ Formula for circular Queue:
⚫ i=(i+1)% MAX
⚫ Circular Queue Full condition:
int full()
{
if( (q1.rear+1)%MAX==q1.front )
return 1;
else
return 0;
}
Circular Queue-Add function
void add(int x)
{
if(empty()==1)
{ q1.rear=q1.front=0; // rear & front will point to same element
q1.data[q1.rear] = x;
}
else
{ q1.rear=(q1.rear+1)%MAX ;
q1.data[q1.rear] = x;
}
}
Circular Queue-Delete function
int del()
{
int x;
x = q1.data[q1.front] ;
if(q1.rear==q1.front)
{ q1.rear=-1; q1.front=0;
}
else
{ q1.front=(q1.front+1)%MAX ;
}
return x;
}
Circular Queue-Display
void display()
{
int i;
i=q1.front;
while( i ! =q1.rear)
{ printf(“\n %d”,q1.data[i]);
i=(i+1)%MAX ;
}
printf(“\n %d”, q1.data[q1.rear]);
}
Priority Queue
Is an ordered list of Homogeneous elements.
Priority based on the urgency of need.
1. An element of higher priority is processed before any element
of lower priority.
2. Two elements with the same priority are processed according
to the order in which they were added to the queue.
◼ Types of priority queue :
(i) Ascending priority queue
(ii) Descending priority queue.