Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

UNIT-II

Stack
1) Definition: Stack is a linear data structure of LIFO(Last In First Out) type. One end of it
will be opened. From that end elements enter and leave the stack. A variable top will
keep track of element on the top of the stack. The initial value of variable top is -1.
Inserting elements into the stack is known as push operation and deleting elements from
the stack is known as pop operation. For every push operation variable top gets
incremented by one and for every pop operation variable top gets decremented by one.
Examples: Stack of files piled up one above the other in a tray.
Stack of books piled up one above the other in a shelf.
2) Primitive Operations: The following is the list of primitive operations possible on stack.
1. Creation
2. Push
3. Pop
4. Stack_full or
Stack_overflow
5. Stack_empty or Stack_underflow
6. Display
3) ADT for stack:
Abstract Data Type stack {
Instance: Stack is a linear data structure of LIFO(Last In First Out)
type. One end of it will be opened. From that end elements enter and leave the stack. A
variable top will keep track of that. The initial value of variable top is -1. Inserting
elements into the stack is known as push operation and deleting elements from the stack
is known as pop operation. For every push operation variable top gets incremented by
one and for every pop operation variable top gets decremented by one.
Operations: Creation()
Push()
Pop()
Stack_full()
or
Stack_overlow()
Stack_empty() or
Stack_underflow()
Display()
}
4) Implementation (or) Representation methods:
There are two representation methods available for stack. They are
i)
Array implementation
ii)
Pointer implementation

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

i)
Array Implementation of stack:
a)Creation of stack
#define MAXSIZE 10
struct stack {
char regno[10];
char name[20];
}st[MAXSIZE];
int top=-1;
b)Stack Empty or
int stack_empty() {
if(top == -1)
return 1;
else
return 0; }

Stack Underflow

c)Stack Full
or
Stack Overflow
int stack_full() {
if(top >= MAXSIZE-1)
return 1;
else
return 0; }
d)Push
void push() {
top++;
if(top==MAXZISE-1)
printf(Stack Overflow\n);
else{
gets(st[top].regno);
gets(st[top].name); } }
e)Pop
void pop() {
char e1[10],e2[20];
if(top==-1)
printf(Stack Underflow\n);
else
{
strcpy(e1,st[top].regno);
strcpy(e2,st[top].name);
strcpy(st[top].regno,0);
strcpy(st[top].name,0);
top--;
printf(Popped element is %s

%s\n,e1,e2); }

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

f)Display
void display()
{ int i;
for(i=0;i<=top;i++)
printf(%s %s,st[i].regno,st[i].name); }
Note:
In main function have menu with options 1)create 2)Push and 3)Pop. In switch case,
case1 should include the logic for creation of stack, case 2 should include the logic for
push operation and last case should include pop operation. During push operation check
for stack overflow and during pop operation check for stack under flow.
5) Applications of stack:
The following is the list of applications of stack
i)
Converting a decimal number into binary number
ii)
Expression conversion
iii)
Expression evaluation
iv)
Checking for well formed expression
v)
Reversing a string
vi)
All Recursive version of programs
1) Converting a decimal number into a binary number:
Algorithm: Decimal to binary conversion
i)
Consider given decimal number
ii)
Maintain a stack and name it as remainder stack as it stores only
remainder values
iii)
Divide decimal number by 2 and push the remainder on to stack. Repeat
this until dividend becomes zero
iv)
Pop remainders from stack one by one and place them in the output in
the same order until stack becomes empty. The final output will denote
binary equivalent of read decimal number
Example 1: Convert (12 )10 into its equivalent binary number using stack
Ans)
Input(decimal number)

Remainder Stack

Output(binary number)

(remainder)
2 | 12 0 push(0)
|
|
| _____
|
|
2 | 6 0 push(0)
|
|
|______
|
|
(1100 )2
2 | 3 1 push(1)
|
|
|______
|
1
| pop(1)
2 | 1 1 push(1)
|
1
| pop(1)
|______
|
0
| pop(0)
0
|____0_____| pop(0)
___________________________________________________________________
_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

