Question of the Day Three people check into a hotel for which they pay the manager $30. The manager...
-
Upload
susan-mccarthy -
Category
Documents
-
view
216 -
download
0
Transcript of Question of the Day Three people check into a hotel for which they pay the manager $30. The manager...
![Page 1: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/1.jpg)
Question of the Day
Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy to return. To make it easier, the bellboy pockets $2 and gives $1 back to each person. Each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has $2, totaling $29. Where is the remaining dollar?
![Page 2: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/2.jpg)
Question of the Day
Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy to return. To make it easier, the bellboy pockets $2 and gives $1 back to each person. Each person paid $10 and got back $1. So they paid $9 each, totaling $27. The bellboy has $2, totaling $29. Where is the remaining dollar?
$25 (manager) + $2 (bellboy) + $3 (customers) = $30
![Page 3: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/3.jpg)
LECTURE 24:QUEUES
CSC 212 – Data Structures
![Page 4: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/4.jpg)
Last-In, First-Out principle used to access data Also called LIFO ordering
Top of stack is where data added & removed Only useful location; cannot access
anything else
Using Stack
![Page 5: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/5.jpg)
Stack Limitations
Great for Pez dispensers, JVMs,& methods All of these use most recent item added
only Do not complain when later additions
served first Many situations use items in order
added Checker at Wegmans & others prevent
cutting in line Use first-come, first-served getting food at
dining hall
![Page 6: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/6.jpg)
Stack Limitations
Great for Pez dispensers, JVMs,& methods All of these use most recent item added
only Do not complain when later additions
served first Many situations use items in order
added Checker at Wegmans & others prevent
cutting in line Use first-come, first-served getting food at
dining hall
![Page 7: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/7.jpg)
Stack Limitations
Great for web browsers, JVMs,& methods All of these use most recent item added
only Do not complain when later additions
served first Many situations use items in order
added Checker at Wegmans & others prevent
cutting in line Use first-come, first-served getting food at
dining hall
![Page 8: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/8.jpg)
Collection’s operations are part of Queue As in Stack, declares size() & isEmpty()
Add & remove elements using 2 methods Element gets added to end with enqueue(elem) dequeue() removes front element in structure
Also includes method to peek in at first element front() returns element at front without removing
Queue ADT
![Page 9: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/9.jpg)
Queue Interface
public interface Queue<E> extends Collection {public E front() throws EmptyQueueException;public E dequeue() throws EmptyQueueException;public void enqueue(E element);
}
Very similar to Stack interface Defines specific methods to add, remove, &
view data Holds many elements, but can access only
one Stack & Queue always add to the end
Remove element at start of this QUEUE… …while STACK removes element at the end
![Page 10: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/10.jpg)
Stacks vs. Queues
Access data with Stack in LIFO order
Last In-First Out Completely unfair (unless you are always
late) Data accessed in Queue using FIFO
order
First In-First Out Lines at bank, airports represented fairly
with these
![Page 11: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/11.jpg)
“Obvious” implementation uses an array Must consume a constant amount of space enqueue() throws exception when it lacks
space Instead write linked list-based
implementation Singly-, doubly-, or circular-linked list could
work Size of the Queue grows & shrinks as
needed No additional exceptions needed, but is it
slower?
Queue Implementation
![Page 12: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/12.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
![Page 13: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/13.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
elem
![Page 14: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/14.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
elem
![Page 15: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/15.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
elem
![Page 16: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/16.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
elem
![Page 17: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/17.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
retVal
![Page 18: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/18.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
retVal
![Page 19: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/19.jpg)
Class defines fields aliased to first & last nodes head & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set head to next Node in list to dequeue element
Linked-list based Queue
head rear
retVal
![Page 20: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/20.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
q
rf
![Page 21: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/21.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 22: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/22.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 23: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/23.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 24: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/24.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 25: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/25.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 26: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/26.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qrf
![Page 27: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/27.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qr f
![Page 28: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/28.jpg)
STACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0
QUEUES are harder, because both ends move dequeue calls will remove element at front Add element to back with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
qr f
![Page 29: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/29.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 30: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/30.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 31: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/31.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 32: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/32.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 33: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/33.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 34: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/34.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 35: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/35.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rf
Array-based Queue
q
r
![Page 36: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/36.jpg)
Two fields track front and rear of QUEUEf equals index of front elementr holds index immediately after rear element
Add & remove elements from opposite ends Uses circular access to the array Works like clock: when end (12) reached,
loop to start
Array must be empty at index in rff
Array-based Queue
q
r
q
r
![Page 37: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/37.jpg)
Array-based Queue Operations
Based on clock math Uses mod
(remainder) Java expressed mod
as %
How mod works:0 % 3 = 01 % 3 = 12 % 3 = 23 % 3 = 0
Algorithm size()N q.length return (N - f + r)
mod N
![Page 38: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/38.jpg)
Array-based Queue Operations
Algorithm enqueue(e)if size() = q.length 1
thenthrow
FullQueueExceptionelse
q[r] er (r + 1) mod
q.lengthq
rf
Algorithm dequeue()if isEmpty() then
throw EmptyQueueException
elseretVal q[f]f (f + 1) mod
q.lengthreturn retVal
![Page 39: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/39.jpg)
Your Turn
Get into your groups and complete activity
![Page 40: Question of the Day Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.](https://reader036.fdocuments.net/reader036/viewer/2022062409/5697bf751a28abf838c80152/html5/thumbnails/40.jpg)
For Next Lecture
No weekly assignment this week. Why, you ask?
Friday’s class is quiz covering Stacks & Queues
Midterm #2 will be in class next Wednesday