question of the day
DESCRIPTION
Question of the Day. - PowerPoint PPT PresentationTRANSCRIPT
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?
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
LECTURE 23:QUEUES
CSC 212 – Data Structures
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
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
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
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
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
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
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
“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
Does Space Matter?
Does Space Matter?
“Well-Known Study” On Space
“Well-Known Study” On Space
“Well-Known Study” On Space
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Your Turn
Get into your groups and complete activity
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