Abstract Data Types (Adts) (Stack)
Abstract Data Types (Adts) (Stack)
Abstract Data Types (Adts) (Stack)
(Stack)
Abstract Data Types
A data type can be considered abstract when it is defined
in terms of operations on it, and its implementation is
hidden.
Static Implementation
Stack is represented as an array
Dynamic Implementation
Stack is represented as a linked list
Push operation
Initially stack is assumed to be empty --- >
top=-1
Now, increment top
Then push the element
4 50
3
40 40
2
1 30 30 30
20 20 20 20
0 10 10 10 10 10
top
Here top=4
stack[top]=50 top 40
Now decrement top by 1 as 30
one value is reduced 20
So, pop function returns the value after 10
pop
Stacks using arrays – push() and pop()
int StackArray[5]; // StackArray can contain up to 50 numbers
int top=-1; // index of the top element of the stack
// -1 used to indicate an empty stack
int pop()
{
int element = StackArray[top];
top--;
return element;
}
What will be the contents of the stack after
the following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
push(E)
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
B
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop E
push(C) D
push(D)
C
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C) D
push(D)
C
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
C
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
push(E)
A
pop
pop stack
pop
pop
What will be the contents of the stack after the
following sequence of operations?
push(A)
push(B)
pop
push(C)
push(D)
push(E)
pop
pop stack
pop
pop
Applications of Stacks
Applications of Stacks
Evaluation of expressions.
Ex: A + B +C means (A + B) + C
A$B$C means A $(B $ C), where $ stands for exponentiation
Ex:
Infix Postfix
A+B AB+
A+B-C AB+C-
(A+B)*(C-D) AB+CD-*
A$B*C-D+E/F/(G+H) AB$C*D-EF/GH+/+
((A+B)*C-(D-E))$(F+G) AB+C*DE- - FG+$
A-B/(C*D$E) ABCDE$*/ -
…………………………………………………………………………………………………….
Infix Prefix
A+B +AB
A+B-C -+ABC
(A+B)*(C-D) *+AB-CD
A$B*C-D+E/F/(G+H) +-*$ABCD//EF+GH
((A+B)*C-(D-E))$(F+G) $-*+ABC-DE+FG
A-B/(C*D$E) -A/B*C$DE
Convert an Infix to Postfix Using Stack:
When an operand is read, it is immediately placed on
to the output Array.
Operators are not immediately output, so we save
(:
We need to stack ‘(‘ when they are encountered.
If we see a ‘)’, then we pop the stack, writing symbols into
output Array until we encounter a corresponding ‘(‘ which is
popped but not output.
We never remove a ‘(‘ from a stack except when processing
a ‘)’.
Steps contd…….
the stack.
output stack
Ex: a+b*c+(d*e+f)*g
output stack
Ex: a+b*c+(d*e+f)*g
a
+
output stack
Ex: a+b * c+(d*e+f)*g
a b
+
output stack
Ex: a+b * c+(d*e+f)*g
*
ab +
output stack
Ex: a+b*c+(d*e+f)*g
*
ab c +
output stack
Ex: a+b*c+ (d*e+f)*g
abc **
+
output stack
Ex: a+b*c + (d*e+f)*g
abc ** +
output stack
Ex: a+b*c+(d*e+f)*g
abc ** + +
output stack
Ex: a+b*c+( d*e+f)*g
abc*+ (
+
stack
output
Ex: a+b*c+( d *e+f)*g
abc*+ d (
+
stack
output
Ex: a+b*c+(d * e+f)*g
abc*+d *
(
output +
stack
Ex: a+b*c+(d* e +f)*g
abc*+d e *
(
output +
stack
abc*+de *
(
output +
stack
Ex: a+b*c+(d*e+f)*g
abc*+de * +
(
output +
stack
Ex: a+b*c+(d*e+f)*g
abc*+de * +
(
output +
+
abc*+de* f (
+
output
stack
abc*+de* f + (
+
output
stack
Ex: a+b*c+(d*e+f ) *g
abc*+de* f +
+
output
stack
Ex: a+b*c+(d*e+f ) *g
abc*+de* f +
+
output
stack
abc*+de*f+ *
+
output
stack
Ex: a+b*c+(d*e+f) * g
abc*+de* f +
+
output
stack
abc*+de*f+ g *
+
output
stack
Ex: a+b*c+(d*e+f)* g
Ex: a+b*c+(d*e+f)*g
abc*+de*f+g *
+
output
stack
abc*+de*f+g* +
output stack
Ex: a+b*c+(d*e+f)*g
abc*+de*f+g*+
output stack
7. Exit
(2) Evaluating a postfix expression
Using stack
(2) Evaluating a postfix expression
Example:
6
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
6
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
3
2
6
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
3
5
2
6
6
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
5
6 1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
3
1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
8
3
1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2
8
3
1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2
4
8
3
3
1 1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2
4
8
3 7
3
1 1
1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2
4
8
3 7
3 7
1 1
1
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2
7 49
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2 3
7 49 49
(2) Evaluating a postfix expression
Ex: 6 2 3 + - 3 8 2 / + * 2 $ 3 + is the given postfix expression.
2 3
7 49 49 52
Algorithm: Evaluation of Postfix Expression
1. Set P as Expression, S as stack, Final Value.
2. Scan P from left to right and repeat step 3 to 5 till end of
expression.
3. If an operand is encountered, put it on the stack S.
4. If operator is encountered
a) Pop top two elements from the stack as A, and B
b) Evaluate A op B.
c) Push the result of above (b) back on the stack S.
(end of if)
(end of step 3 loop)
5. Set Final Value equal to the top element of the stack S.
6. Exit.
Checking for the Correct Parenthesis of
an Expression Using Stack
Correctness of an expression
When the expression involves several sets of multiple
parentheses, we have to ensure that the parentheses are
nested correctly. i.e., we want to check that:
There are equal number of right and left parentheses.
Every right parenthesis is preceded by a matching left
parenthesis.
Ex: ((A+B)
A+B( Are not correct
)A+B(-C
(A+B))-(C+D
(A+B]
Use stacks to check expression correctness
When ever an opening bracket is encountered, it is pushed on to the
stack.
When ever a closing bracket is encountered, the stack is examined.
If the stack is empty, then closing bracket does not have a matching
opening bracket and the expression is therefore invalid.
If the stack is non empty, we pop the stack and check whether the
popped item corresponds to the closing bracket.
Otherwise, one or more bracket have been opened, which have not
been closed and the string is invalid.
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
1
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
{
2
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
[
( (
{ {
2 3
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
[
( (
{ {
3 4
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
{ {
4 5
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
[
{
6
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
[ [
{ {
6 7
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
[ [
{ {
7 8
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
[ [
{ { {
7 8 9
Parenthesis stack at various stages of processing
{ x + (y – [a + b]) * c – [ (d + e) ]}
(
[ [
{ { {
7 8 9 10
Algorithm : Parenthesis Check by stack
1. Set P as Expression, S as stack.
2. Scan P from left to right and repeat step 3 to 4 till end of
expression.
3. If an opening bracket is encountered, put it on the stack S.
4. If closing bracket is encountered then check that:
a) If Stack S is empty then expression is INVALID.
b) If top of stack S is not matching opening bracket then
expression is INVALID.
c) If top of S is matching opening bracket then Pop it.