stacks
play

Stacks Linear list. One end is called top. Other end is called - PDF document

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 top F top E E D D C C B B bottom bottom A A Add a cup to the stack.


  1. 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 top F top E E D D C C B B bottom bottom A A • Add a cup to the stack. • Remove a cup from new stack. • A stack is a LIFO list.

  2. The Interface Stack public interface Stack { public boolean empty(); public Object peek(); public void push(Object theObject); public Object pop(); } 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

  3. 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) 2 1 0

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

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

  6. Towers Of Hanoi/Brahma 4 3 2 1 A B C • 64 gold disks to be moved from tower A to tower C • each tower operates as a stack • cannot place big disk on top of a smaller one Towers Of Hanoi/Brahma 3 2 1 A B C • 3-disk Towers Of Hanoi/Brahma

  7. Towers Of Hanoi/Brahma 2 1 3 A B C • 3-disk Towers Of Hanoi/Brahma Towers Of Hanoi/Brahma 1 2 3 A B C • 3-disk Towers Of Hanoi/Brahma

  8. Towers Of Hanoi/Brahma 3 1 2 A B C • 3-disk Towers Of Hanoi/Brahma Towers Of Hanoi/Brahma 3 2 1 A B C • 3-disk Towers Of Hanoi/Brahma

  9. Towers Of Hanoi/Brahma 3 2 1 A B C • 3-disk Towers Of Hanoi/Brahma Towers Of Hanoi/Brahma 2 3 1 A B C • 3-disk Towers Of Hanoi/Brahma

  10. Towers Of Hanoi/Brahma 3 2 1 A B C • 3-disk Towers Of Hanoi/Brahma • 7 disk moves Recursive Solution 1 A B C • n > 0 gold disks to be moved from A to C using B • move top n-1 disks from A to B using C

  11. Recursive Solution 1 A B C • move top disk from A to C Recursive Solution 1 A B C • move top n-1 disks from B to C using A

  12. Recursive Solution 1 A B C • moves(n) = 0 when n = 0 • moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0 Towers Of Hanoi/Brahma • moves(64) = 1.8 * 10 19 (approximately) • Performing 10 9 moves/second, a computer would take about 570 years to complete. • At 1 disk move/min, the monks will take about 3.4 * 10 13 years.

  13. Chess Story • 1 grain of rice on the first square, 2 for next, 4 for next, 8 for next, and so on. • Surface area needed exceeds surface area of earth. Chess Story • 1 penny for the first square, 2 for next, 4 for next, 8 for next, and so on. • $3.6 * 10 17 (federal budget ~ $2 * 10 12 ) .

  14. Switch Box Routing 1 2 3 4 5 6 7 8 9 10 40 11 39 12 38 13 37 14 36 15 Routing region 35 16 34 17 33 18 32 19 31 20 30 29 28 27 26 25 24 23 22 21 Routing A 2-pin Net 1 2 3 4 4 5 6 7 8 9 10 40 11 Routing Routing 39 12 for pins for pins 5 38 13 1-3 and through 18-40 is 37 14 16 is confined 36 15 confined to lower 35 16 to upper left right 34 17 17 region. region. 33 18 32 19 31 20 30 29 28 27 26 25 24 23 22 21

  15. Routing A 2-pin Net 1 2 3 4 4 5 6 7 8 9 10 40 11 Examine (u,v), 39 12 pins in u<v is a clock- 38 13 2-pin wise net. 37 14 order 36 15 u is start beginn- pin. 35 16 ing with pin 1. 34 17 17 v is end pin. 33 18 32 19 31 20 30 29 28 27 26 25 24 23 22 21 Routing A 2-pin Net 1 2 3 4 4 5 6 7 8 9 10 40 11 Start pin 39 12 => push 38 13 onto stack. 37 14 36 15 End pin => start 35 16 pin must 34 17 17 be at top 33 18 of stack. 32 19 31 20 30 29 28 27 26 25 24 23 22 21

  16. Method Invocation And Return public void a() { …; b(); …} public void b() { …; c(); …} return address in d() return address in c() public void c() return address in e() { …; d(); …} return address in d() public void d() return address in c() { …; e(); …} return address in b() public void e() return address in a() { …; c(); …} Try-Throw-Catch • When you enter a try block, push the address of this block on a stack. • When an exception is thrown, pop the try block that is at the top of the stack (if the stack is empty, terminate). • If the popped try block has no matching catch block, go back to the preceding step. • If the popped try block has a matching catch block, execute the matching catch block.

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

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

  19. Rat In A Maze • Move down. Rat In A Maze • Move left.

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

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

  22. Rat In A Maze • Move downward. Rat In A Maze • Move right.

  23. Rat In A Maze • Move one down and then right. Rat In A Maze • Move one up and then right.

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend