Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called...

47
Applications of Stacks and Queues

Transcript of Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called...

Page 1: 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.

Applications of Stacks and Queues

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

Stacks

• Linear list.

• One end is called top.

• Other end is called bottom.

• Additions to and removals from the top end only.

Page 3: 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.

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

Page 4: 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.

The Abstract Class stack

template<class T>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;};

Page 5: 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.

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

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

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

Page 7: 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.

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1 2

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

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13)15

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

Example

• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

0 1

(2,6) (1,13) (15,19)21

Page 10: 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.

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

Page 11: 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.

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

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

Method Invocation And Returnpublic 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()

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

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.

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

Rat In A Maze

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

Rat In A Maze

• Move order is: right, down, left, up

• Block positions to avoid revisit.

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

Rat In A Maze

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

Page 17: 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.

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

Page 18: 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.

Rat In A Maze

• Move down.

Page 19: 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.

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.

Page 20: 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.

Rat In A Maze

• Move left.

Page 21: 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.

Rat In A Maze

• Move down.

Page 22: 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.

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

Page 23: 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.

Rat In A Maze

• Move backward until we reach a square from which a forward move is possible.

• Move downward.

Page 24: 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.

Rat In A Maze

• Move right.• Backtrack.

Page 25: 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.

Rat In A Maze

• Move downward.

Page 26: 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.

Rat In A Maze

• Move right.

Page 27: 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.

Rat In A Maze

• Move one down and then right.

Page 28: 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.

Rat In A Maze

• Move one up and then right.

Page 29: 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.

Rat In A Maze

• Move down to exit and eat cheese.• Path from maze entry to current position operates as a stack.

Page 30: 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.

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.

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

Bus Stop Queue

Bus Stop

frontrear

rear rear rear rear

Page 32: 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.

Bus Stop Queue

Bus Stop

frontrear

rear rear

Page 33: 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.

Bus Stop Queue

Bus Stop

frontrear

rear

Page 34: 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.

Bus Stop Queue

Bus Stop

frontrear

rear

Page 35: 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.

The Abstract Class queuetemplate <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;

};

Page 36: 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.

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.

Page 37: 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.

Wire Routing

Page 38: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable squares 1 unit from start.

Page 39: 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.

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.

Page 40: 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.

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.

Page 41: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 2 units from start.

11

Page 42: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 3 units from start.

112

22

22

Page 43: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 4 units from start.

112

22

22

3

33

3

Page 44: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 5 units from start.

112

22

22

3

33

34

44

4

4

Page 45: 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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 6 units from start.

112

22

22

3

33

34

44

4

45

5

5 55

5

Page 46: 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.

Lee’s Wire Router

start pin

end pin

End pin reached. Traceback.

112

22

22

3

33

34

44

4

45

5

5 55

56

66

666

66

Page 47: 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.

Lee’s Wire Router

start pin

end pin

4

End pin reached. Traceback.

112

22

22

3

33

34

44

4

45

5

5 55

56

66

666

66

3 521