2.2 Queue Uing Array & Linked List
2.2 Queue Uing Array & Linked List
-Array
-Linked List
0 1 2 3 4 5 . . . SIZE-1
f = r = -1
f = front
Queue is EMPTY
r = rear
Insertion
enQueue( int element )
{
if( rear != size-1)
{
Q re[arrea+r+]; =
}
elseelement; Queue is full !!!
printf(“Queue is full !!!”);
}
0 1 2 3 4 5 6 7 8 9
Q 10 20 30
40
r
Deletion
deQueue( )
{
if( front != rear )
{
pfront +
rintf(“\nElement removed :
}
else
Queue is empty !!!
+;%d”,Q[front]);
printf(“Queue is empty !!!)”;
}
0 1 2 3 4 5 6 7 8 9
Q 10 20 30 40
f r
Display
display( )
{
if( front != rear )
{
for(int i=front+1;
i<=rear, i++)
printf(“%d\t”,Q[i]);
}
else
printf(“Queue is
empty0 !!!”);
1 2 3 4 5 6 7 8 9
}Q 10 20 30 40
Enqueue Operation
Queues maintain two data pointers, front and rear
The following steps should be taken to enqueue insert data into a
queue Step 1 − Check if the queue is full
}
else { // Otherwise Increment only Front pointer
X = Q [ Front ];
Front = Front + 1 ;
} }
QUEUE – isFull and isEmpty
Implementing Queue using single dimension array, check for the rear
pointer to reach at
MAXSIZE-1 to determine that the queue is full
Algorithm - isFull Algorithm - isEmpty
begin procedure isFull begin procedure isEmpty
NULL ELSE
SET REAR->NEXT =
PTR SET
• In step 3, we check if the new node is the 1st node of the linked queue.
• If this is the case, then the new node is tagged as FRONT as well as
REAR.
• Also NULL is stored in the NEXT part of the node(which is also the
FRONT & the REAR node).
• However, if the new node is not the 1st node in the list, then it is added at the
REAR end of the linked queue (or the last node of the queue).
Delete Operation
Step 1: IF FRONT=NULL
Write
“Underflow”
Goto step 5
Step 5: END
(contd)…
• In step 1, we 1st check for the “underflow” condition.
• If the condition is true, then an appropriate message is displayed, otherwise we use
a pointer PTR that points to FRONT.
• In step 4, the memory occupied by PTR is given back to the free pool.
Queue using Linked List
Applications of
❖
Queue
Serving requests on a single shared valuable resources like a
printer, CPU task scheduling etc.
❖ In real life, Call Center phone systems will use Queues, to hold
people calling them in an order, until a service representative is
free
❖ Handling of interrupts in real-time systems. The interrupts
are handled in the same order as they arrive, First come first
served
❖ Batch processing in operating system