stack of cups stacks
play

Stack Of Cups Stacks top F top E E D D C C Linear list. - PDF document

Stack Of Cups Stacks top F top E E D D C C Linear list. B B One end is called top. bottom bottom A A Other end is called bottom. Additions to and removals from the top end Add a cup to the stack. only.


  1. Stack Of Cups Stacks top F top E E D D C C • Linear list. B B • One end is called top. bottom bottom A A • Other end is called bottom. • Additions to and removals from the top end • Add a cup to the stack. only. • Remove a cup from new stack. • A stack is a LIFO list. The Interface Stack Parentheses Matching • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) public interface Stack – 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) public boolean empty(); • (a+b))*((c+d) public Object peek(); – (0,4) public void push(Object theObject); – right parenthesis at 5 has no matching left parenthesis public Object pop(); – (8,12) } – left parenthesis at 7 has no matching right parenthesis Parentheses Matching Example • scan expression from left to right • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) • when a left parenthesis is encountered, add its position to the stack • when a right parenthesis is encountered, remove matching position from stack 2 1 0

  2. Example Example • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 21 15 1 1 0 (2,6) (1,13) 0 (2,6) (1,13) (15,19) Example Example • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) • (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n) 27 1 1 0 (2,6) (1,13) (15,19) (21,25) 0 (2,6) (1,13) (15,19) (21,25)(27,31) (0,32) • and so on Towers Of Hanoi/Brahma Towers Of Hanoi/Brahma 4 3 3 2 2 1 1 A B C A B C • 64 gold disks to be moved from tower A to tower C • 3-disk Towers Of Hanoi/Brahma • each tower operates as a stack • cannot place big disk on top of a smaller one

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

  4. Towers Of Hanoi/Brahma Recursive Solution 3 2 1 1 A B C A B C • 3-disk Towers Of Hanoi/Brahma • n > 0 gold disks to be moved from A to C using B • 7 disk moves • move top n-1 disks from A to B using C Recursive Solution Recursive Solution 1 1 A B C A B C • move top disk from A to C • move top n-1 disks from B to C using A Recursive Solution 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. 1 A B C • moves(n) = 0 when n = 0 • moves(n) = 2*moves(n-1) + 1 = 2 n -1 when n > 0

  5. Chess Story Chess Story • 1 grain of rice on the first square, 2 for next, 4 for • 1 penny for the first square, 2 for next, 4 for next, next, 8 for next, and so on. 8 for next, and so on. • $3.6 * 10 17 (federal budget ~ $2 * 10 12 ) . • Surface area needed exceeds surface area of earth. Switch Box Routing Routing A 2-pin Net 1 2 3 4 5 6 7 8 9 10 1 2 3 4 4 5 6 7 8 9 10 40 11 40 11 Routing Routing 39 12 39 12 for pins for pins 5 38 13 38 13 1-3 and through 37 14 18-40 is 37 14 16 is confined 36 15 36 15 Routing region confined to lower 35 16 35 16 to upper left right 34 17 34 17 17 region. region. 33 18 33 18 32 19 32 19 31 20 31 20 30 29 28 27 26 25 24 23 22 21 30 29 28 27 26 25 24 23 22 21 Routing A 2-pin Net Routing A 2-pin Net 1 2 3 4 4 5 6 7 8 9 10 1 2 3 4 4 5 6 7 8 9 10 40 11 40 11 Examine (u,v), Start pin 39 12 39 12 pins in u<v is a => push clock- 38 13 38 13 2-pin onto wise net. 37 14 stack. 37 14 order 36 15 36 15 u is start End pin beginn- pin. 35 16 => start 35 16 ing with pin must pin 1. 34 17 17 34 17 17 v is end be at top pin. 33 18 33 18 of stack. 32 19 32 19 31 20 31 20 30 29 28 27 26 25 24 23 22 21 30 29 28 27 26 25 24 23 22 21

  6. Method Invocation And Return Try-Throw-Catch public void a() • When you enter a try block, push the address of { …; b(); …} this block on a stack. public void b() • When an exception is thrown, pop the try block { …; c(); …} return address in d() that is at the top of the stack (if the stack is empty, return address in c() public void c() terminate). return address in e() { …; d(); …} • If the popped try block has no matching catch return address in d() block, go back to the preceding step. public void d() return address in c() • If the popped try block has a matching catch { …; e(); …} return address in b() block, execute the matching catch block. public void e() return address in a() { …; c(); …} Rat In A Maze Rat In A Maze • Move order is: right, down, left, up • Block positions to avoid revisit. Rat In A Maze Rat In A Maze • Move order is: right, down, left, up • Move backward until we reach a square from which • Block positions to avoid revisit. a forward move is possible.

  7. Rat In A Maze Rat In A Maze • Move down. • Move left. Rat In A Maze Rat In A Maze • Move down. • Move backward until we reach a square from which a forward move is possible. Rat In A Maze Rat In A Maze • Move backward until we reach a square from which • Move right. a forward move is possible. • Backtrack. • Move downward.

  8. Rat In A Maze Rat In A Maze • Move downward. • Move right. Rat In A Maze Rat In A Maze • Move one down and then right. • 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.

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