Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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

3 Bus Stop Queue Bus Stop front rear

4 Bus Stop Queue Bus Stop front rear

5 Bus Stop Queue Bus Stop front rear

6 Bus Stop Queue Bus Stop front rear

7 Queue – formula based (b). Delete(x), (c). Add(D)

8 ADT of Queue

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

10 Wire Routing

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

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

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

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

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

16 Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 6 units from start. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5

17 Lee’s Wire Router start pin end pin End pin reached. Traceback. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5 6 6 6 66 6 66

18 Lee’s Wire Router start pin end pin 4 End pin reached. Traceback. 11 2 22 2 2 3 33 3 4 4 4 4 4 5 5 55 5 5 6 6 6 66 6 66 35 2 1

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

20 Custom Array Queue Possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

21 Custom Array Queue Another possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

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

23 Add An Element [0] [1] [2][3] [4] [5] AB C front rear Move rear one clockwise.

24 Add An Element Move rear one clockwise. [0] [1] [2][3] [4] [5] AB C front rear Then put into queue[rear]. D

25 Remove An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise.

26 Remove An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise. Then extract from queue[front].

27 Moving rear Clockwise [0] [1] [2][3] [4] [5] AB C front rear rear++; if ( rear = = queue.length) rear = 0; rear = (rear + 1) % queue.length;

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

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

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

31 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. [0] [1] [2][3] [4] [5] front rear

32 A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear

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

34 A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE

35 A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE F 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!

36 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 –Performance is slightly better when first strategy is used.

37 Queue – formula based

38 Queue – formula based (cont.)

39

40

41 int main(void) { Queue q(100); // add a few elements q.Add(1); cout << "Queue back is " << q.Last() << endl; q.Add(2); cout << "Queue back is " << q.Last() << endl; q.Add(3); cout << "Queue back is " << q.Last() << endl; q.Add(4); cout << "Queue back is " << q.Last() << endl; // The above statement can be written as: // q.Add(1).Add(2).Add(3).Add(4) cout << "Queue should be 1234, front to back" << endl; Test program

42 // test empty and size if (q.Empty()) cout << "The queue is empty" << endl; else cout << "The queue is not empty" << endl; int x; while (!q.Empty()) { cout << "Queue front is " << q.First() << endl; q.Delete(x); cout << “Deleted front element" << endl; } return 0; }

43 Output Queue back is 1 Queue back is 2 Queue back is 3 Queue back is 4 Queue should be 1234, front to back The queue is not empty Queue front is 1 Deleted front element Queue front is 2 Deleted front element Queue front is 3 Deleted front element Queue front is 4

44 Derive From Chain abcde null firstNode lastNode frontrear

45 Queue – linked list

46 Queue – linked list (cont.)

47

48

49

50

51 int main(void) { LinkedQueue q; // add a few elements q.Add(1); cout << "Queue back is " << q.Last() << endl; q.Add(2); cout << "Queue back is " << q.Last() << endl; q.Add(3); cout << "Queue back is " << q.Last() << endl; q.Add(4); cout << "Queue back is " << q.Last() << endl; // The above statement can be written as: // q.Add(1).Add(2).Add(3).Add(4) cout << "Queue should be 1234, front to back" << endl; Test program

52 // test empty and size if (q.Empty()) cout << "The queue is empty" << endl; else cout << "The queue is not empty" << endl; int x; while (!q.Empty()) { cout << "Queue front is " << q.First() << endl; q.Delete(x); cout << “Deleted front element" << endl; } return 0; }

53 Output Queue back is 1 Queue back is 2 Queue back is 3 Queue back is 4 Queue should be 1234, front to back The queue is not empty Queue front is 1 Deleted front element Queue front is 2 Deleted front element Queue front is 3 Deleted front element Queue front is 4


Download ppt "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."

Similar presentations


Ads by Google