Example 2: Convert (25)10 into its equivalent binary number using stack
Try by yourself
Expression: It is a combination of operands and operators
There are 3 types of expressions. They are
i)
Infix expression: An expression in which an operator lies between its
operands is known as infix expression
ii)
Prefix expression: An expression in which an operator precedes its
operands is known as prefix expression
iii)
Postfix expression: An expression in which an operator succeeds its
operands is known as postfix expression
2) Expression Conversion:
i) Infix to postfix
ii) Infix to prefix
Note: Another name to postfix notation is Reverse Polish Notation (RPN)
Algorithm: Infix expression to postfix expression
1) Read the infix expression from left to right taking one symbol at a time
2) Maintain a stack and name it as operators stack as it holds only operators
3) If read symbol happens to be ( then push it on to the stack
4) If read symbol happens to be operand then place it in output(Postfix expression).
Write the output from left to right
5) If read symbol happens to be an operator then
a) Check precedence of In Coming operator(ICP) is less than or equal to the
precedence of In Stack operator(ISP). If so then pop the operator from the stack
and place it in the output. Continue this until the precedence of in coming
operator(ICP) becomes greater than the precedence of in stack
operator(ISP).
b) Otherwise right away push the in coming operator(ICP) on to the stack
6) If read symbol happens to be ) then pop all the operators from the stack one by
one till its corresponding ( is encountered. Place the popped operators in the output
in their order of popping. Then cancel (. Dont place braces in the output.
7) Repeat steps 1 through 6 until all symbols of an infix expression are read. Finally
display the output which will be the postfix expression of given infix expression
Example 1: Convert the following infix notation into its postfix notation
1 * (2 3 + 4 ) ^ 5 / (6 * 7 + 8 )

Ans)
Infix Expression (Input):
1 * (2 3 + 4 ) ^ 5 / (6 * 7 + 8 )

