9queue
9queue
<Lecture 9: Queue>
Sungmin Cha
New York University
10.02.2024
Outline
• Notice
• Queue
– Queue
– Implementation of Queue
– Application [Solving Programming Problem]
2
Outline
• Notice
• Queue
– Queue
– Implementation of Queue
– Application [Solving Programming Problem]
3
Stack
• The concept and principles of a stack
– Last In First Out (LIFO)
The element most recently ‘pushed’ is the first one to be ‘popped’
Empty Stack
4
Stack
• The concept and principles of a stack
– Last In First Out (LIFO)
The element most recently ‘pushed’ is the first one to be ‘popped’
1 top
12 top 12
1 top
12 top 12 12 top
7
Outline
• Notice
• Queue
– Queue
– Implementation of Queue
– Application [Solving Programming Problem]
8
What is Queue?
• Queue
– A collection of entities that are maintained in a sequence
– The operation of a queue makes it a First-In-First-Out(FIFO)
data structure
9
Queue
• The concept and principles of a queue
– Access to a queue
Front: the element at the beginning of the queue
Tail: the element at the end of the queue
front tail
20 5 15 32
10
Queue
• The concept and principles of a queue
– Enqueue(): add an element to the end of the queue
front tail
2
20 5 15 32
enqueue(2)
11
Queue
• The concept and principles of a queue
– Enqueue(): add an element to the end of the queue
After enqueue(), the tail refers to a new element
front tail
2
20 5 15 32
enqueue(2)
front tail
20 5 15 32 2
12
Queue
• The concept and principles of a queue
– dequeue(): remove an element to the beginning of the queue
front tail
20 20 5 15 32 2
dequeue()
13
Queue
• The concept and principles of a queue
– dequeue(): remove an element to the beginning of the queue
After dequeue(), the front should be changed
front tail
20 20 5 15 32 2
dequeue()
front tail
5 15 32 2
14
Queue
• The concept and principles of a queue
– front(): get an element at the front of the queue
front tail
20 5 15 32 2
15
Queue
• The concept and principles of a queue
– front(): get an element at the front of the queue
If we request an element from a queue, it always provides the element at
the front
There is no tail() in the general implementation of the queue
20 20 5 15 32 2
16
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
The element that is first enqueued is dequeued first
Empty
queue
17
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
The element that is first enqueued is dequeued first
Empty
queue
front tail
Enqueue(20),
Enqueue(5),
Enqueue(15) 20 5 15
18
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
The element that is first enqueued is dequeued first
Empty
queue
front tail
Enqueue(20),
Enqueue(5),
Enqueue(15) 20 5 15
front tail
Dequeue()
5 15
19
Queue
• In which situations is FIFO effective?
– Waiting lines
20
Queue
• In which situations is FIFO effective?
– Packet queuing and dropping in routers
21
Implementation of Queue
• ADT of Queue
22
Array Queue
• Queue implementation using an array
– The primitive architecture of an array queue (at some state)
front tail
2 5
queue[0] queue[n-1]
queue
[0,…,n-1] 20 5 15 32
23
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
Initial state (empty queue)
24
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
enqueue(2)
front tail
25
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
enqueue(4) – enqueue (8)
front tail
2 4 5 6 7 8
26
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
dequeue(2), enqueue(4) – dequeue(6)
front tail
7 8
27
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
enqueue(9)
front tail
7 8 9
Queue is full?
28
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
enqueue(9)
front tail
7 8 9
Queue is full?
Empty space
29
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
Initial state
front
tail
n-1 0
6 1
5 2
4 3 30
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
After four times of enqueue()
front
n-1 0
1
6 1
32
13
5 2
32
tail
4 3 31
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
After two times of dequeue()
n-1 0
6 1
13
front
5 2
32
tail
4 3 32
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
After four times of enqueue()
n-1 0
tail
87
6 1
18
64 13
5 2
21 32
4 3 front 33
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
enqueue(99)
tail <- tail + 1 ?
Error
n-1 0
87
6 1
18
64 13
5 2
21 32
4 3 front 34
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
enqueue(99)
tail <- (tail+1)%n
87 99
6 1
18
64 13
5 2
21 32
4 3 front 35
Array Queue
• Implementation of enqueue(x) function
– Initial state of an array queue
tail front
queue.length-1 0
6 1
numItems = 0
5 2
4 3
36
Array Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(12)
tail
queue.length-1 0
7
6 1
numItems = 4 14
22
5 2
1
front 4 3
37
Array Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(12)
tail <- (tail+1) % queue.length
queue.length-1 0
7
6 1
numItems = 4 14
22
5 2
1
front 4 3
38
Array Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(12)
tail <- (tail+1) % queue.length
7 12
6 1
numItems = 4 14
22
5 2
1
front 4 3
39
Array Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(12)
tail
queue.length-1 0
7 12
6 1
numItems = 5 14
numItems++
22
5 2
1
front 4 3
40
Array Queue
• Implementation of dequeue() function
– Example: before and after applying dequeue()
tail
queue.length-1 0
7 12
6 1
numItems = 5 14
22
5 2
1
front 4 3
41
Array Queue
• Implementation of dequeue() function
– Example: before and after applying dequeue()
tail
queue.length-1 0
7 12
6 1
numItems = 5 14
22
5 2
get queue[front] 1
front 4 3
42
Array Queue
• Implementation of dequeue() function
– Example: before and after applying dequeue()
tail
queue.length-1 0
7 12
6 1
numItems = 5 14
22
5 2
1
(front + 1) % queue.length
4 3
43
Array Queue
• Implementation of dequeue() function
– Example: before and after applying dequeue()
tail
queue.length-1 0
7 12
6 1
numItems = 4 14
numItems--
22
5 2
1
front
4 3
44
Array Queue
• Exception case of each operation
– enqueue()
if a queue is full
– dequeue()
If a queue is empty
45
Linked List Queue
• Queue implementation using a Linked List
– The architecture of a linked list queue
Singly-linked circular linked list
10 17 35 40 55
– Empty stack
tail
10 17 35 40 55
enqueue(8)
tail
Create newNode
10 17 35 40 55 8
tail
47
Linked List Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(8)
10 17 35 40 55
enqueue(8)
tail
Set newNode.next
newNode.next = tail.next
10 17 35 40 55 8
tail
48
Linked List Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(8)
10 17 35 40 55
enqueue(8)
tail
Set tail.next
tail.next = newNode
10 17 35 40 55 8
tail
49
Linked List Queue
• Implementation of enqueue(x) function
– Example: before and after applying enqueue(8)
10 17 35 40 55
enqueue(8)
tail
Set tail
tail = newNode
10 17 35 40 55 8
tail
50
Linked List Queue
• Implementation of enqueue(x) function
– Example: in the case of starting from an empty queue
tail
51
Linked List Queue
• Implementation of enqueue(x) function
– Example: in the case of starting from an empty queue
tail
enqueue(8)
Create newNode
tail
52
Linked List Queue
• Implementation of enqueue(x) function
– Example: in the case of starting from an empty queue
tail
enqueue(8)
8
Set newNode.next
newNode.next = newNode
tail
53
Linked List Queue
• Implementation of enqueue(x) function
– Example: in the case of starting from an empty queue
tail
enqueue(8)
8
Set tail
tail = newNode
tail
54
Linked List Queue
• Implementation of dequeue(x) function
– Example: before and after applying dequeue()
10 17 35 40 55
tail
55
Linked List Queue
• Implementation of dequeue(x) function
– Example: before and after applying dequeue()
10 17 35 40 55
dequeue()
tail
10 17 35 40 55
Create front
front = tail.next tail
front
56
Linked List Queue
• Implementation of dequeue(x) function
– Example: before and after applying dequeue()
10 17 35 40 55
dequeue()
tail
10 17 35 40 55
Set tail.next
tail.next = front.next
front tail
return front
57
Linked List Queue
• Exception case of each operation
– dequeue()
If a queue is empty
58
Thank you!
E-mail: [email protected]
59