You are on page 1of 40

Unit-2 Linear Data Structure-Stack

 Outline:
• Stack Definition and Concept
• Operations on Stack( Push, Pop, Peep, Change)
• Application of Stack
1.Expression Conversion(Infix, Postfix(Reverse Polish, Prefix(Polish)
2.Expression Evaluation.
3.Recursion.
• Tower of Hanoi.
Unit-2 Linear Data Structure-Stack
 Stack Definition and Concept:
• A linear list which allow insertion and deletion of an element at one end only is called
Stack.
• A Stack is a data structure which is used to store data in a particular order.
• It follows Last In First Out(LIFO) Order.
• The element which is inserted last, that will be come out first.
• There are many real-life examples of a stack.
• Consider an example of plates stacked over one another in the canteen.
Unit-2 Linear Data Structure-Stack
 Operations on Stack:
• There are four operations which we can perform on stack.
1. Push(Insert): Which insert an element at TOP of the stack.
2. Pop(Delete): Which Remove the most recently added elements from the
TOP of the stack.
3. Peep(Search): To find ith element from TOP of the stack.
4. Change(Update):Changes the value of the ith element from the TOP of the stack.
Unit-2 Linear Data Structure-Stack
 Operations on Stack:
• TOP is a pointer that points to the top element of stack.
• Initially when the Stack is Empty, TOP has a value of “Zero”.
• Each time a new element is inserted in the stack, the pointer is Incremented by “one”.
• The pointer is decremented by “one” each time a deletion is made from the stack
Unit-2 Linear Data Structure-Stack
 Procedure: PUSH(S,TOP,X)
• This procedure insert an element X to the top of a stack.
• Stack is represented by a vector S Containing N Elements.
• A pointer TOP represents the top element of the stack.
1. [Check for Stack overflow] Stack is Empty, TOP=0
If Top ≥ N N=3
then Write (‘Stack Overflow’)
PUSH(S,TOP,10) ,TOP=1
Return PUSH(S,TOP,15),TOP=2
2. [Increment Top] 30 TOP=3
PUSH(S,TOP,30),TOP=3
Top ← Top + 1 PUSH(S,TOP,40),
3. [Insert Element] TOP=2 15 15
Overflow
S[Top] ← X
TOP=1 10 10 10
4. [Finished]
Return
S S S
Unit-2 Linear Data Structure-Stack
 Procedure: POP(S,TOP)
• This procedure remove and returns the
top element from a stack.
• Stack is represented by a vector S Containing N Elements.
Stack is FULL, TOP=3,
• A pointer TOP represents the top element of the stack. N=3
1. [Check for underflow on Stack] POP(S,TOP),TOP=3,
If Top = 0 TOP=2
then Write (‘Stack Underflow on Pop’) Return S[2+1]
Exit POP(S,TOP),TOP=2, TOP=3 30
2. [Decrement Pointer] TOP=1
Top ← Top-1 Return S[1+1] 15 TOP=2 15
3. [Return former top element of POP(S,TOP),TOP=1
Stack] TOP=0 10 10 TOP=1 10
Return S[0+1]
Return (S[Top+1])
POP(S,TOP),TOP=0
Underflow S S S
Unit-2 Linear Data Structure-Stack
 Procedure: PEEP(S,TOP,I)
• This procedure returns the value of ith element from
top of the stack.
• The element is not deleted by this function.
• Stack is represented by a vector S Containing N Elements.
• A pointer TOP represents the top element of the stack.
1. [Check for Stack Underflow] Stack is FULL, TOP=5,
If (Top - I + 1) ≤ 0 N=5 TOP=5 50
then Write (‘Stack Underflow on 40
PEEP(S,TOP,4)= 5 – 4 + 1=2=S[2]=20
Peep’) PEEP(S,TOP,2)= 5 – 2 + 1=4=S[4]=40
30
Exit PEEP(S,TOP,6)= 5 – 6 + 1=0=S[0]=Underflow 20
2. [Return Ith element from top 10
of the stack]
S
Return (S[Top-I+1])
Unit-2 Linear Data Structure-Stack
 Procedure: CHANGE(S,TOP,X,I)
• This procedure changes the value of ith element from the top of
the stack to the value contained in X.
• Stack is represented by a vector S Containing N Elements.
• A pointer TOP represents the top element of the stack
1. [Check for stack underflow]
If (Top - I + 1) ≤ 0 Stack is FULL, TOP=5,
then Write (‘Stack Underflow on Change’) N=5
TOP=5 50 50
Exit
2. [Change the Ith element from top of stack] 40 70
CHANGE(S,TOP,60,4)= 5 – 4 + 1=2=S[2]=60
S[Top - I + 1] ← X 30 30
CHANGE(S,TOP,70,2)= 5 – 2 + 1=4=S[4]=70
3. [Finished] CHANGE(S,TOP,6)= 5 – 6 + 1=0=S[0]=Underflow 20 60
Return 10 10

S S
Unit-2 Linear Data Structure-Stack
 Application of Stack:
1. Expression Conversion: An expression is a collection of
operators and operands that represents a specific value.
• In above definition, operator is a symbol which performs a particular task like arithmetic
operation.
• Operands are the values on which the operators can perform the task.
• For Example, A + B * C
• An arithmetic expression can be written in three different but equivalent notations, i.e.,
without changing the essence or output of an expression.
• These notations are −
1. Infix Notation
2. Prefix (Polish) Notation
3. Postfix (Reverse-Polish) Notation
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
1. Infix Notation: We write expression in infix notation, e.g. a + b,
where operators are used in-between operands.
2. Prefix Notation: In this notation, operator is prefixed to operands, i.e. operator is written
ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix
notation is also known as Polish Notation.
3. Postfix Notation: This notation style is known as Reversed Polish Notation(RPN). In this
notation style, the operator is post fixed to the operands i.e., the operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.
Infix =a+b
Prefix = +ab
Postfix = ab+
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
 Precedence and Associativity:
• When an operand is in between two different operators, which operator will take the operand
first, is decided by the precedence of an operator over others. For example −

• As multiplication operation has precedence over addition, b * c will be evaluated first.


• Associativity describes the rule where operators with the same precedence appear in an
expression.
• For example, in expression a + b − c, both + and – have the same precedence, then which part
of the expression will be evaluated first, is determined by associativity of those operators.
• Here, both + and − are left associative, so the expression will be evaluated as (a + b) − c.
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
 Precedence and Associativity:
• Precedence and associativity determines the order of evaluation of an expression. Following is
an operator precedence and associativity table (highest to lowest) −
Sr.No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

Multiplication ( ∗ ) &
2 Second Highest Left Associative
Division ( / )

Addition ( + ) &
3 Lowest Left Associative
Subtraction ( − )
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
 Infix to postfix conversion: There are two algorithms for that.
1. UNPARENTHESIZED_SUFFIX(Without Bracket).
e.g. a + b * c - d / e

2. REVPOL(With Bracket)
e.g. a * (b + c) - d
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
1. Algorithm UNPARENTHESIZED_SUFFIX:
This algorithm converts the string INFIX to its reverse Polish string equivalent(POLISH).
INFIX: Input string INFIX representing an infix expression whose single character symbols have
precedence values and ranks as given in Table.
S: vector S representing a stack.
NEXTCHAR: String function NEXTCHAR which, when invoked, returns the next character of the
input string.
RANK: RANK contains the value of each head of the reverse Polish string.
NEXT: NEXT contains the symbol being examined.
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
Algorithm UNPARENTHESIZED_SUFFIX:
TEMP:TEMP is a temporary variable which contains the unstacked element.
POLISH: which contain final output string(postfix form)
We assume that the given input string is padded on the right with the special symbol “#”.
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
Algorithm UNPARENTHESIZED_SUFFIX:
1. [Initialize the stack] 6. [Push current symbol onto stack and obtain next input symbol]
Top ← 1 Call Push (S, Top, NEXT)
S[Top] ← ‘#’ NEXT ← NEXTCHAR (INFIX)
2. [Initialize output string and rank count] 7. [Remove remaining elements from stack]
POLISH ← ‘’ Repeat while S[Top] ≠ ‘#’
RANK ← 0 TEMP ← Pop (S, Top)
3. [Get first input symbol] POLISH ← POLISH O TEMP
NEXT ← NEXTCHAR(INFIX) RANK ← RANK + r(TEMP)
4. [Translate the infix expression] If RANK < 1
Repeat thru step 6 while NEXT ≠ ‘#’ then Write (‘INVALID’)
5. [Remove symbols with greater or equal precedence from stack] Exit
Repeat while f(NEXT) ≤ f(S[Top]) 8. [Is the expression valid?]
TEMP ← Pop(S, Top) (this copies the stack contents into TEMP) If RANK = 1
POLISH ← POLISH O TEMP then Write (‘VALID’)
RANK ← RANK + r(TEMP) Else Write (‘INVALID’)
If RANK < 1 Exit
then Write (‘INVALID’)
Exit
Unit-2 Linear Data Structure-Stack
 Example:
A+B*C–D
INFIX: A + B * C – D #
Input Content Reverse Polish Rank
Symbol of Stack Expression(POLISH)
(NEXT)
# 0
1. [Initialize the stack]
Top ← 1 A
S[Top] ← ‘#’
2. [Initialize output string
and rank count]
POLISH ← ‘’
RANK ← 0
3. [Get first input symbol]
NEXT ← NEXTCHAR(INFIX)
Unit-2 Linear Data Structure-Stack
 Example:
A+B*C–D Input Content Reverse Polish Rank
INFIX: A + B * C – D # Symbol of Stack Expression(POLISH)
(NEXT)
4. [Translate the infix expression]
# 0
Repeat thru step 6 while NEXT ≠
‘#’ A (3≤0) #A 0
5. [Remove symbols with greater
or equal precedence from stack] + (1≤3) #+ A 1
Repeat while f(NEXT) ≤ f(S[Top]) (1≤0)
TEMP ← Pop(S, Top) (this copies
the stack contents into TEMP)
B (3≤1) #+B A 1
POLISH ← POLISH O TEMP * (2≤3) #+* AB 2
RANK ← RANK + r(TEMP) (2≤1)
If RANK < 1
then Write (‘INVALID’) C (3≤2) #+*C AB 2
Exit
6. [Push current symbol onto
- (1≤3) #- ABC*+ 1
stack and obtain next input (1≤2)
symbol] (1≤1)
Call Push (S, Top, NEXT)
NEXT ← NEXTCHAR (INFIX)
D(3≤1) #-D ABC*+
#
Unit-2 Linear Data Structure-Stack
 Example:
A+B*C–D Input Conte Reverse Polish Rank
INFIX: A + B * C – D # Symbol nt of Expression(POLISH)
(NEXT) Stack
7. [Remove remaining # 0
elements from stack]
Repeat while S[Top] ≠ ‘#’ A (3≤0) #A 0
TEMP ← Pop (S, Top) + (1≤3) #+ A 1
POLISH ← POLISH O TEMP (1≤0)
RANK ← RANK + r(TEMP) B (3≤1) #+B A 1
If RANK < 1
then Write (‘INVALID’) * (2≤3) #+* AB 2
Exit (2≤1)
8. [Is the expression valid?] C (3≤2) #+*C AB 2
If RANK = 1 - (1≤3) #- ABC*+ 1
then Write (‘VALID’) (1≤2)
Else Write (‘INVALID’) (1≤1)
Exit
D (3≤1) #-D ABC*+ 1
# # ABC*+D- 1
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
2. Algorithm REVPOL:
• This algorithm converts INFIX into reverse polish and places the result into POLISH.
• Given an input string INFIX containing an infix expression which has been padded on the right
with ‘)’.
• All Symbol have precedence value given in table.
• Stack is represented by vector S, TOP denotes the top of the stack, Algorithm PUSH and POP
are used for stack manipulation.
• Function NEXTCHAR returns the next symbol from given input string.
• The integer variable RANK contains the rank of Expression.
• The string variable TEMP is used for temporary storage purpose.
Unit-2 Linear Data Structure-Stack
 Expression Conversion:
2. Algorithm REVPOL:
Unit-2 Linear Data Structure-Stack
2. Algorithm REVPOL:
1. [Initialize stack] 4. [Translate the infix expression]
Top ← 1 Repeat thru step 7 while NEXT ≠ ‘’
S[Top] ← ‘(‘ 5. [Remove symbols with greater
precedence from stack]
2. [Initialize output string and rank
If Top < 1 8. [Is the expression valid?]
count] then Write (‘INVALID’)
POLISH ← ‘’ If Top ≠ 0 or RANK ≠ 1
Exit then Write (‘INVALID’)
RANK ← 0 Repeat while f(NEXT) < g(S[TOP])
3. [Get first input symbol] TEMP ← Pop (S, Top)
else Write (‘VALID’)
NEXT ← NEXTCHAR (INFIX) POLISH ← POLISH O TEMP Exit
RANK ← RANK + r(TEMP)
If RANK < 1
then Write (‘INVALID’)
Exit
6. [Are there matching parentheses?]
If f(NEXT) ≠ g(S[Top])
then Call Push (S, Top, NEXT)
else Pop (S, Top)
7. [Get next input symbol]
NEXT ← NEXTCHAR (INFIX)
Unit-2 Linear Data Structure-Stack
 Example:

A * (B + C ) – D Input Content Reverse Polish Rank


INFIX: A * (B + C) – D ) Symbol of Stack Expression(POLISH)
(NEXT)
1. [Initialize stack] ( 0
Top ← 1
S[Top] ← ‘(‘ A
2. [Initialize output string
and rank count]
POLISH ← ‘’
RANK ← 0
3. [Get first input symbol]
NEXT ← NEXTCHAR (INFIX)
Unit-2 Linear Data Structure-Stack
Input Content Reverse Polish Rank
Symbol of Stack Expression(POLISH)
 Example:
(NEXT)
A * (B + C ) – D
( 0
INFIX: A * (B + C) – D )
A (7<0) (A
4. [Translate the infix expression]
Repeat thru step 7 while NEXT ≠ ‘’ * (3<8) (* A 1
5. [Remove symbols with greater (3<0)
precedence from stack]
( (9<4) (*( A 1
If Top < 1
then Write (‘INVALID’) B (7<0) (*(B A 1
Exit
Repeat while f(NEXT) < g(S[TOP]) + (1<8) (*(+ AB 2
TEMP ← Pop (S, Top) (1<0)
POLISH ← POLISH O TEMP
C (7<2) (*(+C AB 2
RANK ← RANK + r(TEMP)
If RANK < 1 ) (0<8) (* ABC+ 2
then Write (‘INVALID’) (0<2)
Exit (0<0)
6. [Are there matching
parentheses?] - (1<4) (- ABC+* 1
If f(NEXT) ≠ g(S[Top]) (1<0)
then Call Push (S, Top, NEXT) D (7<2) (-D ABC+* 1
else Pop (S, Top)
7. [Get next input symbol] ) (0<8) ABC+ *D- 1
NEXT ← NEXTCHAR (INFIX) (0<2)
(0<0)
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Postfix
• Given an input string POSTFIX represent postfix expression.
• This algorithm evaluates postfix expression and put the result into variable
Answer.
• Stack is represent by vector S, TOP denotes the top of the stack, Algorithm PUSH
and POP are used for stack manipulation.
• Function NEXTCHAR returns the next symbol from given input string.
• OP1,OP2 and TEMP are used for temporary storage purpose.
• PERFORM_OPERATION is a function which perform required operation on OP1
and OP2.
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Postfix
1. [ Initialize Stack ]
Top ← 0
Answer  0
2. [ Evaluate the postfix expression ]
Repeat until last character
TEMP NEXTCHAR (POSTFIX)
If TEMP is OPERAND
Then PUSH(S,TOP,TEMP)
Else
OP2  POP(S,TOP)
OP1  POP(S,TOP)
Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
PUSH(S, TOP, Answer)
3. [Return Answer from Stack]
Return (POP(S,TOP))
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Postfix Example
POSTFIX: 9 3 4 * 8 + 4 / -
TEMP OP1 OP2 Answer Stack
9 9
1. [ Initialize Stack ]
3 9,3 Top ← 0
Answer  0
4 9,3,4 2. [ Evaluate the postfix expression ]
Repeat until last character
* 3 4 12 9,12 TEMP NEXTCHAR (POSTFIX)
If TEMP is OPERAND
8 9,12,8 Then PUSH(S,TOP,TEMP)
+ 12 8 20 9,20 Else
OP2  POP(S,TOP)
4 9,20,4 OP1  POP(S,TOP)
Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
/ 20 4 5 9,5 PUSH(S, TOP, Answer)
3. [Return Answer from Stack]
- 9 5 4 4 Return (POP(S,TOP))
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Postfix Example
POSTFIX: 5 4 6 + * 4 9 3 / + *
TEMP OP1 OP2 Answer Stack
5 5
4 5,4
6 5,4,6
1. [ Initialize Stack ]
+ 4 6 10 5,10 Top ← 0
Answer  0
* 5 10 50 50 2. [ Evaluate the postfix expression ]
4 50,4 Repeat until last character
TEMP NEXTCHAR (POSTFIX)
9 50,4,9 If TEMP is OPERAND
Then PUSH(S,TOP,TEMP)
3 50,4,9,3 Else
OP2  POP(S,TOP)
/ 9 3 3 50,4,3 OP1  POP(S,TOP)
+ 4 3 7 50,7 Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
PUSH(S, TOP, Answer)
* 50 7 350 350 3. [Return Answer from Stack]
Return (POP(S,TOP))
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Prefix
• Given an input string PREFIX represent prefix expression.
• This algorithm evaluates prefix expression and put the result into variable
Answer.
• Stack is represent by vector S, TOP denotes the top of the stack, Algorithm PUSH
and POP are used for stack manipulation.
• Function NEXTCHAR returns the next symbol from given input string.
• OP1,OP2 and TEMP are used for temporary storage purpose.
• PERFORM_OPERATION is a function which perform required operation on OP1
and OP2.
Introduction to Data Structure
2. Expression Evaluation:
 Algorithm Evaluate_Prefix
1. [ Initialize Stack ]
Top ← 0
Answer  0
2. [ Evaluate the prefix expression ]
Repeat from last character up to First
TEMP NEXTCHAR (PREFIX)
If TEMP is OPERAND
Then PUSH(S,TOP,TEMP)
Else
OP1  POP(S,TOP)
OP2 POP(S,TOP)
Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
PUSH(S, TOP, Answer)
3. [Return Answer from Stack]
Return (POP(S,TOP))
Introduction to Data Structure
 Example 1:Prefix Evaluation
PREFIX: - 9 / + * 3 4 8 4

TEMP OP1 OP2 Answer Stack


4 4
8 4,8
1. [ Initialize Stack ]
4 4,8,4 Top ← 0
Answer  0
3 4,8,4,3 2. [ Evaluate the prefix expression ]
Repeat from last character up to First
* 3 4 12 4,8,12 TEMP NEXTCHAR (PREFIX)
If TEMP is OPERAND
+ 12 8 20 4,20 Then PUSH(S,TOP,TEMP)
Else
/ 20 4 5 5 OP1  POP(S,TOP)
OP2  POP(S,TOP)
9 5,9 Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
PUSH(S, TOP, Answer)
3. [Return Answer from Stack]
- 9 5 4 4 Return (POP(S,TOP))
Introduction to Data Structure
 Example 2:Prefix Evaluation
PREFIX: + * A B – C + C * B A (A= 4, B=8,C=12)
+ * 4 8 – 12 + 12 * 8 4
TEMP OP1 OP2 Answer Stack
4 4
8 4,8
* 8 4 32 32
1. [ Initialize Stack ]
12 32,12 Top ← 0
Answer  0
+ 12 32 44 44 2. [ Evaluate the prefix expression ]
Repeat from last character up to First
12 44,12
TEMP NEXTCHAR (PREFIX)
- 12 32 -20 -20 If TEMP is OPERAND
Then PUSH(S,TOP,TEMP)
8 -20,8 Else
OP1  POP(S,TOP)
4 -20,8,4 OP2  POP(S,TOP)
Answer  PERFORM_OPERATION(OP1,OP2,TEMP)
* 4 8 32 -20,32 PUSH(S, TOP, Answer)
+ 32 -20 12 12 3. [Return Answer from Stack]
Return (POP(S,TOP))
Unit-2 Linear Data Structure-Stack
 Example: infix to postfix
A+B*C–D Infix

A+B*C -D

= A + T1 - D , T1 = BC*

= T2 - D ,T2 = AT1+
= T3 ,T3 = T2D-
= T2 D-
= AT1+ D -
=ABC*+D- Postfix
Unit-2 Linear Data Structure-Stack
 Example: Infix to postfix
A * (B + C ) – D Infix

A * (B + C )- D

= A * T1 - D , T1 = BC+

= T2 - D ,T2 = AT1*
= T3 ,T3 = T2D-
= T2D-
= AT1* D -

=ABC+*D- Postfix
Unit-2 Linear Data Structure-Stack
 Example: postfix to infix
ABC*+D- Postfix

ABC*+D–

= A T1 + D - ,T1=B * C

= T2 D – ,T2=A + T1

= T3 , T3=T2 - D
= T2 - D
= A + T1 – D
=A+B*C-D Infix
Unit-2 Linear Data Structure-Stack
 Example: infix to prefix
A+B*C–D Infix

A+B*C -D

= A + T1 - D , T1 = *BC

= T2 - D ,T2 = +AT1
= T3 ,T3 = -T2D
= -T2D
= - +A T1 D

=-+A*B C D Prefix
Unit-2 Linear Data Structure-Stack
 Example: prefix to infix
-+A*BC D Prefix

-+A*BC D

= - + A T1 D ,T1= B*C

= - T2 D ,T2= A + T1

= T3 , T3= T2 – D
= T2 – D
= A + T1 – D
= A + B * C -D Infix
Unit-2 Linear Data Structure-Stack
 Example:Transform the following expression to postfix(reverse polish)and evaluate postfix
expression by assuming A=1,B=2,C=3,D=4,E=6,F=6,G=1,I=3 and J=3
A + B - C *D / E +F $ G / (I+J) Infix
A + B - C * D / E + F $ G / (I+J)
= A + B - C * D / E + F $ G / T1 T1= I J +
= A + B - C * D / E + T2 / T1 T2= F G $
= A + B - T3 / E + T2 / T1 T3=C D *
= A + B - T4 + T2 / T1 T4=T3 E /
= A + B - T4 + T5 T5=T2 T1 /
= T6 - T4 + T5 T6= A B +
= T7 + T5 T7= T6 T4 –
= T8 T8= T7 T5 +
= T7 T5 +
= T6 T4 – T5 +
= A B + T4 – T5 +
= A B + T4 – T2T1/ +
= A B + T3E/ - T2 T1 / +
= A B + CD* E / - T2 T1 /+
= A B + C D * E / - FG$ T1 / +
= A B + C D * E / - F G $ IJ+ / +
=AB+ CD*E/-FG$ IJ+/+ Postfix
Unit-2 Linear Data Structure-Stack
Tower of Hanoi:
 The Tower of Hanoi is a mathematical game or Puzzle.
 It consists of three rods and a number of disks of different sizes,
which can slide onto any rod.
 The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the
smallest at the top, thus making a conical shape.
 The objective of the puzzle is to move the entire stack to another rod, obeying the following
simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack or on an empty rod.
3. No larger disk may be placed on top of a smaller disk.
 With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to
solve a Tower of Hanoi puzzle is 2n − 1, where n is the number of disks.
Unit-2 Linear Data Structure-Stack
Tower of Hanoi:

You might also like