1 / 43

Queues

Queues. Linear list. One end is called front . Other end is called rear . Additions are done at the rear only. Removals are made from the front only. Bus Stop. Bus Stop Queue. front. rear. rear. rear. rear. rear. Bus Stop. Bus Stop Queue. front. rear. rear. rear.

anjelita
Download Presentation

Queues

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. Queues • Linear list. • One end is called front. • Other end is called rear. • Additions are done at the rear only. • Removals are made from the front only.

  2. Bus Stop Bus Stop Queue front rear rear rear rear rear

  3. Bus Stop Bus Stop Queue front rear rear rear

  4. Bus Stop Bus Stop Queue front rear rear

  5. Bus Stop Bus Stop Queue front rear rear

  6. The Interface Queue public interface Queue { public boolean isEmpty(); public Object getFrontEelement(); public Object getRearEelement(); public void put(Object theObject); public Object remove(); }

  7. Revisit Of Stack Applications • Applications in which the stack cannot be replaced with a queue. • Parentheses matching. • Towers of Hanoi. • Switchbox routing. • Method invocation and return. • Try-catch-throw implementation. • Application in which the stack may be replaced with a queue. • Rat in a maze. • Results in finding shortest path to exit.

  8. Wire Routing

  9. start pin end pin Lee’s Wire Router Label all reachable squares 1 unit from start.

  10. start pin end pin Lee’s Wire Router 1 1 Label all reachable unlabeled squares 2 units from start.

  11. start pin end pin Lee’s Wire Router 2 2 1 1 2 2 2 Label all reachable unlabeled squares 3 units from start.

  12. start pin end pin Lee’s Wire Router 3 3 2 2 1 1 2 2 2 3 3 Label all reachable unlabeled squares 4 units from start.

  13. start pin end pin Lee’s Wire Router 4 3 3 2 2 1 1 2 2 2 3 4 3 4 4 4 Label all reachable unlabeled squares 5 units from start.

  14. start pin end pin Lee’s Wire Router 5 4 5 3 3 2 2 1 1 2 2 2 3 4 3 4 5 4 4 5 5 5 Label all reachable unlabeled squares 6 units from start.

  15. start pin end pin Lee’s Wire Router 6 5 6 4 5 3 3 2 2 1 1 2 2 2 6 3 4 3 4 5 6 4 4 5 6 5 6 5 6 6 End pin reached. Traceback.

  16. start pin end pin 1 2 3 4 Lee’s Wire Router 6 5 6 4 5 3 3 2 2 1 1 2 2 2 6 3 4 3 4 5 5 6 4 4 5 6 5 6 5 6 6 End pin reached. Traceback.

  17. a b c d e 0 1 2 3 4 5 6 Derive From ArrayLinearList • when front is left end of list and rear is right end • Queue.isEmpty() => super.isEmpty() • O(1) time • getFrontElement() => get(0) • O(1) time • getRearElement() => get(size() - 1) • O(1) time • put(theObject) => add(size(), theObject) • O(1) time • remove() => remove(0) • O(size) time

  18. e d c b a 0 1 2 3 4 5 6 Derive From ArrayLinearList • when rear is left end of list and front is right end • Queue.isEmpty() => super.isEmpty() • O(1) time • getFrontElement() => get(size() - 1) • O(1) time • getRearElement() => get(0) • O(1) time • put(theObject) => add(0, theObject) • O(size) time • remove() => remove(size() - 1) • O(1) time

  19. Derive From ArrayLinearList • to perform each opertion in O(1) time (excluding array doubling), we need a customized array representation.

  20. lastNode firstNode null a b c d e front rear Derive From ExtendedChain • when front is left end of list and rear is right end • Queue.isEmpty() => super.isEmpty() • O(1) time • getFrontElement() => get(0) • O(1) time

  21. lastNode firstNode null a b c d e front rear Derive From ExtendedChain • getRearElement() => getLast() … new method • O(1) time • put(theObject) => append(theObject) • O(1) time • remove() => remove(0) • O(1) time

  22. lastNode firstNode null e d c b a rear front Derive From ExtendedChain • when front is right end of list and rear is left end • Queue.isEmpty() => super.isEmpty() • O(1) time • getFrontElement() => getLast() • O(1) time

  23. lastNode firstNode null a b c d e rear front Derive From ExtendedChain • getRearElement() => get(0) • O(1) time • put(theObject) => add(0, theObject) • O(1) time • remove() => remove(size-1) • O(size) time

  24. Custom Linked Code • Develop a linked class for Queue from scratch to get better preformance than obtainable by deriving from ExtendedChain.

  25. queue[] [2] [3] [1] [4] [0] [5] Custom Array Queue • Use a 1D array queue. • Circular view of array.

  26. [2] [3] A B C [1] [4] [0] [5] Custom Array Queue • Possible configuration with 3 elements.

  27. [2] [3] C [1] [4] B A [0] [5] Custom Array Queue • Another possible configuration with 3 elements.

  28. [2] [3] [2] [3] A B rear rear front front C C [1] [4] [1] [4] B A [0] [5] [0] [5] Custom Array Queue • Use integer variables front and rear. • front is one position counterclockwise from first element • rear gives position of last element

  29. [2] [3] A B rear front C [1] [4] [0] [5] Add An Element • Move rear one clockwise.

  30. [2] [3] A B front C [1] [4] [0] [5] rear Add An Element • Move rear one clockwise. • Then put into queue[rear]. D

  31. [2] [3] A B rear front C [1] [4] [0] [5] Remove An Element • Move front one clockwise.

  32. [2] [3] front A B rear C [1] [4] [0] [5] Remove An Element • Move front one clockwise. • Then extract from queue[front].

  33. [2] [3] A B rear front C [1] [4] [0] [5] Moving rear Clockwise • rear++; • if(rear = = queue.length) rear = 0; • rear = (rear + 1) % queue.length;

  34. [2] [3] rear front C [1] [4] B A [0] [5] Empty That Queue

  35. [2] [3] rear C [1] [4] B [0] [5] Empty That Queue front

  36. [2] [3] rear C [1] [4] [0] [5] Empty That Queue front

  37. [2] [3] rear [1] [4] [0] [5] Empty That Queue • When a series of removes causes the queue to become empty, front = rear. • When a queue is constructed, it is empty. • So initialize front = rear = 0. front

  38. [2] [3] rear front C [1] [4] B A [0] [5] A Full Tank Please

  39. [2] [3] rear front [1] [4] [0] [5] A Full Tank Please D C B A

  40. [2] [3] front [1] [4] [0] [5] A Full Tank Please rear D E C B A

  41. [2] [3] front [1] [4] [0] [5] A Full Tank Please D E C F B A rear • When a series of adds causes the queue to become full, front = rear. • So we cannot distinguish between a full queue and an empty queue!

  42. Ouch!!!!! • Remedies. • Don’t let the queue get full. • When the addition of an element will cause the queue to be full, increase array size. • This is what the text does. • Define a boolean variable lastOperationIsPut. • Following each put set this variable to true. • Following each remove set to false. • Queue is empty iff (front == rear) && !lastOperationIsPut • Queue is full iff (front == rear) && lastOperationIsPut

  43. Ouch!!!!! • Remedies (continued). • Define an integer variable size. • Following each put do size++. • Following each remove do size--. • Queue is empty iff (size == 0) • Queue is full iff (size == queue.length) • Performance is slightly better when first strategy is used.

More Related