0% found this document useful (0 votes)
3 views

CSE 203-July2021-Queue-I

The document discusses the concept of queues as a FIFO (First In, First Out) data structure, detailing operations such as enqueue and dequeue. It covers array-based and linked list implementations, highlighting the challenges of array-based queues, including the drifting queue problem and solutions like using a circular array. Additionally, it explains how to manage the front and rear pointers to efficiently handle queue operations.

Uploaded by

rkiriyama5094
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSE 203-July2021-Queue-I

The document discusses the concept of queues as a FIFO (First In, First Out) data structure, detailing operations such as enqueue and dequeue. It covers array-based and linked list implementations, highlighting the challenges of array-based queues, including the drifting queue problem and solutions like using a circular array. Additionally, it explains how to manage the front and rear pointers to efficiently handle queue operations.

Uploaded by

rkiriyama5094
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

CSE 203: Data Structures

Lecture 4: Elementary Data


Structures- Queue

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:

head tail rear

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

– dequeue operations require only O(1) time because the


front element of the queue is the last element in the array.
7
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

– However, enqueue operations will require O(n) time,


because the n elements currently in the queue must each
be shifted back. 8
9
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. rear
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
front

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

– then an enqueue operation is equivalent to an append


operation on a list. This requires only O(1) time.
10
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. rear
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
front

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

– But now, a dequeue operation requires O(n) time,


because all of the elements must be shifted down by one
position. 11
Solution: Queue Implementation (1)

(a) Gives the Queue after 20, 5, 12, 17 are


enqueued initially. (b) Gives Queue after 20 and
5 are dequeued and 3, 30, 4 are enqueued.
12
Performance
• Enqueue: O(1)
• Dequeue: O(1)
• Drifting Queue problem:
– Dequeue => the front index increases.
– Over time, the entire queue will drift toward the higher-
numbered positions in the array.
– Once an element is inserted into the highest-numbered
position in the array, the queue has run out of space.
– This happens despite the fact that there might be free
positions at the low end of the array where elements have
previously been removed from the queue.
13
Drifting Queue Problem
Suppose that:
– The array capacity is 16
– We have performed 16 pushes
– We have performed 5 pops
• The queue size is now 11

– We perform one further push

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:

push => enqueu

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

• How can we recognize when 0 1 2 3 4

the queue is empty or full?


rear 12 5
– front = rear?
• This means one element is front 11 9 8 7 6
there. 0 1 2 3 4
– rear = front -1?
12 5
• Seems a solution.
• What happens when the queue rear
front 11 9 8 7 6
is full?
front 0 1 2 3 4

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

• We can in fact prove that we 0 1 2 3 4

cannot solve the above issue


12 5
by changing the definition of
front and rear rear
11 9 8 7 6
• Assume front is fixed. front

• We can have n values of rear 0 1 2 3 4

• But we can have n+1 possible


12 5
states of the queue
rear
• Pigeon hole principle=> one 11 9 8 7 6
value of rear must be 2 different states for the
mapped to 2 different same value of rear
states!!! 19
Two Solutions
• keep an explicit count of the number of
elements in the queue, or at least a Boolean
variable that indicates whether the queue is
empty or not.
• Another solution is to make the array be of
size n + 1, and only allow n elements to be
stored.
• We use the second one…

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

rear 12 full 5 12 7 enqueues done 5

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

You might also like