CSE 203-July2021-Queue-I
CSE 203-July2021-Queue-I
1
Queues
FIFO: First in, First Out
Restricted form of list: Insert at one end,
remove from the other.
Notation:
• Insert: Enqueue
• Delete: Dequeue
• First element: Front
• Last element: Rear
2
Abstract Queue
Also called a first-in–first-out (FIFO) data structure
– Graphically, we may view these operations as follows:
Enqueue
Dequeue
3
Abstract Queue
There are two exceptions associated with
abstract Queue data structure:
– It is an undefined operation to call either pop
or front on an empty queue
4
Queue ADT in C++
C++3elatestP129.pdf
5
Array Based Implementation
• The array-based queue is somewhat tricky to
implement effectively.
• A simple conversion of the array-based list
implementation is not efficient.
• Why?
6
Array Based Implementation
• Assume that there are n elements in the queue and Like our list
implementation, we require that all elements of the queue be stored in
the first n positions of the array. front
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
rear
Impl. 1:
• The rear element of the queue is at position 0,
front
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
rear
front
rear 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
Impl. 1:
• The rear element of the queue is at position 0,
front
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
rear
front
rear 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
Impl. 2:
• The rear element of the queue is at position n-1,
rear
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
front
rear
front 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
Impl. 1:
• The rear element of the queue is at position 0,
rear
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
front
rear
front 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
In this case, the array is not full and yet we cannot place
any more objects in to the array
14
Solution to Drifting Queue Problem:
Implementation (2)
Instead of viewing the array on the range 0, …, 15,
consider the indices being cyclic:
…, 15, 0, 1, …, 15, 0, 1, …, 15, 0, 1, …
This is referred to as a circular array
15
Solution to Drifting Queue Problem:
Implementation (2)
Now, the next push may be performed in the
next available location of the circular array:
16
Implementation
• We allow the queue to continue directly from
the highest-numbered position in the array to
the lowest-numbered position.
• This is easily implemented through use of the
modulus operator (denoted by % in C++).
– positions in the array are numbered from 0 through
size-1,
– and position size-1 is defined to immediately
precede position 0
• (which is equivalent to position size % size).
17
One Subtle Issue front
0 1 2 3 4
12 5
12 5
11 9 8 7 6
rear
11 9 8 7 6 18
rear
A strange proof
front
20
front front
rear
0 1 2 3 4 0 1 2 3 4
rear
12 empty 5 12 First enqueue 5
11 9 8 7 6 11 9 8 7 6
front front
0 1 2 3 4 0 1 2 3 4
11 9 8 7 6 11 9 8 7 6
rear
21
Array implementation of Queue
C++3elatestP132.pdf
22
Linked List Implementation of Queue
• We will use a header link node, which allows for a simpler
implementation of the enqueue operation by avoiding any
special cases when the queue is empty.
• On initialization, the front and rear pointers will point to the
header node, and front will always point to the header node
while rear points to the true last link node in the queue.
• Method enqueue places the new element in a link node at
the end of the linked list (i.e., the node that rear points to)
and then advances rear to point to the new link node.
• Method dequeue removes and returns the first element of
the list.
23
Linked List Implementation of Queue
• There is a small mistake in clear()
• rear = front => rear = front-next
C++3elatestP134.pdf
24