0% found this document useful (0 votes)
40 views35 pages

Understanding Queue Data Structures

Uploaded by

Aakarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views35 pages

Understanding Queue Data Structures

Uploaded by

Aakarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Queue

Queue
• Queue is Linear Data Structure
• It follows First In First Out(FIFO) principal
•It has two pointers front and rear
e.g.:
Deletion Insertion
(DEQUEUE) (ENQUEUE)
10 20 30 40 50
FRONT A[0] A[1] A[2] A[3] A[4] REAR)
Queue
The queue can be implemented into two
ways:
◦ Using arrays (Static implementation)
◦ Using pointer (Dynamic implementation)

49
Memory Representation of a queue
using array

4
Operations on Queue
Insertion:
Algorithm:

Step 1: If REAR = MAX – 1 then


Write “Queue is Overflow”
Goto step 4
[End of IF]
Step 2: IF FRONT=-1 and REAR=-1
SET FRONT=REAR=0
ELSE
SET
REAR=REAR+1 [END
OF IF]
Step 3: SET QUEUE [REAR] = NUM
Step 4: EXIT
Example of Insertion in
Queue
A[0] A[1] A[2] A[3] A[4]
F=R=(-1)

10
A[0] A[1] A[2] A[3] A[4]

F=R=0

10 20
A[0] A[1] A[2] A[3] A[4]

F=0 R=1
Operations on Queue
Deletion:
Algorithm:

Step 1: IF FRONT = -1 OR FRONT>REAR


Write “Queue is Underflow”
ELSE
SET VAL=QUEUE [FRONT]
FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT
Example of Deletion in
Queue
10 20
A[0] A[1] A[2] A[3] A[4]

F=0 R=1
20
A[0] A[1] A[2] A[3] A[4]

F=R=1
Types Of Queue

1. Circular Queue
2. Priority Queue
3. Deque
4. Multiple Queue
Circular Queue
Why Circular Queue is
needed?
• Problem:
– Wastage of memory in standard queue in DEQUEUE
operation
What is Circular
Queue?
• The Arrangement of the elements Q[0], Q[1],...,Q[n] in a
circular fashion with Q[1] following Q[n] is called Circular
Queue.
• The last node is connected to first node to
make a circle.
• Initially, Both Front and Rear pointers points to the beginning
of the array.
• It is also known as “Ring Buffer”.
• e.g.:

Q[6] Q[7]

Q[0] F=R=Q[0]
Q[5]

Q[4]
Q[1]

Q[3] Q[2]
Operations on Circular
Queue
Insertion:
Algorithm:

Step 1: IF FRONT=0 and REAR=MAX-1 Then


Write “Overflow”
Goto Step 4
[END OF IF]
Step 2: IF FRONT = REAR=-1 then
SET FRONT=REAR=0
ELSE IF REAR=MAX-1 and FRONT!=0
SET REAR=0
ELSE
SET REAR=REAR+1
[END OF IF]
Step 3: SET QUEUE[REAR]=VAL
Step 4: EXIT
e.g.:

Q[6] Q[7] R=Q[7]


35 40

Q[5] 30 Q[0] F=Q[0]

25 10
Q[4]
Q[1]

20 15
Q[3] Q[2]

Queue is full(Overflow)
e.g.: (cond..)

Q[6] Q[7]

Q[5] 2 Q[0] F=R=Q[0]

Q[4]
Q[1]

Q[3] Q[2]

If F=R=-1 then F=R=0


e.g.: (cond..)

Q[5] Q[6]
35 40

R=Q[7]
Q[4] 30 45 Q[7]

25
Q[3]
Q[0]

20 15
Q[2] Q[1] F=Q[1]

If REAR=MAX-1 and FRONT!=0


e.g.: (cond..)

Q[6] Q[7]

Q[5] 5 Q[0] F=Q[0]

10
Q[4]
Q[1] R=Q[1]

Q[3] Q[2]

Rear=Rear+1
Operations on Circular
Queue
Deletion:
Algorithm:
Step 1: If FRONT = -1 then
Write ("Circular Queue Underflow“)
GOTO Step 4
Step 2: SET VAL=QUEUE[FRONT]
Step 3: If FRONT = REAR then
SET FRONT=REAR=-1
ELSE
IF FRONT=MAX-1
SET FRONT=0
ELSE
SET FRONT=FRONT+1
[END OF IF]
[END OF IF]
Step 4: EXIT
e.g.:

Q[6] Q[7]

Q[5] Q[0] F=R=-1

Q[4]
Q[1]

Q[3] Q[2]

Queue is Empty(Underflow)
e.g.:

Q[6] Q[7]

Q[5] 5 Q[0] F=R=Q[0]

Q[4]
Q[1]

Q[3] Q[2]

If F=R=0 then F=R=-1


e.g.: (cond..)

Q[5] Q[6]
35 40

F=Q[7]
Q[4] 30 45 Q[7]

25
Q[3]
Q[0]

20 15
Q[2] Q[1] R=Q[1
]
If Front=MAX-1 then Front=0
e.g.: (cond..)

Q[7] Q[8]
35 40

R=Q[1]
Q[6] 30 45 Q[1]

25
Q[5]
Q[2]

20

F=Q[4] Q[4] Q[3]

Front=Front+1
Application of Queue
• Simulation
• Various features of Operating system
• Multi-programming platform systems.
• Different types of scheduling algorithms
• Round robin technique algorithms
• Printer server routines
• Various application software’s is also
based on queue data structure.

24
Priority Queue
Priority Queue
• In priority queue, each element is assigned a
priority.
• Priority of an element determines the order in
which the elements will be processed.
• Rules:
1. An element with higher priority will processed
before an element with a lower priority.
2. Two elements with the same priority are
processed on a First Come First Serve basis.
Types of Priority Queue
1. Ascending Priority Queue
In this type of priority queue, elements can be
inserted into any order but only the smallest
element can be removed.

2. Descending Priority Queue


In this type of priority queue, elements can be
inserted into any order but only the largest element
can be removed.
Array Representation of Priority
Queue
Insertion Operation:
• While inserting elements in priority queue we
will add it at the appropriate position
depending on its priority
• It is inserted in such a way that the elements
are always ordered either in Ascending or
descending sequence
Array Representation of Priority
Queue
15 10
A[0] A[1] A[2] A[3] A[4]

Front Rear

20 15 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
25 15 13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
Array Representation of Priority
Queue
Deletion Operation:
• While deletion, the element at the front is
always deleted.
Array Representation of Priority
Queue
20 15 13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear

15 13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
13 10
A[0] A[1] A[2] A[3] A[4]

Front Rear
Double Ended Queue
Deque / Double Ended Queue
• A list in which elements can be inserted or
deleted either end
• It is also known as “Head-Tail Linked List”
• It has two pointers LEFT and RIGHT, which
point to either end of the deque.
• Dequeue can be implemented using Circular
Array or a Circular doubly linked list where
Dequeue[n-1] is followed by Dequeue[0]
Types of Deque
• Input Restricted Deque:-
In this dequeue, insertion can be done only at one of
the end, while deletion can be done from both ends.

• Output Restricted Deque:-


In this dequeue, deletion can be done only at
one of the ends, while insertions can be done
on both ends
Basic Deque Operations
The following are the basic operations that can be
performed on deque.

•insertfront: Insert or add an item at the front of the deque.


•InsertLast: Insert or add an item at the rear of the deque.
•deleteFront: Delete or remove the item from the front of the
queue.
•delete last: Delete or remove the item from the rear of the
queue.
•getFront: Retrieves the front item in the deque.
•getLast: Retrieves the last item in the queue.
•isEmpty: Checks if the deque is empty.
•isFull: Checks if the deque is full

You might also like