ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and...
-
Upload
justina-blankenship -
Category
Documents
-
view
217 -
download
2
Transcript of ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and...
![Page 1: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/1.jpg)
ICOM 4035 – Data StructuresLecture 10 – Queue ADT
Manuel Rodriguez Martinez Electrical and Computer EngineeringUniversity of Puerto Rico, Mayagüez
©Manuel Rodriguez – All rights reserved
![Page 2: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/2.jpg)
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
![Page 3: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/3.jpg)
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
![Page 4: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/4.jpg)
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
![Page 5: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/5.jpg)
ICOM 4035 5
Part I
• Introduction to the Queue ADT
M. Rodriguez-Martinez
![Page 6: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/6.jpg)
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
![Page 7: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/7.jpg)
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
![Page 8: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/8.jpg)
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
![Page 9: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/9.jpg)
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
![Page 10: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/10.jpg)
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
![Page 11: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/11.jpg)
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
![Page 12: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/12.jpg)
ICOM 4035 12
Uses for Queue (1)
• Networks – queues are used by routers to hold data packets until ready for forwarding
M. Rodriguez-Martinez
![Page 13: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/13.jpg)
ICOM 4035 13
Uses for Queue (2)
• Print queue for shared printers – hold documents until ready for printing
M. Rodriguez-Martinez
![Page 14: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/14.jpg)
ICOM 4035 14
Part II
• Design and implementation of queue using doubly linked list
M. Rodriguez-Martinez
![Page 15: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/15.jpg)
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
![Page 16: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/16.jpg)
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
![Page 17: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/17.jpg)
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
![Page 18: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/18.jpg)
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
![Page 19: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/19.jpg)
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
![Page 20: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/20.jpg)
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
![Page 21: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/21.jpg)
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
![Page 22: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/22.jpg)
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
![Page 23: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/23.jpg)
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
![Page 24: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/24.jpg)
ICOM 4035 24
Queue operations: dequeue() (2)
M. Rodriguez-Martinez
Ron Ron Nedheader
End of queue Front of queue
Apu
tailcurrentSize= 4 3
![Page 25: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/25.jpg)
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
![Page 26: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/26.jpg)
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
![Page 27: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/27.jpg)
ICOM 4035 27
Part III
• Design and implementation of queue using arrays
M. Rodriguez-Martinez
![Page 28: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/28.jpg)
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
![Page 29: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/29.jpg)
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
![Page 30: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/30.jpg)
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
![Page 31: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/31.jpg)
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
![Page 32: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/32.jpg)
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
![Page 33: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/33.jpg)
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
![Page 34: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/34.jpg)
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
![Page 35: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/35.jpg)
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
![Page 36: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/36.jpg)
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
![Page 37: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/37.jpg)
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
![Page 38: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/38.jpg)
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
![Page 39: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/39.jpg)
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
![Page 40: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/40.jpg)
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
![Page 41: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/41.jpg)
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
![Page 42: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/42.jpg)
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
![Page 43: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/43.jpg)
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
![Page 44: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/44.jpg)
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
![Page 45: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/45.jpg)
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
![Page 46: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/46.jpg)
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
![Page 47: ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.](https://reader030.fdocuments.net/reader030/viewer/2022032722/56649f475503460f94c6989b/html5/thumbnails/47.jpg)
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