380 likes | 477 Views
Question of the Day. How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?. Question of the Day.
E N D
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
Question of the Day • How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented differently, as before?
CSC 212 – Data Structures Lecture 23:Queues
Using Stack • 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
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
Queue ADT • 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 Interface public interface Queue<E> extends Collection {public Efront()throws EmptyQueueException;public Edequeue() throws EmptyQueueException;public void enqueue(E element); } • Very similar to Stackinterface • Defines specific methods to add, remove, & view data • Holds many elements, but can access only one • Stack & Queuealways 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 • LastIn-First Out • Completely unfair (unless you are always late) • Data accessed in Queue using FIFO order • FirstIn-First Out • Lines at bank, airports represented fairly with these
Queue Implementation • “Obvious” implementation uses an array • Must consume a constant amount of space • enqueue()throws exceptionwhen 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?
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head elem
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head elem
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head elem
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head elem
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head retVal
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head retVal
Linked-list based Queue • Class defines fields aliased to first & last nodes • head & rearoften used as fields’ names (creative!) • enqueue element by adding new Node after rear • Set head to next Nodein list to dequeue element rear head retVal
Circular Access q f r • 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 f r • 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 f r • 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 f r • 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 f r • 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 f r • 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 f r • 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 f r • 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 f r • 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
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 q f r
Array-based Queue • Two fields track front and rear of Queue fequals index of front element rholds 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 inr q q f f r r
Array-based Queue Operations Algorithmsize()N q.length return(N -f+r) mod N • Based on clock math • Uses mod (remainder) • Java expressed mod as % • How mod works:0 % 3 = 01 % 3 = 12 % 3 = 23 % 3 = 0
q f r Array-based Queue Operations Algorithmenqueue(e) ifsize() = q.length 1then throw FullQueueException else q[r]e r(r+ 1) mod q.length Algorithmdequeue() ifisEmpty() then throw EmptyQueueException else retValq[f] f(f+ 1) mod q.length returnretVal
Your Turn • Get into your groups and complete activity
For Next Lecture • Read GT section 5.3 before Wednesday's class • Discusses design of the Deque ADT • Array-based implementation of Deque presented • Dequeimplementation of linked-list also shown • Week #8 weekly assignment due on Tuesday • Midterm #2 will be in class next Monday