Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues Linear list. One end is called front. Other end is called rear.

Similar presentations


Presentation on theme: "Queues Linear list. One end is called front. Other end is called rear."— 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. By comparison, in a stack adds and removes are to/from the same end of he list.

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

3 Bus Stop Queue Bus Stop front rear rear rear

4 Bus Stop Queue Bus Stop front rear rear

5 Bus Stop Queue Bus Stop front rear rear
This is FIFO queue. Some real life queues are not FIFO. Removal from a queue may be done by priority rather than by arrival time order. For example, loading into a plane– first class, frequent fliers, by rows from back of plane rather than by order in which you arrive at the departure gate.

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

7 Wire Routing Represent as a grid in which components and already placed wires are denoted by blocked grid positions.

8 Lee’s Wire Router Label all reachable squares 1 unit from start.
start pin end pin Could use rat in a maze animation from Web site instead. Blue squares are blocked squares. Orange squares are available to route a wire. A queue of reachable squares is used. The queue is initially empty, and the start pin square/cell is the examine cell. This cell has a distance value of 0. Unreached unblocked squares adjacent to the examine cell are marked with their distance (this is 1 more than the distance value of the examine cell) and added to the queue. Then a cell is removed from the queue and made the new examine cell. This process is repeated until the end pin is reached or the queue becomes empty. Cells become examine cells in order of their distance from the start pin. Label all reachable squares 1 unit from start.

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

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

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

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

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

14 Lee’s Wire Router End pin reached. Traceback. 6 5 6 start pin 4 5 3 3
2 2 end pin 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.

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

16 Queue Operations IsEmpty … return true iff queue is empty
Front … return front element of queue Rear … return rear element of queue Push … add an element at the rear of the queue Pop … delete the front element of the queue

17 Queue in an Array Use a 1D array to represent a queue.
Suppose queue elements are stored with the front element in queue[0], the next in queue[1], and so on.

18 Derive From arrayList Pop() => delete queue[0]
1 2 3 4 5 6 a b c d e Pop() => delete queue[0] O(queue size) time Push(x) => if there is capacity, add at right end O(1) time

19 O(1) Pop and Push to perform each opertion in O(1) time (excluding array doubling), we use a circular representation.


Download ppt "Queues Linear list. One end is called front. Other end is called rear."

Similar presentations


Ads by Google