Professional Documents
Culture Documents
Delete an element
FRONT: 2 BB CC DD …
REAR: 4 1 2 3 4 5 6 7 N
Insert an element
FRONT: 2 BB CC DD EE …
REAR: 5
1 2 3 4 5 6 7 N
FRONT: 7
XX … ZZ
REAR: N
1 2 3 4 5 6 7 N
FRONT: N
AA BB CC DD EE FF XX … ZZ
REAR: 7
1 2 3 4 5 6 7 N
FRONT: 7
XX …
REAR: 7
1 2 3 4 5 6 7 N
Algorithm to Insert in Q
[1] If FRONT = REAR % N + 1 then
Print: Overflow and Exit
[2] [Empty Queue]
If REAR = 0 then
Set FRONT = 1 and REAR = 1
Else Set REAR = REAR % N + 1
[3] Set QUEUE[REAR] = ITEM
[4] Exit
Queue
FRONT: 7
AA BB CC DD EE FF XX … ZZ
REAR: 6
1 2 3 4 5 6 7 N
REAR
FRONT
AA BB CC X
15
Insertion in a Queue
REAR
FRONT
AA BB CC X
NEW
DD
16
Insertion in a Queue
FRONT
AA BB CC
DD X
REAR
17
Delete from a Queue
REAR
FRONT
AA BB CC X
18
Delete from a Queue
REAR
FRONT
BB CC X
19
Linked Queue
LEFT: 4 AA BB CC DD
RIGHT: 7
1 2 3 4 5 6 7 8
Variation of deque
• There are two variation of deque
[1] Input-restricted queue: Deque which
allows insertions at only one end of the list
but allows deletion at both ends of the list
LEFT: 2 A C D F
RIGHT: 5 1 2 3 4 5 6
Deque
LEFT: 2
A C D F
RIGHT: 5
1 2 3 4 5 6
Two Letters on right is deleted
LEFT: 2 A C
RIGHT: 3 1 2 3 4 5 6
Deque
LEFT: 2
A C
RIGHT: 3
1 2 3 4 5 6
K, L and M are added to the Left
LEFT: 5 K A C M L
RIGHT: 3 1 2 3 4 5 6
Priority Queue
• A priority queue is a collection of
elements such that each element has been
assigned a priority and that the order in
which the elements are deleted and
processed comes from the following rules:
[1] Elements of higher priority is processed
before any elements of lower priority
[2] Two elements with the same priority are
processed according to the order in which
they were added to the queue
Priority Queue
• There are different ways a priority
queue can be represented such as
Head
A1 B2 F2 D3
32
Insertion and Deletion
• Deletion : Delete the first node in the list.
• Insertion: Find the location of Insertion
[3] Exit
Insertion Algorithm [outline]
Insert an element with priority M
[1] Insert ITEM as the rear element in
row M of QUEUE
[2] Exit