1 / 40

Question of the Day

Question of the Day.

regis
Télécharger la présentation

Question of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 Lecture 24:Queues

  4. 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

  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. 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

  9. 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

  10. 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

  11. 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?

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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 retValq[f] f(f+ 1) mod q.length returnretVal

  39. Your Turn • Get into your groups and complete activity

  40. 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

More Related