Operators Stack:
*7 ( -1 +2 ^3 /6

( *4 +5

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

Postfix Expression (Output):


1234+5^67*8+/*
Note: Subscripts of operators indicate the sequence in which the operators are
popped from the stack
Try the following examples of converting infix notation into postfix notation by yourself.
i)
a * b (c+d) ^ e / (f * g + h)
Ans) a b * c d + e ^ f g * h + / ii)
8+ (((7-5) * (9-4)+6) / 4 )
Ans) 8 7 5 9 4 - * 6 + 4 / +
Algorithm: Infix expression to prefix expression
1) Read the infix expression from right to left taking one symbol at a time
2) Maintain a stack and name it as operators stack as it holds only operators
3) If read symbol happens to be ) then push it on to the stack
4) If read symbol happens to be operand then place it in output(Postfix expression).
Write the output from right to left.
5) If read symbol happens to be an operator then
a) Check precedence of Incoming operator(ICP) is less than(<) the precedence
of In Stack operator(ISP). If so then pop the operator from the stack and place it
in the output. Continue this until the precedence of incoming operator(ICP)
becomes greater than or equal to the precedence of operator in the
stack(ISP). Then push in coming operator (ICP) on to the stack.
b) Otherwise(>=) right away push the in coming operator(ICP) on to the stack
6) If read symbol happens to be ( then pop all the operators from the stack one by
one till its corresponding ) is encountered. Place the popped operators in the output
in their order of popping. Then cancel ). Dont place braces in the output.
7) Repeat steps 1 through 6 until all symbols of an infix expression are read. Finally
display the output which will be the postfix expression of given infix expression
Example 1: Convert the following infix notation into its prefix notation
1 * (2 3 + 4 ) ^ 5 / (6 * 7 + 8 )
Ans)
Infix Expression (Input):
1 * (2 3 + 4 ) ^ 5 / (6 * 7 + 8 )

Operators Stack:
) +2 *1 /7 ^5 ) +4 -3 *6
Prefix Expression (Ouput):
/*1^+-2345+*678

Note: Subscripts of operators indicate the sequence in which the operators are
popped from the stack

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

Try the following examples of converting infix notation into prefix notation by yourself.
i) a * b (c+d) ^ e / (f * g + h)
Ans) - * a b /^ + c d e + * f g h
ii) 8+ (((7-5) * (9-4)+6) / 4 )
Ans) + 8 / + * - 7 5 9 4 6 4

3) Expression Evaluation:
i) Postfix expression evaluation
ii) Prefix expression evaluation
Algorithm: Evaluation of Postfix expression
1) Read the postfix expression from left to right by taking one symbol at a time
2) Maintain a stack and name it as operands stack as it holds only operands
3) If the read symbol is an operand push it on to the stack
4) If the read symbol is an operator then pop its two operands from the stack and
perform the required operation as (second popped operand) operator (first popped
operand) and push the result back on to the stack
5) Repeat steps 2 and 3 till the end of the expression is encountered
6) Pop the final result from the stack and display
Example 1: Evaluate the following postfix expression, given, A=2, B=4, C=6 and D=1
BCAD*CA+D/++
Ans)
Postfix
Expression
BCA D * CA + D / + +

Operands
Stack
|
|
|
|
|
A5 D 7
|
| A 1 D 3 C 6 8 8 89
|
| C2 44 410
1211 |
| B12_________________|

Result
C A = 6 -2 = 4
4 * D = 4 *1 = 4
C + A = 6 + 2= 8
8/D=8/1=8
4 + 8 =12
B + 12 = 4 + 12 = 16

Try the following examples of evaluation postfix expression by yourself.


1) 8 7 5 9 4 - * 6 + 4 / +
Ans)
12
2) B C A / D + * C A D / +
Ans)
20
3) B C A D / - * C A D + * +
Ans)
34

given A=2, B=4, C=6

and

D=1

given A=2, B=4, C=6

and

D=1

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

Algorithm: Evaluation of Prefix expression


1) Read the prefix expression from right to left by taking one symbol at a time
2) Maintain a stack and name it as operands stack as it holds only operands
3) If the read symbol is an operand push it on to the stack
4) If the read symbol is an operator then pop its two operands from the stack and
perform the required operation as (first popped operand) operator (second popped
operand) and push the result back on to the stack
5) Repeat steps 2 and 3 till the end of the expression is encountered
6) Pop the final result from the stack and display
Example 1: Evaluate the following prefix expression, given, A=2, B=4, C=6 and D=1
+*B+/CAD/- CAD
( Its infix expression is B * (C/A+D) + (C-A) / D )
Ans)
Prefix
Expression
+ * B + / C A D/ - C A D

Operands
Stack

Result

|
|
|
|
|
C5
|
| C1
A 6 37 B 9
|
| A 2 43 D 8 49
1610 |
| D4_____411____________|

C A = 6 -2 = 4
4 / D = 4 /1 = 4
C /A= 6/2 =3
3+D=3+1=4
4 * B =16
16 + 4 = 20

Try the following examples of evaluation prefix expression by yourself.


1) + 8 / + * - 7 5 9 4 6 4
Ans)
12
2) + + B * - C A D / + C A D
Ans)
16

given A=2, B=4, C=6

and

D=1

4) Checking for well-formed expression:


Algorithm: Checking for fully parenthesized (or) well-formed expression
(Expression is said to fully parenthesized or well-formed if the number of left
parenthesis is equal to the number of right parenthesis)
1) Read the given expression from left to right by taking one symbol at a time
2) Maintain a stack for keeping left braces.
3) If the read symbol is an left brace then push it on to the stack
4) If the read symbol is either an operand or operator then ignore it
5) If the read symbol is a right brace then pop its corresponding left brace from the stack
6) Repeat steps 3 through 5 till the end of the expression.
One of the following situations may arise
i)
If for every right brace there exists corresponding left brace in the
expression when expression end reached and stack is empty then we say
_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

ii)

iii)

that the expression is fully parenthesized (or) well formed.


If for a right brace there exists no left brace in the stack when end of
expression reached and stack is empty then we say that the expression is
not fully parenthesized (or) well formed.
If stack is not empty and there exists left brace in it for which there is no
corresponding right brace when end of expression reached then we say
that the expression is not fully parenthesized (or) well formed.

The following examples illustrate the above three situations.


1) (a + ( b c)1)2 * d
|
|
|
(1
|
For every right brace there
|____(2_____|
exists corresponding left brace
and stack is becoming empty finally.
So expression is fully parenthesized.
2) (a + ( b c )1 * d

3) a + ( b c )1 * d)2

|
|
|
(1
|
|____(2______|

|
|
|
|
|____(1______|

For another left brace there


exists no corresponding right brace
and stack not becoming empty finally. So
expression is not fully parenthesized one.
For another right brace there
exists no corresponding left brace
and stack is becoming empty before end
of expression reached. So again
expression is not fully parenthesized one.

Other applications of stack are recursive version of programs. Recollect recursive


programs studied in the UNIT-I of this course.

_______________________________________________________________________________
Prepared by M.Nagaraju (MNU), SCSE, VIT

You might also like