icom 4035 – data structures lecture 10 – queue adt manuel rodriguez martinez electrical and...
TRANSCRIPT
ICOM 4035 – Data StructuresLecture 10 – Queue ADT
Manuel Rodriguez Martinez Electrical and Computer EngineeringUniversity of Puerto Rico, Mayagüez
©Manuel Rodriguez – All rights reserved
ICOM 4035 2
Lecture Organization
• Part I – Introduction to the Queue ADT
• Part II – Design and implementation of queue using doubly linked lists
• Part III – Design and implementation of queue using dynamic arrays
M. Rodriguez-Martinez
ICOM 4035 3
Objectives
• Introduce the concept of a Queue– First-In First-Out (FIFO) list
• Discuss the application of queues
• Understand the design and implementation of a queues using arrays and linked lists
• Provide motivating examplesM. Rodriguez-Martinez
ICOM 4035 4
Companion videos
• Lecture10 videos– Contains the coding process associated with this
lecture– Shows how to build the interfaces, concrete
classes, and factory classes mentioned here
M. Rodriguez-Martinez
ICOM 4035 5
Part I
• Introduction to the Queue ADT
M. Rodriguez-Martinez
ICOM 4035 6
Queue ADT
• Queue:– collection of things with restriction on access
• Elements are added at the end and removed from the front• First element in must be first element out• No notion of specific position for elements other than element
at the front
– repetitions are allowed
M. Rodriguez-Martinez
Queue of people Queue of namesApu Ned Ron Ron
Queue of cars
ICOM 4035 7
Structure of a Queue
M. Rodriguez-Martinez
Apu Ned Ron RonEnd of queue
Front of queue
Apu Ned Ron Ron
Adding Amy to Queue
Amy
End of queue Front of queue
Removing Ron from Queue
Apu Ned Ron
End of queue Front of queue
Element are always:• Added at the end• Removed from the frontQueue: First-In First-Out (FIFO) List
ICOM 4035 8
Queue ADT Operations
• size() – number of elements in queue• isEmpty() – is the queue empty• front() – inspect element at the front of the
queue without removing it• enqueue() – add a new element at the end of
the queue• dequeue() – remove and returns element at the
front of the queue• makeEmpty() –remove all elements from queue
M. Rodriguez-Martinez
ICOM 4035 9
Queue operations: front()
M. Rodriguez-Martinez
Q.front()
Returns: “Ron”
• front() – inspects and returns the element at the front of the queue.
• No modification is made on queue.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 10
Queue operations: dequeue()
M. Rodriguez-Martinez
Q.dequeue()
Returns: “Ron”
• dequeue() – removes and returns the element at the frontof the queue.
• Size is decreased by one.
Apu Ned Ron RonQ=
Apu Ned RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 11
Queue operations: enqueue()
M. Rodriguez-Martinez
Q.enqueue(“Jil”)
Returns: nothing
• enqueue() – adds a new element at the end of the queue.• Size is increased by one.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
Jil
ICOM 4035 12
Uses for Queue (1)
• Networks – queues are used by routers to hold data packets until ready for forwarding
M. Rodriguez-Martinez
ICOM 4035 13
Uses for Queue (2)
• Print queue for shared printers – hold documents until ready for printing
M. Rodriguez-Martinez
ICOM 4035 14
Part II
• Design and implementation of queue using doubly linked list
M. Rodriguez-Martinez
ICOM 4035 15
Queues and doubly linked lists
M. Rodriguez-Martinez
Apu Ned Ron RonQ=
End of queue Front of queue
Ron Ron Nedheader
End of queue Front of queue
Apu
tail
ICOM 4035 16
Key idea: use header and tail
• Use header to point to the front of queue• Use tail to point to last element of queue• currentSize – number of elements
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu
tail
currentSize = 4
ICOM 4035 17
Sample cases
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu tail
currentSize = 4
headertail
Empty queue
currentSize = 0
headertail
One element queue
currentSize = 1
Apu
Multi element queue
ICOM 4035 18
Queue operations: front()
M. Rodriguez-Martinez
Q.front()
Returns: “Ron”
• front() – inspects and returns the element at the front of the queue.
• No modification is made on queue.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 19
Queue operations: front() (2)
• If not empty– return header.getNext().getValue()
• Null if empty• Complexity: O(1)– Simply follow the references
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu
tailcurrentSize = 4
ICOM 4035 20
Queue operations: enqueue()
M. Rodriguez-Martinez
Q.enqueue(“Jil”)
Returns: nothing
• enqueue() – adds a new element at the end of the queue.• Size is increased by one.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
Jil
ICOM 4035 21
Queue operations: enqueue() (2)
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu
tail
currentSize =Jil
4 5
ICOM 4035 22
Queue operations: enqueue() (3)
• Add a new element before the tail– tail.setPrev(newNode);
• Increment with currentSize++• Complexity: O(1)– Simply manipulate the references and int value
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
tailcurrentSize = 5
JilApu
ICOM 4035 23
Queue operations: dequeue()
M. Rodriguez-Martinez
Q.dequeue()
Returns: “Ron”
• dequeue() – removes and returns the element at the frontof the queue.
• Size is decreased by one.
Apu Ned Ron RonQ=
Apu Ned RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 24
Queue operations: dequeue() (2)
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu
tailcurrentSize= 4 3
ICOM 4035 25
Queue operations: dequeue() (3)
• If not empty– result = header.getNext().getValue()– Change header with header.setNext(header.getNext().getNext())– Decrement current size with currentSize--
• Null if empty• Complexity: O(1)
– Simply follow the references
M. Rodriguez-Martinez
Ron Nedheader
End of queue Front of queue
Apu
tailcurrentSize = 3
ICOM 4035 26
Easy operations
• size()– Return value of variable currentSize– Complexity: O(1)
• isEmpty()– Return size() == 0– Complexity: O(1)
• makeEmpty()– Call dequeue() while queue is not empty– Complexity: O(n), n = Q.size()
M. Rodriguez-Martinez
ICOM 4035 27
Part III
• Design and implementation of queue using arrays
M. Rodriguez-Martinez
ICOM 4035 28
Arrays and queue
• We have always used arrays as linear entities
M. Rodriguez-Martinez
Ron Ron Ned Apu
0 1 2 3 4 5
unusedin use
currentSize: 4
ICOM 4035 29
Problem: mapping queue to array
M. Rodriguez-Martinez
Apu Ned Ron RonQ=
End of queue Front of queue
Ron Ron Ned Apu
0 1 2 3 4 5
unusedin use
End of queue Front of queue
This scheme appears to work until we dequeue
ICOM 4035 30
Problem: dequeue
M. Rodriguez-Martinez
Apu Ned RonQ=
End of queue Front of queue
Ron Ned Apu
0 1 2 3 4 5
unusedin use
End of queue Front of queue
No we get holesin the arrayunused
ICOM 4035 31
Problem: dequeue (2)
M. Rodriguez-Martinez
Apu Ned RonQ=
End of queue Front of queue
Ron Ned Apu
0 1 2 3 4 5
unusedin use
End of queue Front of queue
One option is to move all elements to the left, but it makes queue very inefficient
ICOM 4035 32
Notion of circular array
M. Rodriguez-Martinez
Apu Ned Ron RonQ=
End of queue Front of queue
Ron Ron Ned Apu
0 1 2 3 4 5
unusedin use
End of queue Front of queue
end
0
1
23
Ron
Ron
NedApu
4
front5
Suppose we could bend the arrayto connect the end with the start
We can now wrap around numbers! Go
from 5 to 0 again
ICOM 4035 33
Notion of circular array (2)
• Front – Keep track of current
element at front
• End – Keeps track of next
available position for end
• currentSize– Current size of queue
M. Rodriguez-Martinez
Apu Ned Ron RonQ=
End of queue Front of queue
end
0
1
23
Ron
Ron
NedApu
4
front5
currentSize = 4
ICOM 4035 34
Notion of circular array (3)
• As element are added or removed simply move the variables ahead (increment by one)• Use modulo math
for this• Enables wrap around
M. Rodriguez-Martinez
Apu Ned RonQ=
End of queue Front of queue
end
0
1
23
Ron
NedApu
4
front
5
currentSize = 3
ICOM 4035 35
Notion of circular array (4)
• As element are added or removed simply move the variables ahead (increment by one)• Use modulo math
for this• Enables wrap around
M. Rodriguez-Martinez
Li Mel JilQ=
End of queue Front of queue
end
0
1
23
Mel
Jil4
front
5
currentSize = 3
Li
ICOM 4035 36
Modulo math
• Remember f(n,i) = n mod i – Remainder after dividing n by i– Range of function is set {0, 1, 2, …, i-1}
• Results are cyclic: – 0 mod 3 = 0– 1 mod 3 = 1– 2 mod 3 = 2– 3 mod 3 = 0 – 4 mod 3 = 1– 5 mod 3 = 2
...
M. Rodriguez-Martinez
ICOM 4035 37
Operating on circular array
M. Rodriguez-Martinez
0
1
2
3
Ron
Ned
Apu
4
front
end
0
1
2
3
Ron
Ron
Ned
Apu
4
front
end
Q.dequeue()
• dequeue moves the front forward
ICOM 4035 38
Notion of circular array (2)
M. Rodriguez-Martinez
• Enqueue moves the end forward• Re-allocate array when size() = elements.length -1
0
1
2
3
Ron
Ned
Apu
4
front
end
0
1
2
3
Ron
Ron
Ned
Apu
4
front
end
Q.enqueue(Li)
Ron
Li
5
ICOM 4035 39
Queue operations: front()
M. Rodriguez-Martinez
Q.front()
Returns: “Ron”
• front() – inspects and returns the element at the front of the queue.
• No modification is made on queue.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 40
Queue operations: front (2)
• If not empty, simply return element at front
• Complexity: O(1)– Random access to array
element
M. Rodriguez-Martinez
end
0
1
23
Ron
Ron
NedApu
4
front5
currentSize = 4
ICOM 4035 41
Queue operations: enqueue()
M. Rodriguez-Martinez
Q.enqueue(“Jil”)
Returns: nothing
• enqueue() – adds a new element at the end of the queue.• Size is increased by one.
Apu Ned Ron RonQ=
Apu Ned Ron RonQ=
End of queue Front of queue
End of queue Front of queue
Jil
ICOM 4035 42
Queue operations: enqueue(2)
• Add element at end• Update end as:– end = (end + 1) mod N ,
wher N = array length
• Complexity: O(n), n = Q.size()– If need to reallocate
M. Rodriguez-Martinez
end
0
1
23
Ron
Ron
NedApu
4
front5
currentSize = 4
Jil
end
currentSize = 5
ICOM 4035 43
Queue operations: enqueue(3)
• Add Xi to this example causes wrap around
• end will become 0– (5 +1) mod 6 = 0
M. Rodriguez-Martinez
0
1
23
Ron
NedApu
4
front
5
currentSize = 4
Jil
end
Xi
end
currentSize = 5
ICOM 4035 44
Queue operations: dequeue()
M. Rodriguez-Martinez
Q.dequeue()
Returns: “Ron”
• dequeue() – removes and returns the element at the frontof the queue.
• Size is decreased by one.
Apu Ned Ron RonQ=
Apu Ned RonQ=
End of queue Front of queue
End of queue Front of queue
ICOM 4035 45
Queue operations: dequeue(2)
• remove element at front• Update front as:– front = (front+ 1) mod N ,
wher N = array length
• Complexity: O(1)– Only need to manipulate
numeric value
M. Rodriguez-Martinez
end
0
1
23
Ron
Ron
NedApu
4
front5
currentSize = 4
Jil
end
front
ICOM 4035 46
Easy operations
• size()– Return value of variable top– Complexity: O(1)
• isEmpty()– Return size() == 0
• Alternative: front == end
– Complexity: O(1)• makeEmpty()– Call pop() while queue is not empty– Complexity: O(n), n = Q.size()
M. Rodriguez-Martinez
ICOM 4035 47
Summary
• Introduced the concept of a Queue– First-In First-Out (FIFO) list– Elements are added to the front– Elements are removed from the end
• Discuss the application of queues– Forwarding data in network routers– Print queues in shared printers
• Describe the design and implementation with– doubly linked lists– arrays
M. Rodriguez-Martinez