Understanding Stack Operations and Implementation
Understanding Stack Operations and Implementation
Example:
Unit-3 Stack
Prepared By:
Prof. Vishal A. Polara
Assistant Professor
Information Technology Department
Birla Vishvakarma Mahavidyalaya Engineering College
1
Unit-3 Stack 3/16/2022
Algorithm Algorithm
Step-1: [Initialization]
Step-1: [Initialization] Initialization or declare int a[10]
Initialization or declare int a[10] Variable i, n, TOP=-1, choice
Variable i, n, TOP=-1, choice Set i LB 0
Set i LB 0
Step–2: Get the size of stack from user n X value.
Step–3: Repeat step–3 while(i < n)
Step–2: Get the size of stack from user n X value.
Increase TOP TOP + 1
Step–3: Repeat step–3 while (choice != ‘n’) Assign value a[TOP] X value
Get PUSH value from user X value Increase i i + 1
Check TOP value if (TOP < n) Step–4: Repeat step–3 while (choice != ‘n’)
Increase TOP TOP + 1 If(TOP == -1)
Assign value a[TOP] X printf(“Stack Underflow”);
If (TOP>=n) break;
7 Prof. Vishal A. Polara 10 Prof. Vishal A. Polara
printf(“Stack Overflow”);
break;
Get the value for choice from user Otherwise
Print POP value a[TOP]
Step–4: Print value of stack after PUSH operation. Decrease TOP TOP-1
Set i LB 0 Get the value for choice
Print X value a[i] from stack. Step–5: Print value of stack after POP operation.
Increase i i + 1 up to TOP Set i LB 0
Step–5: Finished. Print X value a[i] from stack.
Exit Increase i i + 1 up to TOP
Step–6: Finished.
Exit
2
Unit-3 Stack 3/16/2022
TOP. If TOP = NULL, then it indicates that the stack is empty. SET TOP = NEW_NODE
[END OF IF]
It also support all operation of stack
Step 4: END
Pop Operation
Create node in linked list The pop operation is used to delete the topmost element
from a stack.
Struct node However, before deleting the value, we must first check
{ if TOP=NULL, because if this is the case, then it means
that the stack is empty and no more deletions can be
Int data;
done.
Struct node *next(link); If an attempt is made to delete a value from a stack that
}; is already empty, an UNDERFLOW message is printed.
Struct node *top = NULL In case TOP!=NULL, then we will delete the node
pointed by TOP, and make TOP point to the second
element of the linked stack.
3
Unit-3 Stack 3/16/2022
Polish Expression
Void pop() Arithmetic expressions are normally written in infix
{ notation. It is called infix because the arithmetic
struct node *temp; operator (i.e. +, -, *, /) is in-between the operands.
if(top==NULL) It is difficult to develop an algorithm to evaluate infix
printf(“No node or element to delete”) expressions due to precedence problems. You cannot
else simply evaluate an expression straight left to right!
temp=top the task of the compiler would be much easier. If we
could evaluate an arithmetic expression by simply going
printf(“%d”,temp->data)
straight left to right.
top=top->next(link)
It is possible by Transforming the expression into a form
temp-> next(link)=NULL
called Polish notation.
free(temp)
The process of writing the operators of an expression
} either before their operands or after operands are called
polish notation.
20 Prof. Vishal A. Polara 23 Prof. Vishal A. Polara
4
Unit-3 Stack 3/16/2022
5
Unit-3 Stack 3/16/2022
Algorithm
o If a right parenthesis is the current symbol, pop
the stack down to (and including) the first left
parenthesis. Write all the symbols except the
left parenthesis to the output
2) If token is greater(precedence), push onto the
operator stack
3) If token is equal (precedence) use the lesser
rule
o Comparing against an empty operator stack
will always result in a push onto the operator
stack
(A-(B/C+(D%E*F)/G)*H)
while(![Link]())
{
res=res + [Link]()
[Link]()
}
Return res
}
6
Unit-3 Stack 3/16/2022
Steps for infix to prefix using stack Evaluation of postfix using stack
Requires usage of an operator stack and prefix string.
Steps: 23*54*+9- Postfix
• If token is an operand, push onto prefix string. Evalpostfix(exp)
• If token is an operator, compare this with the top of the {
operator stack Create a stack s
1) If token is lesser (precedence), pop the operator stack and For i=0 to length(exp)-1
push onto prefix string then revaluate. {
o An incoming right parenthesis will be considered to have if(exp(i) is operand)
higher priority than any other symbol. A right parenthesis push(exp[i])
on the stack will not be removed unless an incoming left elseif(exp[i] is operator)
parenthesis is found. op2=pop()
o op1=pop()
res=perform(exp[i],op1,op2)
39 Prof. Vishal A. Polara 42 Prof. Vishal A. Polara push(res)
7
Unit-3 Stack 3/16/2022
res=perform(exp[i],op1,op2)
43 Prof. Vishal A. Polara push(res) 46 Prof. Vishal A. Polara
8
Unit-3 Stack 3/16/2022
Tower of Hanoi
Analysis of factorial using recursion The tower of Hanoi is one of the main applications of
recursion. It was invented in 1883 by French mathematician
Step 1: Specify the base case which will stop the lucas for 64 disk.
function from making a call to itself. For example: The problem is to move all these rings show in
Step 2: Check to see whether the current value being
fig. from pole A to pole C while maintaining the same order.
There are two rules:
processed matches with the value of the base case. If
yes, process and return the value. 1) only one disk move at a time
2) The smaller disk must always come above the larger disk.
Step 3: Divide the problem into smaller or simpler sub-
Solution:
problems.
Base case: if n=1
Step 4: Call the function from each sub-problem. Move the ring from A to C using B as spare
Step 5: Combine the results of the sub-problems. Recursive case:
Step 6: Return the result of the entire problem. Move n – 1 rings from A to B using C as spare
Move the one ring left on A to C using B as spare
50 Prof. Vishal A. Polara 53 Prof.
Move n – 1 rings from B to C using A as spare
Vishal A. Polara
9
Unit-3 Stack 3/16/2022
10
Unit-3 Stack 3/16/2022
11
Unit-3 Stack 3/16/2022
Thank You
12