DSA Unit 4 Stack
DSA Unit 4 Stack
What is Stack
Characteristics of a stack
examples of stack
Initially
top=-1 0 1 2 3 4 5…………………. 99
Way II: Use structure:
Struct stack
{
int s[100];
int top;
}st;
Struct stack
{
int data
struct stack * next;
}node;
Representation of stack using linked list
top
Isempty(): if (top==NULL)
Isfull(): Not Applicable for LL implementation
40
Push(): //Similar to insert_beg() of SLL
-if isempty() then top=current_node 30
-else insert_beg nod
Pop(): //Similar to delete_beg() of SLLe 20
-if isempty() then don’t call pop()
-else if last node then top=NULL 10
10 NULL
Null
-else delete_beg()
Stack using linked list
Implemetation of stack using linked list
Implicit and explicit stack
Implicit stack: built-in , used in function call and return,
recursion etc.
Explicit stack: push-pop functions are called by user, eg-
assembly language
Stack Applications
Expression conversion
Expression Evaluation
Reversing a string
Tree traversal( inorder, preorder, postorder)
Graph traversal ( DFS)
Checking if expression contains balanced parenthesis or
not
Undo button, Back button of browser
Expressions:
Expression is a string of operands and operators.
Operands are some numeric values.
Operators are of two types unary and binary
Unary operators are + and -.
Binary operators are =,-,+,/,*
Types of Expressions:
There are 3 types
1.Infix Expression.
2.Postfix Expression
3.Prefix Expression
1.Infix Expression.
Here the arrangement of operators is as follows
Infix Expression=op1 operator op2
For Ex.
(a + b)
(a + b)*(c - d)
(a + b / e)* (d + f)
Ex.
a b+
ab+cd-*
a b e /+ d f + *
In postfix expression parenthesis are not used
3.Prefix Expression
a b e /+ d f + *
* +a/be+df
Convert A*B+(C-D/E)
postfix and prefix
Postfix: AB*CDE/-+
Prifix: +*AB-C/DE
Conversion from Infix to postfix
Expression.
When we want to convert expressions the most important thing we should know
priorities of the operators.
This helps in determining the order of evaluation of operands.
Operator Precedence in
Expression
^ 3
*,/ 2
+,- 1
( 4
Rules for Converting Infix to Postfix:
* * A
B * AB
+ POP * and PUSH AB*
C + AB*C
$ empty AB*C+
(a + b ) * (c - d) $
Answer a b + c d - *
Conversion of infix to postfix
Test above algorithm for following expression
Given infix expression is
A*B+(C-D/E)
Postfix expression is
AB*CDE/-+
Conversion of infix to postfix
Test above algorithm for following expression
Given infix expression is
A*B+(C/D-E)
Postfix expression is
AB*CD/E-+
Algorithm for evaluation of postfix
expression
1.Read the postfix expression character by character from left to right.
2.If character represents operand, then push it onto the stack
3.Otherwise if it is an operator, pop two operands as op2 & op1 and
perform arithmetic operations.
+ then result = op1 + op2
- then result = op1 – op2
* then result = op1 * op2
/ then result= op1 / op2
4.Push the result onto the stack
5.Repeat step 1-4 till the postfix expression is not over.
6.Pop the result from the top of the stack and print it.
Evaluation of postfix expression
Evaluate following postfix expression using above algorithm
5+2*3==523*+
Answer=11
623+-382/+*2^3+
Answer is 52
Evaluation of postfix expression
Evaluate following postfix expression using above algorithm
248+-8+
Answer: -2
234*+
Answer: 14
Conversion of infix to prefix
Algorithm:
i. Take s which is stack contains operators
ii. p[ ] character string which holds result
iii. infix[ ] string containing expression
iv. n= strlen(infix)
v. n=n-1, k=n points to last character
vi. If i/p is an operand copy it in p
p[k]= infix[n];
k- - ; n- -;
vii. If infix[n] is operator then
a.) if s is empty push operator in s
b.)if s is not empty then check whether priority of current
operator is greater than that of operator on top of stack, if so
push this operator onto s else
c.) pop operator from stack and place them in p till current
operators priority becomes greater than the operator on top of
the stack or stack become empty and push current operator
onto stack
viii.If n>=0 repeat step vi.
ix. Pop all the elements of s and place it in p.
x. Print p
xi. Stop.
Conversion of infix to prefix A*B+C
OUTPUT: +*ABC
Infix: A*B+(C-D/E)
Answer: +*AB-C/DE