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 The Abstract Class queue
template <class T> class queue { public: virtual ~queue() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& front() = 0; virtual T& back() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

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 Represent as a grid in which components and already placed wires are denoted by blocked grid positions.

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

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

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

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

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

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

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

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

17 Derive From arrayList 1 2 3 4 5 6 a b c d e when front is left end of list and rear is right end empty() => queue::empty() O(1) time size() => queue::size(0) front() => get(0) back() => get(size() - 1)

18 Derive From arrayList 1 2 3 4 5 6 a b c d e pop() => erase(0)
1 2 3 4 5 6 a b c d e pop() => erase(0) O(size) time push(theElement) => insert(size(), theElement) O(1) time

19 Derive From arrayList 1 2 3 4 5 6 a b c d e when front is right end of list and rear is left end empty() => queue::empty() O(1) time size() => queue::size(0) front() => get(size() - 1) back() => get(0)

20 Derive From arrayList 1 2 3 4 5 6 a b c d e
1 2 3 4 5 6 a b c d e pop() => erase(size() - 1) O(1) time push(theElement) => insert(0, theElement) O(size) time

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

22 Derive From extendedChain
b c d e NULL firstNode lastNode front rear when front is left end of list and rear is right end empty() => extendedChain::empty() O(1) time size() => extendedChain::size()

23 Derive From ExtendedChain
b c d e NULL firstNode lastNode front rear front() => get (0) O(1) time back() => getLast() … new method

24 Derive From ExtendedChain
b c d e NULL firstNode lastNode front rear push(theElement) => push_back(theElement) O(1) time pop() => erase(0)

25 Derive From extendedChain
b a NULL firstNode lastNode rear front when front is right end of list and rear is left end empty() => extendedChain::empty() O(1) time size() => extendedChain::size()

26 Derive From extendedChain
b c d e NULL firstNode lastNode rear front front() => getLast() O(1) time back() => get(0)

27 Derive From extendedChain
b c d e NULL firstNode lastNode rear front push(theElement) => insert(0, theElement) O(1) time pop() => erase(size() - 1) O(size) time

28 Custom Linked Code Develop a linked class for queue from scratch to get better preformance than obtainable by deriving from extendedChain.

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

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

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

32 Custom Array Queue Use integer variables theFront and theBack.
theFront is one position counterclockwise from first element theBack gives position of last element use front and rear in figures [0] [1] [2] [3] [4] [5] A B C [0] [1] [2] [3] [4] [5] A B C rear rear front front

33 Push An Element Move rear one clockwise. [0] [1] [2] [3] [4] [5] A B C
front rear

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

35 Pop An Element Move front one clockwise. [0] [1] [2] [3] [4] [5] A B C
rear

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

37 Moving rear Clockwise if (rear = = arrayLength) rear = 0; rear++;
[0] [1] [2] [3] [4] [5] A B C front rear rear = (rear + 1) % arrayLength;

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

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

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

41 Empty That Queue [0] [1] [2] [3] [4] [5] rear front 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.

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

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

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

45 A Full Tank Please [0] [1] [2] [3] [4] [5] D E front 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!

46 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 lastOperationIsPush. Following each push set this variable to true. Following each pop set to false. Queue is empty iff (front == rear) && !lastOperationIsPush Queue is full iff (front == rear) && lastOperationIsPush

47 Ouch!!!!! Remedies (continued). Define an integer variable size.
Following each push do size++. Following each pop do size--. Queue is empty iff (size == 0) Queue is full iff (size == arrayLength) Performance is slightly better when first strategy is used. Due to the cost of increments/decrements But we do those operations anyway  size() in O(1)


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

Similar presentations


Ads by Google