Download presentation
Presentation is loading. Please wait.
Published byAdam Lang Modified over 9 years ago
2
STACK STACK STACK OPERATIONS STACK OPERATIONS PUSH ALGORITHM PUSH ALGORITHM POP ALGORITHM POP ALGORITHM USES OF STACK USES OF STACK THE TOWER OF HANOI THE TOWER OF HANOI
3
Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be removed Which is the first element to pick up? BACK
4
BASIC STACK OPERATIONS The stack concept is introduced and two basic stack operations are discussed: Push Pop
7
BACK
8
STEP 1: [overflow ?] If Top = N then write “overflow” return [end of if statement] STEP 2: [increment Top] Top:=Top + 1 STEP 3: [insert new element] S[Top]:=X STEP 4: [finished] return S=name of stack X=element to be inserted Top=top pointer N=maximum size of stack BACK
9
STEP 1: [underflow ?] If Top = 0 then write “underflow” return [end of if statement] STEP 2: [delete elements] X:=S[Top] STEP 3: [decrement pointer] Top:=Top-1 STEP 4: [finished] return S=name of stack Top= top pointer X=variable used to store deleted elements BACK
10
Stacks are used for: ◦ Any sort of nesting (such as parentheses) ◦ Evaluating arithmetic expressions (and other sorts of expression) ◦ Implementing function or method calls ◦ Keeping track of previous choices (as in backtracking) ◦ Keeping track of choices yet to be made (as in creating a maze) BACK
11
In the great temple of Brahma in Benares, on a brass plate under the dome that marks the center of the world, there are 64 disks of pure gold that the priests carry one at a time between these diamond needles. According to Brahma's immutable law: No disk may be placed on a smaller disk. In the beginning of the world all 64 disks formed the Tower of Brahma on one needle. Now, however, the process of transfer of the tower from one needle to another is in mid course. When the last disk is finally in place, once again forming the Tower of Brahma but on a different needle, then will come the end of the world and all will turn to dust.
12
◦ GIVEN: three poles ◦ a set of discs on the first pole, discs of different sizes, the smallest disc at the top ◦ GOAL: move all the discs from the left pole to the right one. ◦ CONDITIONS: only one disc may be moved at a time. ◦ A disc can be placed either on an empty pole or on top of a larger disc.
13
INITIAL STATE
14
MOVE1
15
MOVE 2
16
MOVE 3
17
MOVE 4
18
MOVE 5
19
MOVE 6
20
MOVE 7(FINAL STATE) BACK
21
THANKS.. BACK
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.