2. Stack
โข A variable sized data structure in which insertion or deletion
is made just from one end
or
โข A LIFO data structure is called Stack
Operations on Stack
1) Push ( to insert an element into stack)
2) Pop( to take an element out of stack)
The last element inserted is deleted or taken out first and
first element at last
The stack can be implemented using Array as well as Link
List
3. Stack using Arrays
โข The no of elements that can be inserted in a the stack is
the dimension of array. The current position of stack(i.e
its size is known by a pointer called its top.
Algorithm to insert data in stack Push(S,Top, X, N)
1 If Top >= N then
wrtie (โStack fullโ) and exit
2 Top=Top+1
3 S[Top]= X
4 Exit
4. Stack Operations
Algorithm to delete an element from Stack Pop(S, Top)
1 If Top= 0 then
write(โ stack is emptyโ) and exit
2 Pop = S[Top]
3 Top= Top-1
4 Exit
Applications of Stack
i) Recursion
ii) Evaluation of Arithmetic expressions
a) Infix notation
b) prefix notations
c) postfix notations
5. Recursion
โข The calling of a subprogram to itself is called recursions
Examples: Factorial of a number
Fact(N)
1) If N= 0 then
Fact =1
2) Fact= N* Fact(N-1)
Polish Suffix notations
โข It is named after Polish mathematician Jan Lukasiewiez.
The operator symbol is placed before its two operands in
this notations
โข Examples: AB+, CD-, EF*
6. PSN notations
โข The expressions in which operands are preceded by the
operators are called PSN notations or postfix notations.
โข Example: Following are PSN notations
AB+
DE*
ABC*+
โข Convert A+B*C into PSN
Symbol scanned operator Stack PSN
A A
+ + A
B + AB
* +* AB
C +* ABC
$ ABC *+
7. RPN(Q, P)
โข Suppose Q is expression in infix form. This algorithm
covert this expression into equivalent postfix expression P
1) Scan Q from left to right and repeat Steps 2 to 5 for each
element of Q until the end of Expression
2) If an operand is encountered, add it to P
3) If a left parenthesis is encountered, push it onto stack
4) If an operator is encountered then:
a) Repeatedly pop from stack and add to P each operator
which has the same precedence as or higher precedence
than (?)
b) Add (?) to stack
5) If a right parenthesis is encountered then:
a) Repeatedly pop from stack and add to P each operator
until a left parenthesis is encountered
b) remove left parenthesis
6) Exit
8. Quiz 1
โข Q.1 What is difference b/w Stack and Array.
Can a stack be an array and an array can be a
stack?
โข Q.2 Convert following expressions into PSN
((A- (B+C)) *D)^(E+F)
9. Assignment I
Q.2 Write a program that should take infix expression and convert it
into PSN. The program should also evaluate PSN expression.
10. Algorithm for evaluation of PSN
1 Scan the Postfix string from left to right.
2 Initialise an empty stack.
3 Repeat step 4 until the end of string
4 If the scanned character is an operand, Push
it to the stack.
a) If the scanned character is an Operator, pop top
two elements and apply operator.
b) Push result back on top of stack
5 Pop last value from stack
5 Exit.
12. Towers of Hanoi
โข Suppose three Pegs labled A, B and C and Suppose on Peg A
there are placed a finite number n of disks with decreasing size.
โข The objective of game is to move the disks from Peg A to PegC
using Peg B as an intermediate.
โข The rules of game are as:
i) Only one disk may be moved at a time, specifically only the
top disk on any Peg may be moved to any other Peg.
ii) A larger disk can not be placed on a smaller disk
โข Example: For n=3
โข Move top disk from A to C Move top disk from A to C
โข Move top disk from A to B Move top disk from B to A
โข Move top disk from C to B Move top disk from B to C
โข Move top disk from A to C
13. Recursive Solution
1 Move top n-1 disks from A to B
2 Move the top disk from A to C
3 Move the top n-1 disks from B to C
General notation:
Tower(N, Beg, Sec, End)
When N=1
Tower(1, Beg, Sec, End)
When N>1
Tower( N-1 Beg, End, Sec)
Tower(1, Beg, Sec, End) Beg โฆ> End
Tower( N-1, Sec, Beg, End)