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

Preview:

Citation preview

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

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;};

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

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

Example

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

0 1 2

Example

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

0 1

(2,6) (1,13)15

Example

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

0 1

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

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

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

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()

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.

Rat In A Maze

Rat In A Maze

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

• Block positions to avoid revisit.

Rat In A Maze

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

Rat In A Maze

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

Rat In A Maze

• Move down.

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.

Rat In A Maze

• Move left.

Rat In A Maze

• Move down.

Rat In A Maze

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

Rat In A Maze

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

• Move downward.

Rat In A Maze

• Move right.• Backtrack.

Rat In A Maze

• Move downward.

Rat In A Maze

• Move right.

Rat In A Maze

• Move one down and then right.

Rat In A Maze

• Move one up and then right.

Rat In A Maze

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

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.

Bus Stop Queue

Bus Stop

frontrear

rear rear rear rear

Bus Stop Queue

Bus Stop

frontrear

rear rear

Bus Stop Queue

Bus Stop

frontrear

rear

Bus Stop Queue

Bus Stop

frontrear

rear

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;

};

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.

Wire Routing

Lee’s Wire Router

start pin

end pin

Label all reachable squares 1 unit from start.

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.

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.

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 2 units from start.

11

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 3 units from start.

112

22

22

Lee’s Wire Router

start pin

end pin

Label all reachable unlabeled squares 4 units from start.

112

22

22

3

33

3

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

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

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

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

Recommended