Professional Documents
Culture Documents
Unit 3-Linear DS - STACK
Unit 3-Linear DS - STACK
UNIT 3
SYLLABUS CONTENT
Stack as an ADT
Operations
Array and Linked List representation of Stack
Applications
Reversing data
Conversion of Infix to prefix and postfix expression
Evaluation of postfix and prefix expressions
Balanced parenthesis, etc.
STACK
A stack is a linear data structure in which all additions and deletions are restricted to one end, called the top. Stacks
are known as the last in–first out (LIFO) data structure.
Imagine these objects are in a glass jar
Simulation: https://yongdanielliang.github.io/animation/web/Stack.html
DR. NEEPA SHAH 4
ARRAY-BASED STACK
…
S
0 1 2 t
DR. NEEPA SHAH 5
Step 1: Start
Step 2: Check if the stack is full or not (top = MAX-1)
If the stack is full,Then print "Stack Overflow“
Step 3: Else, // the stack is not full
Increment top by 1 and Set, a[top] = newElem
Step 4: Stop
Step 1: Start
Step 3: Else, store the value pointed by top in a variable elem=a[top] and decrement top by 1.
Step 1: Start
size ( )
return (top+1)
isEmpty ( )
Step 1: Start
Step 2: if the stack is empty then return 1
Step 3: Else, return 0
Performance
Let n be the number of elements in the stack
The space used is O(n)
Each operation runs in time O(1)
Limitations
The maximum size of the stack must be defined a priori and cannot be changed
Trying to push a new element into a full stack causes an implementation-specific error
APPLICATIONS OF STACK
PARENTHESES MATCHING
Algorithm ParenMatch(X,n):
Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number
Output: true if and only if all the grouping symbols in X match
Let S be an empty stack
for i=0 to n-1 do
if X[i] is an opening grouping symbol then
S.push(X[i])
else if X[i] is a closing grouping symbol then
if S.isEmpty() then
return false {nothing to match with}
if S.pop() does not match the type of X[i] then
return false {wrong type}
if S.isEmpty() then
return true {every symbol matched}
else
return false {some symbols were never matched}
FUNCTION INVOCATION
Program Stack
Program Counter
Stack Frame
METHOD STACK
main() {
To keep track of the chain of active methods with int i = 5;
a stack foo(i); bar
} PC = 1
When a method is called, push a frame containing m=6
on the stack foo(int j) {
int k;
Local variables and return value
k = j+1; foo
Program counter, keeping track of the statement bar(k); PC = 3
being executed } j=5
When a method ends, its frame is popped from bar(int m) { k=6
the stack and control is passed to the method on …
top of the stack }
main
PC = 2
Dr. Neepa Shah
i=5
DR. NEEPA SHAH 16
RECURSION INVOCATION
RECURSION CONTD.
int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
RECURSION CONTD.
RECURSION CONTD.
A problem is that we need parentheses or precedence rules to handle more complicated expressions:
For Example :
a + b * c = (a + b) * c ?
= a + (b * c) ?
Infix notation: a + b
Prefix notation: + a b
Postfix notation: a b +
Other Names
Prefix notation:“Polish notation”
Postfix notation:“Reverse Polish notation” or RPN.
Why
With postfix and prefix notations, parentheses are no longer needed!
POSTFIX EXPRESSION
EXAMPLE
(a + b) * c ab+c* *+abc
a + (b * c) abc*+ +a*bc
postfixVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
DR. NEEPA SHAH 28
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
DR. NEEPA SHAH 29
infixVect
b-c)*d–(e+f)
postfixVect
+ a
(
infixVect
*c)*d–(e+f)
postfixVect
ab
+
(
DR. NEEPA SHAH 31
infixVect
c)*d–(e+f)
postfixVect
- ab+
(
DR. NEEPA SHAH 32
infixVect
)*d–(e+f)
postfixVect
- ab+c
(
DR. NEEPA SHAH 33
infixVect
*d–(e+f)
postfixVect
ab+c-
infixVect
d–(e+f)
postfixVect
ab+c-
*
DR. NEEPA SHAH 35
infixVect
–(e+f)
postfixVect
ab+c-d
*
infixVect
(e+f)
postfixVect
ab+c–d*
-
infixVect
e+f)
postfixVect
(
ab+c–d*
-
infixVect
+f)
postfixVect
( ab+c–d*e
-
DR. NEEPA SHAH 39
infixVect
f)
postfixVect
+
( ab+c–d*e
-
DR. NEEPA SHAH 40
infixVect
)
+ postfixVect
( ab+c–d*ef
-
infixVect
postfixVect
ab+c–d*ef+
-
DR. NEEPA SHAH 42
infixVect
postfixVect
ab+c–d*ef+-
step1: step2:
step3: step4:
step7: step8:
step11: step12:
step15: step16:
/ - +* -*
(3) Delete all parentheses.
ab/c-de*+ac*-
PRACTICE EXAMPLES
Infix Postfix
2+3*4 234*+
a*b+5 ab*5+
(1+2)*7 12+7*
a*b/c ab*c/
(a/(b-c+d))*(e-a)*c abc-d+/ea-*c*
a/b-c+d*e-a*c ab/c-de*+ac*-
DR. NEEPA SHAH 52
x
x+y
(x + y) - z
w * ((x + y) - z)
(2 * a) / ((a + b) * (a - c))
1. First, reverse the given infix expression and scan the expression from left to right.
2. If it is operand, output it.
3. If it is ')', then push it into the stack.
4. If it is '(', pop operators from stack and output them until an opening parenthesis is encountered. Pop and discard the opening parenthesis.
5. If it is an operator
i. If the stack is empty, then push the operator on stack.
ii. If the top of the stack is ')', push the operator on the stack.
iii. If it has higher precedence than the TOP of the stack, push it (the incoming operator) into the stack.
iv. If it has the same precedence with a TOP of the stack, push it into the stack.
v. If it has lower precedence then pop the operator from the stack till it finds the operator of a lower precedence or same precedence
vi. If has the same precedence with the top of the stack and it is ‘^’, then pop the top of the stack till the condition is true. If the condition is not
true, push the ^ operator.
6. When we reach the end of the expression, pop, and print all the operators from the top of the stack.
7. At the end, reverse the output.
Prefix: - + + A B C ^ - D E F + –+ FED-^C
) –+) FED-^C
B –+) FED-^CB
+ –+)+ FED-^CB
A –+)+ FED-^CBA
DR. NEEPA SHAH 56
( –+ FED-^CBA+
FED-^CBA++–
INFIX TO PREFIX
1. A+B*C+D
2. A+B+C+D
3. (A + B) * (C + D)
4. (A+B)*(C/D)
5. A * B- (C + D) + E
6. A*(B*C+D*E)+F
7. (A+B)*C+(D-E)/F+G
8. K + L - M*N + (O^P) * W/U/V * T + Q
9. (2-3+4)*(5+6*7)
10. 2-3+4-5*6
Postfix Evaluation
234*-5+ -5
23-45+* -9
234*5+- -15
Each operator in a postfix string refers to the previous two operands in the string.
Suppose that each time we read an operand we push it into a stack. When we reach an operator, its
operands will then be top two elements on the stack
We can then pop these two elements, perform the indicated operation on them, and push the result
on the stack.
So that it will be available for use as an operand of the next operator.
Final answer is
• 49
• 51
• 52
• 7
• None of these
EVALUATE: 623+-382/+*2^3+
Symbol Opnd1 Opnd2 Value OpendStk
6 6
2 6, 2
3 6, 2, 3
+ 2 3 5 6, 5
- 6 5 1 1
3 1, 3
8 1, 3, 8
2 1, 3, 8, 2
/ 8 2 4 1, 3, 4
+ 3 4 7 1, 7
* 1 7 7 7
2 7, 2
DR. NEEPA SHAH
^ 7 2 49 49 67
3 49, 3
+ 49 3 52 52
1. 2 3 4 * +
2. 3 4 * 2 5 * +
3. 6 3 2 4 + – *
4. 10 5 60 6 / * 8 –
5. 5 4 6 + * 4 9 3 / + *
6. 23-4+56*-
7. 23-4+567*+*
Step 1: Start Evaluating expression from right to left or reverse the expression
Step 2: If a character is an operand push it to Stack
Step 3: If the character is an operator
pop two elements from the Stack.
Operate on these elements according to the operator, and push the result back to the Stack
Step 4: Step 2 and 3 will be repeated until the end has reached.
Step 5:The Result is stored at the top of the Stack, return it
PRACTICE EXAMPLES
+9*26
*+69-31
- * + 2 2 3 10
-+8/632
-+7*45+20
/+33-+47*+123
-/*2*5+3652