question of the day
DESCRIPTION
Question of the Day. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/3.jpg)
LECTURE 23:QUEUES
CSC 212 – Data Structures
![Page 4: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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 first element in structure
Also includes method to peek in at first element first() returns first element without removing
Queue ADT
![Page 9: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/9.jpg)
Queue Interface
public interface Queue<E> extends Collection {public E first() throws EmptyCollectionException;public E dequeue() throws EmptyCollectionException;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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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
orderFirst In-First Out Lines at bank, airports represented fairly
with these
![Page 11: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/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- or doubly-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](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/12.jpg)
Does Space Matter?
![Page 13: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/13.jpg)
Does Space Matter?
![Page 14: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/14.jpg)
“Well-Known Study” On Space
![Page 15: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/15.jpg)
“Well-Known Study” On Space
![Page 16: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/16.jpg)
“Well-Known Study” On Space
![Page 17: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/17.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Linked-list based Queue
front rear
![Page 18: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/18.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Linked-list based Queue
front rear
elem
![Page 19: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/19.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Linked-list based Queue
front rear
elem
![Page 20: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/20.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Linked-list based Queue
front rear
elem
![Page 21: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/21.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Linked-list based Queue
front rear
elem
![Page 22: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/22.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set front to next Node in list to dequeue element
Linked-list based Queue
front rear
retVal
![Page 23: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/23.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set front to next Node in list to dequeue element
Linked-list based Queue
front rear
retVal
![Page 24: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/24.jpg)
Class defines fields aliased to first & last nodes front & rear often used as fields’ names
(creative!) enqueue element by adding new Node after rear
Set front to next Node in list to dequeue element
Linked-list based Queue
front rear
retVal
![Page 25: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/25.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queue
rearfront
![Page 26: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/26.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront
rear
![Page 27: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/27.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront
rear
![Page 28: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/28.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront
rear
![Page 29: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/29.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront rear
![Page 30: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/30.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront rear
![Page 31: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/31.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefront rear
![Page 32: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/32.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefrontrear
![Page 33: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/33.jpg)
STACKS are easy for arrays: only 1 end “moves” Stack can rely on index 0 since bottom
stationary QUEUES are harder, because both ends
move dequeue calls will remove element at front Add element to rear with calls to enqueue
Ends of a array-based QUEUE like clock time
Circular Access
queuefrontrear
![Page 34: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/34.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
![Page 35: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/35.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear
element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 36: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/36.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear
element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 37: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/37.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 38: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/38.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear
element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 39: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/39.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 40: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/40.jpg)
Two fields track front and rear of QUEUEfront equals index of front elementrear holds index immediately after rear element(QUEUE stores number of elements using count)
Adds & removes elements from opposite ends Uses circular indexing within array When end reached, index loops back to 0; just
like clock
Array-based Queue
frontqueue
rear
![Page 41: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/41.jpg)
Array-based Queue Operations
Algorithm enqueue(e)if size() = queue.length
thenexpandCapacity()
endif queue[rear] erear (rear + 1) rear rear %
queue.length count count + 1
queuerearfront
Algorithm dequeue()if isEmpty() then
throw EmptyCollectionException
elseretVal
queue[front]front (front + 1)front front %
queue.lengthcount count - 1return retVal
![Page 42: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/42.jpg)
Your Turn
Get into your groups and complete activity
![Page 43: Question of the Day](https://reader035.vdocuments.us/reader035/viewer/2022081604/56816569550346895dd7f852/html5/thumbnails/43.jpg)
For Next Lecture
Tomorrow at 5PM weekly assignment #8 due
Wednesday’s class: quiz on Stacks & Queues
Project #1 due on Saturday at 11:59PM