Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

Similar presentations


Presentation on theme: "Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only."— Presentation transcript:

1

2 Applications of Stacks and Queues

3 Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.

4 Stack Of Cups Add a cup to the stack. bottom top C A B D E F Remove a cup from new stack. A stack is a LIFO list. bottom top C A B D E

5 The Abstract Class stack template class stack { public: virtual ~stack() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& top() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

6 Parentheses Matching (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) –Output pairs (u,v) such that the left parenthesis at position u is matched with the right parenthesis at v. (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38) (a+b))*((c+d) –(0,4) –right parenthesis at 5 has no matching left parenthesis –(8,12) –left parenthesis at 7 has no matching right parenthesis

7 Parentheses Matching scan expression from left to right when a left parenthesis is encountered, add its position to the stack when a right parenthesis is encountered, remove matching position from stack

8 Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 2

9 Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13) 15

10 Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19) 21

11 Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25) 27

12 Example (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 0 1 (2,6)(1,13)(15,19)(21,25)(27,31)(0,32) and so on

13 Method Invocation And Return public void a() { …; b(); …} public void b() { …; c(); …} public void c() { …; d(); …} public void d() { …; e(); …} public void e() { …; c(); …} return address in a() return address in b() return address in c() return address in d() return address in e() return address in c() return address in d()

14 Method Invocation And Return When a method is invoked, the address to return to following execution of the invoked method is pushed on to a stack. When a return is executed, a return to address (location in program) is popped from the stack and execution continues from that address.

15 Rat In A Maze

16 Move order is: right, down, left, up Block positions to avoid revisit.

17 Rat In A Maze Move order is: right, down, left, up Block positions to avoid revisit.

18 Rat In A Maze Move backward until we reach a square from which a forward move is possible.

19 Rat In A Maze Move down.

20 Algorithm Using Stack Yellow squares are on the stack. Red squares have been but are not right now on stack. Red squares should not be moved to again. Yellow squares may have adjacent squares not yet moved to.

21 Rat In A Maze Move left.

22 Rat In A Maze Move down.

23 Rat In A Maze Move backward until we reach a square from which a forward move is possible.

24 Rat In A Maze Move backward until we reach a square from which a forward move is possible. Move downward.

25 Rat In A Maze Move right. Backtrack.

26 Rat In A Maze Move downward.

27 Rat In A Maze Move right.

28 Rat In A Maze Move one down and then right.

29 Rat In A Maze Move one up and then right.

30 Rat In A Maze Move down to exit and eat cheese. Path from maze entry to current position operates as a stack.

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

32 Bus Stop Queue Bus Stop front rear

33 Bus Stop Queue Bus Stop front rear

34 Bus Stop Queue Bus Stop front rear

35 Bus Stop Queue Bus Stop front rear

36 The Abstract Class queue template 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; };

37 Revisit Of Stack Applications Applications in which the stack cannot be replaced with a queue. –Parentheses matching. –Towers of Hanoi. –Method invocation and return. Application in which the stack may be replaced with a queue. –Rat in a maze. Results in finding shortest path to exit.

38 Wire Routing

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

40 Algorithm Using Queue A queue of reachable squares is used. The queue is initially empty, and the start pin cell is the examine cell. Unreached unblocked squares adjacent to the examine cell are marked with their distance and added to the queue.

41 Algorithm Using Queue (Cont.) 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.

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

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

44 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

45 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

46 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

47 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

48 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


Download ppt "Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only."

Similar presentations


Ads by Google