2. Queue
• A queue is a linear list of element in which insertion
can be done at one end which is known as REAR and
deletion can be done which is known as FRONT.
• Operation:
1. Insertion : add a new element in queue
2. Deletion: Removing an element in queue
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
4. Array representation of queues:
• Size of the queue is N = 5;
a) Initially empty :
b) A, B, C inserted :
c) A is deleted:
d) D and then E inserted:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Front: 0
Rear: 0
Front: 1
Rear: 3
Front: 2
Rear: 3
Front: 2
Rear: 5
A B C
B C
B C D E
5. Operations with Queue:
1. enqueue :
– Insertion of the element into the queue
– Insertion always takes place from the rear end.
– Before performing insertion check weather queue is full or
not
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
6. enQueue:
Description:
– Here QUEUE is an array with N locations.
– FRONT and REAR points to the front and rear of the QUEUE.
– ITEM is the value to be inserted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm enqueue(QUEUE, N, FRONT, REAR, ITEM)
{
if (REAR = N) Then //Check for overflow
write ("Overflow");
else
if (FRONT and REAR = 0) Then //Check if QUEUE is empty
{
FRONT := 1;
REAR := 1;
}
else
{
REAR := REAR + 1; //Increment REAR by 1
}
QUEUE[REAR] := ITEM;
}
7. Operations with Queue:
2. dequeue :
– Deletion of the element from the queue
– Deletion always takes place from the front end.
– Before performing deletion check weather queue is empty
or not
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
8. deQueue:
Description:
– Here QUEUE is an array with N locations.
– FRONT and REAR points to the front and rear of the QUEUE.
– ITEM is the value to be deleted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm dequeue(QUEUE, N, FRONT, REAR, ITEM)
{
if (FRONT = 0) Then //Check for underflow
write ("Underflow");
else
{
ITEM := QUEUE[FRONT];
if (FRONT = REAR) Then //Check if only one element is left
{
FRONT := 0;
REAR := 0;
}
else
{
FRONT := FRONT + 1; //Increment FRONT by 1
}
}
}
9. Circular Queue
• Linear Queue:
• Circular Queue:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Front: 4
Rear: 8
A B C D E
A B
C
D
EF
H
G
F G H
Front: 4
Rear: 8
REAR := (REAR + 1) % SIZE
:= (8 + 1) % 8
:= 1
1 2
3
4
56
7
8
FRONT := (FRONT + 1) % SIZE
:= (4 + 1) % 8
:= 5
10. encQueue:
Description:
– Here QUEUE is an array with N locations.
– FRONT and REAR points to the front and rear of the QUEUE.
– ITEM is the value to be inserted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm encqueue(QUEUE, N, FRONT, REAR, ITEM)
{
if (FRONT == (REAR + 1) % N) Then //Check for overflow
write ("Overflow");
else
{
if (FRONT and REAR = 0) Then //Check if QUEUE is empty
{
FRONT := 1;
REAR := 1;
}
else
{
REAR := (REAR + 1) % N; //Increment REAR by 1
}
QUEUE[REAR] := ITEM;
}
}
11. decQueue:
Description:
– Here QUEUE is an array with N locations.
– FRONT and REAR points to the front and rear of the QUEUE.
– ITEM is the value to be deleted.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Algorithm decqueue(QUEUE, N, FRONT, REAR, ITEM)
{
if (FRONT = 0) Then //Check for underflow
write ("Underflow");
else
{
ITEM := QUEUE[FRONT];
if (FRONT = REAR) Then //Check if only one element is left
{
FRONT := 0;
REAR := 0;
}
else
{
FRONT := (FRONT + 1) % N; //Increment FRONT by 1
}
}
}