2.4 Queue Uing Array & Linked List.
2.4 Queue Uing Array & Linked List.
Presentatio
U19CS201- DATA STRUCTURES
MODULE 3 STACKS &
n QUEUES
G.G.SREEJA, AP/CCE
QUEUE ADT
-Array
-Linked List
S
d efining the queue, utilizing it, and then completely erasing it
from the memory
□ enqueue − add : store an item to the queue [ REAR END]
□ dequeue − remove : access an item from the queue [ FRONT END ]
□ peek − Gets the element at the front of the queue without removing it
□ IsFull − Checks if the queue is full [ REAR equals ARRAYSIZE-1 ]
□ IsEmpty − Checks if the queue is empty [ FRONT and REAR equals -1 ]
Implementation
#define size 10
void main()
{
int Q[size], front = -1, rear = -1,op,element;
void enQueue(int);
void deQueue();
void display();
printf(“Select your option: ”);
printf(“1: Insert\n2: Delete\n3:
Display\n”); scanf(“%d”,&op);
switch(op)
{
case 1: printf(“Enter element to be insert:
”); scanf(“%d”,&element);
enQueue(element);
break;
case 2: deQueue();
break;
case 3: display();
break;
When the compiler executes above code it allocates memory as
follows….
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)
{
re a r + + ;
} Q [ r ea r ] =
elseelement; Queue is full !!!
printf(“Queue is full !!!”);
}
0 1 2 3 4 5 6 7 8 9
Q 10 20 30
r 40
Deletion
deQueue( )
{
if( front != rear )
{
front
r
} p intf(“\nElement removed
++;Queue is empty !!!
%:
else 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 empty !!!”);
0 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
}
}
Dequeue Operation
Accessing data from the queue
The following steps are taken to perform dequeue operation
}
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 isEmpty
begin procedure isFull
if front and rear equals -1
if rear equals to MAXSIZE-1
return true return true
else else
return false return false
endif endif
• All insertions will be done at the rear end and all the deletions will be done at the
front end.
• Apart from this, there is another operation “peek” which returns the value of the 1st
element of the queue.
Insert Operation
Step 1: Allocate memory from the new node and name it as
PTR
NULL ELSE
SET REAR->NEXT =
NEXT =
• 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
• 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