bus stop queue queues
play

Bus Stop Queue Queues Linear list. Bus Stop One end is called - PDF document

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


  1. Bus Stop Queue Queues • Linear list. Bus Stop • One end is called front. front rear rear rear rear • Other end is called rear. rear • Additions are done at the rear only. • Removals are made from the front only. Bus Stop Queue Bus Stop Queue Bus Bus Stop Stop front rear rear front rear rear rear Bus Stop Queue The Interface Queue public interface Queue Bus { Stop public boolean isEmpty(); front rear rear public Object getFrontEelement(); public Object getRearEelement(); public void put(Object theObject); public Object remove(); }

  2. Revisit Of Stack Applications Wire Routing • 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. Lee’s Wire Router Lee’s Wire Router start pin start pin end pin end pin 1 1 Label all reachable unlabeled squares 2 units Label all reachable squares 1 unit from start. from start. Lee’s Wire Router Lee’s Wire Router start pin start pin 3 3 2 2 2 2 end pin end pin 1 1 2 1 1 2 2 2 2 2 3 3 Label all reachable unlabeled squares 3 units Label all reachable unlabeled squares 4 units from start. from start.

  3. Lee’s Wire Router Lee’s Wire Router 5 start pin 4 start pin 4 5 3 3 3 3 2 2 2 2 end pin end pin 1 1 2 1 1 2 2 2 2 2 3 4 3 4 3 4 3 4 5 4 4 4 4 5 5 5 Label all reachable unlabeled squares 5 units Label all reachable unlabeled squares 6 units from start. from start. Lee’s Wire Router Lee’s Wire Router 6 5 6 6 5 6 start pin 4 5 start pin 4 5 3 3 3 3 2 2 2 2 end pin end pin 1 1 2 1 1 1 2 2 2 6 2 2 2 6 3 4 3 4 5 6 3 4 3 3 4 4 5 5 6 4 4 5 6 4 4 5 6 5 6 5 6 5 6 5 6 6 6 End pin reached. Traceback. End pin reached. Traceback. Derive From ArrayLinearList Derive From ArrayLinearList a b c d e e d c b a 0 1 2 3 4 5 6 0 1 2 3 4 5 6 � when front is left end of list and rear is right end � when rear is left end of list and front is right end • Queue.isEmpty() => super.isEmpty() • Queue.isEmpty() => super.isEmpty() – O(1) time – O(1) time • getFrontElement() => get(0) • getFrontElement() => get(size() - 1) – O(1) time – O(1) time • getRearElement() => get(size() - 1) • getRearElement() => get(0) – O(1) time – O(1) time • put(theObject) => add(size(), theObject) • put(theObject) => add(0, theObject) – O(1) time – O(size) time • remove() => remove(0) • remove() => remove(size() - 1) – O(size) time – O(1) time

  4. Derive From ExtendedChain Derive From ArrayLinearList lastNode firstNode � to perform each opertion in O(1) time (excluding null array doubling), we need a customized array a b c d e representation. front rear � 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 Derive From ExtendedChain Derive From ExtendedChain lastNode lastNode firstNode firstNode null null a b c d e e d c b a front rear rear front • getRearElement() => getLast() … new method � when front is right end of list and rear is left end – O(1) time • Queue.isEmpty() => super.isEmpty() • put(theObject) => append(theObject) – O(1) time – O(1) time • getFrontElement() => getLast() • remove() => remove(0) – O(1) time – O(1) time Derive From ExtendedChain Custom Linked Code lastNode firstNode null a b c d e • Develop a linked class for Queue from scratch to get better preformance than obtainable by deriving from ExtendedChain. rear front • getRearElement() => get(0) – O(1) time • put(theObject) => add(0, theObject) – O(1) time • remove() => remove(size-1) – O(size) time

  5. Custom Array Queue Custom Array Queue • Possible configuration with 3 elements. • Use a 1D array queue. queue[] [2] [3] A B • Circular view of array. C [1] [4] [2] [3] [0] [5] [1] [4] [0] [5] Custom Array Queue Custom Array Queue • Another possible configuration with 3 • Use integer variables front and rear. elements. – front is one position counterclockwise from first element [2] [3] – rear gives position of last element C [2] [3] [2] [3] [1] [4] A B rear rear B A front front [0] [5] C C [1] [4] [1] [4] B A [0] [5] [0] [5] Add An Element Add An Element • Move rear one clockwise. • Move rear one clockwise. • Then put into queue[rear]. [2] [3] [2] [3] A B A B rear front front C C [1] [4] [1] [4] D [0] [5] [0] [5] rear

  6. Remove An Element Remove An Element • Move front one clockwise. • Move front one clockwise. • Then extract from queue[front]. [2] [3] [2] [3] front A B A B rear rear front C C [1] [4] [1] [4] [0] [5] [0] [5] Moving rear Clockwise Empty That Queue • rear++; [2] [3] if ( rear = = queue.length) rear = 0; rear front C [1] [4] [2] [3] A B B A rear front [0] [5] C [1] [4] [0] [5] • rear = (rear + 1) % queue.length; Empty That Queue Empty That Queue [2] [3] [2] [3] rear rear C C [1] [4] [1] [4] B [0] [5] [0] [5] front front

  7. Empty That Queue A Full Tank Please [2] [3] [2] [3] rear rear front C [1] [4] [1] [4] B A front [0] [5] [0] [5] • 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. A Full Tank Please A Full Tank Please [2] [3] [2] [3] rear rear D D E front front C C [1] [4] [1] [4] B A B A [0] [5] [0] [5] A Full Tank Please Ouch!!!!! • Remedies. [2] [3] � Don’t let the queue get full. D E front • When the addition of an element will cause the queue to be C F full, increase array size. [1] [4] • This is what the text does. B A rear [0] [5] � Define a boolean variable lastOperationIsPut. • Following each put set this variable to true. • Following each remove set to false. • When a series of adds causes the queue to • Queue is empty iff (front == rear) && !lastOperationIsPut become full, front = rear. • Queue is full iff (front == rear) && lastOperationIsPut • So we cannot distinguish between a full queue and an empty queue!

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend