stacks linear list. one end is called top. other end is called bottom. additions to and removals...
Post on 22-Dec-2015
214 views
TRANSCRIPT
![Page 1: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/1.jpg)
Stacks
• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end only.
![Page 2: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/2.jpg)
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 3: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/3.jpg)
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 4: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/4.jpg)
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 5: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/5.jpg)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1 2
![Page 6: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/6.jpg)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13)15
![Page 7: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/7.jpg)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
0 1
(2,6) (1,13) (15,19)21
![Page 8: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/8.jpg)
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 9: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/9.jpg)
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 10: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/10.jpg)
Towers Of Hanoi/Brahma
A B C
1234
• 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
![Page 11: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/11.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
123
![Page 12: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/12.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
12
3
![Page 13: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/13.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1 2 3
![Page 14: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/14.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
1 23
![Page 15: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/15.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
123
![Page 16: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/16.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
123
![Page 17: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/17.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
12
3
![Page 18: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/18.jpg)
Towers Of Hanoi/Brahma
• 3-disk Towers Of Hanoi/Brahma
A B C
123
• 7 disk moves
![Page 19: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/19.jpg)
Recursive Solution
A B C
1
• n > 0 gold disks to be moved from A to C using B• move top n-1 disks from A to B using C
![Page 20: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/20.jpg)
Recursive Solution
A B C
1
• move top disk from A to C
![Page 21: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/21.jpg)
Recursive Solution
A B C
1
• move top n-1 disks from B to C using A
![Page 22: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/22.jpg)
Recursive Solution
A B C
1
• moves(n) = 0 when n = 0• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
![Page 23: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/23.jpg)
Towers Of Hanoi/Brahma
• moves(64) = 1.8 * 1019 (approximately)• Performing 109 moves/second, a computer would take about
570 years to complete.• At 1 disk move/min, the monks will take about 3.4 * 1013 years.
![Page 24: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/24.jpg)
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.
![Page 25: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/25.jpg)
Chess Story
• 1 penny for the first square, 2 for next, 4 for next, 8 for next, and so on.
• $3.6 * 1017 (federal budget ~ $2 * 1012) .
![Page 26: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/26.jpg)
Switch Box Routing1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
17
18
19
20
40
39
38
37
36
35
34
33
32
31
Routing region
![Page 27: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/27.jpg)
17
Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
Routing for pins 5 through16 is confined to upper right region.
Routing for pins 1-3 and 18-40 is confined to lower left region.
![Page 28: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/28.jpg)
17
Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
(u,v), u<v is a 2-pin net.
u is start pin.
v is end pin.
Examine pins in clock-wise order beginn-ing with pin 1.
![Page 29: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/29.jpg)
17
Routing A 2-pin Net1 2 3 4 5 6 7 8 9 10
30 29 28 27 26 25 24 23 22 21
11
12
13
14
15
16
18
19
20
40
39
38
37
36
35
34
33
32
31
4
17
Start pin => push onto stack.
End pin => start pin must be at top of stack.
![Page 30: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/30.jpg)
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 31: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/31.jpg)
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.
![Page 32: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/32.jpg)
Rat In A Maze
![Page 33: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/33.jpg)
Rat In A Maze
• Move order is: right, down, left, up
• Block positions to avoid revisit.
![Page 34: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/34.jpg)
Rat In A Maze
• Move order is: right, down, left, up• Block positions to avoid revisit.
![Page 35: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/35.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
![Page 36: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/36.jpg)
Rat In A Maze
• Move down.
![Page 37: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/37.jpg)
Rat In A Maze
• Move left.
![Page 38: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/38.jpg)
Rat In A Maze
• Move down.
![Page 39: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/39.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
![Page 40: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/40.jpg)
Rat In A Maze
• Move backward until we reach a square from which a forward move is possible.
• Move downward.
![Page 41: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/41.jpg)
Rat In A Maze
• Move right.• Backtrack.
![Page 42: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/42.jpg)
Rat In A Maze
• Move downward.
![Page 43: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/43.jpg)
Rat In A Maze
• Move right.
![Page 44: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/44.jpg)
Rat In A Maze
• Move one down and then right.
![Page 45: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/45.jpg)
Rat In A Maze
• Move one up and then right.
![Page 46: Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only](https://reader035.vdocument.in/reader035/viewer/2022062715/56649d815503460f94a657d6/html5/thumbnails/46.jpg)
Rat In A Maze
• Move down to exit and eat cheese.• Path from maze entry to current position operates as a stack.