Download presentation
Presentation is loading. Please wait.
Published bySusan McCarthy Modified over 9 years ago
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 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?
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 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
3
CSC 212 – Data Structures
4
Using Stack
5
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
6
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
7
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
8
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
9
Queue Interface public interface Queue 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 Q UEUE … …while S TACK removes element at the end
10
Stacks vs. Queues
11
“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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
21
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
22
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
23
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
24
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
25
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
26
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
27
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
28
S TACKS are easy for arrays: only 1 end “moves” Can always find Stack’s bottom at index 0 Q UEUES 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 Q UEUE like clock time Circular Access q r f
29
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
30
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
31
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
32
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
33
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
34
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
35
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f Array-based Queue q r
36
Two fields track front and rear of Q UEUE f equals index of front element r 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 r f f Array-based Queue q r q r
37
Array-based Queue Operations Based on clock math Uses mod (remainder) Java expressed mod as % How mod works: 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 3 % 3 = 0 Algorithm size() N q.length return (N f + r) mod N
38
Array-based Queue Operations Algorithm enqueue(e) if size() = q.length 1 then throw FullQueueException else q[r] e r (r + 1) mod q.length q rf Algorithm dequeue() if isEmpty() then throw EmptyQueueException else retVal q[f] f (f + 1) mod q.length return retVal
39
Your Turn Get into your groups and complete activity
40
For Next Lecture No weekly assignment this week. Why, you ask? Stacks & Queues Friday’s class is quiz covering Stacks & Queues Midterm #2 Midterm #2 will be in class next Wednesday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.