SlideShare a Scribd company logo
DATA STRUCTURES
(CS3401)
Dr. Somaraju Suvvari,
Asst. Prof, Dept. of CSE, NIT Patna.
somaraju@nitp.ac.in;
soma2402@gmail.com;
Dr Somaraju Suvvari NITP -- CS3401 1
The Course
DATA STRUCTURES
Dr Somaraju Suvvari
NITP -- CS3401
2
STACKS
Dr Somaraju Suvvari
NITP -- CS3401
3
Dr Somaraju Suvvari
NITP -- CS3401
4
UNIT IV: Stacks
Basics of Stack data structure, Implementation of stack using array and linked list,
Operations on stacks, Applications of Stacks, Notations – infix, prefix and postfix,
Conversion and evaluation of arithmetic expressions using Stacks.
5
Data structure-stack
• Stack is a linear data structure in which the insertion and deletion operations are
performed at only one end.
• In a stack, adding and removing of elements are performed at a single position which
is known as "top”.
• That means, a new element is added at top of the stack and an element is removed
from the top of the stack.
• Inserting an element into stack at top is said to be PUSH operation.
• Deleting element from top of the stack is said to be POP operation.
• In stack, the insertion and deletion operations are performed based on LIFO (Last In
First Out) principle or FILO (First in Last Out)
A
B
C
D
top
D->C->B->A
The order in
which elements
gets removed
from the stack
Disk-A
Disk-B
Disk-C
Disk-D
[0]
[1]
[4]
[2]
[3]
[5]
top (presently)
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
6
Example of inserting elements into stack (PUSH operation)
Insert the elements 10,45,12,16,35 and 50 into an empty stack
10
top
top
45
10
top
12
45
10
top
16
12
45
10
top
35
16
12
45
10
top
50
35
16
12
45
10
top
Inserting (push)
Dr Somaraju Suvvari
NITP -- CS3401
7
50
35
16
12
45
10
top
35
16
12
45
10
top
16
12
45
10
top
45
10
top
10
top
top
Deleted elements { }
Deleted elements {50}
Deleted elements {50, 35}
Deleted elements {50 , 35, 16}
Deleted elements {50 , 35, 16, 12, 45}
Deleted elements {50 , 35, 16, 12, 45, 10}
In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out)
principle.
Example of Removing elements from the stack (POP operation)
top 12
45
10
Deleted elements {50 , 35, 16, 12}
Dr Somaraju Suvvari
NITP -- CS3401
8
int main()
{
…..
…
ABC();
….
…
}
ABC()
{
DEF();
}
DEF()
{
…..
…
GHI();
}
GHI()
{
…..
…
}
When a function main calls
function ABC, then main is
inserted onto the top of stack.
main will be removed once
ABC is completed After GHI execution is
completed, DEF will be
removed from the top of
stack.
main
ABC
main
DEF
ABC
main
ABC
main
main
When a function call is invoked, calling function will be pushed onto the stack. It will be
remain in the stack until called function finishes.
Application of stack (Importance of stack)
Dr Somaraju Suvvari
NITP -- CS3401
9
Stack Operations
• Standard operations
• Push
• To insert an element at the top of stack
• Pop
• To remove element from the top of stack
• Display (user defined)
• To display the elements of stack
• Usually once it is done stack will be empty.
• Peak (user defined)
• Returns the top of the element
Dr Somaraju Suvvari
NITP -- CS3401
10
Applications of STACK
• Reversing a list
• Parentheses checking
• Conversion of an infix expression into a postfix expression
• Evaluation of a postfix expression
• Conversion of an infix expression into a prefix expression
• Evaluation of a prefix expression
• Recursion
• Tower of Hanoi
• DFS technique
Dr Somaraju Suvvari
NITP -- CS3401
11
Implementation of Stack
• Simple Array based Implementation.
• Linked list Implementation.
Implementation of STACK using Arrays
Dr Somaraju Suvvari
NITP -- CS3401
12
Dr Somaraju Suvvari
NITP -- CS3401
13
STACK ADT (Array Based)
// Define the stack
#define MAX 100
typedef struct STACK S;
Element Type stack[MAX];
int top=-1;
// Define the set of operations on the stack
void push(Element_Type[], int, Element Type);
Element Type pop(Element_Type [], int);
void Display(Element_Type [], int)
Element_Type Peak(Element_Type [], int);
Dr Somaraju Suvvari
NITP -- CS3401
14
Push operation
PUSH(Element Type *stack, int top, Element Type value)
• push() is a function used to insert an element into the stack.
• The new element is always inserted at top position.
• Step 1 - Check whether stack is FULL. (top = = SIZE-1 or not)
• Step 2 - If stack is FULL, then display "Stack OVERFLOW!!!
Insertion is not possible!!!" and terminate the function.
• Step 3 - If stack is NOT FULL, then increment top value by one
(top++) and set stack[top] to value (stack[top] = value).
step - 1 : IF top = Max-1
Display “ OVERFLOW”
Go to step-4
step – 2: top++
Step – 3: stack[top] = value
Step - 4: End
Time Complexity = O(1)
15
• #define MAX 4
• int stack[MAX];
• int top=-1;
• push(int *stack, int top, int ele)
{
if(top==MAX-1)
{ printf(“n Stack Overflow”);
}
else
{ top=top+1;
stack[top]=ele;
}
}
Insert 20,45,12,16, and 35
top=-1
[0]
[1]
[3]
[2]
20
top=0
[0]
[1]
[3]
[2]
45
20
top=1
[0]
[1]
[3]
[2] 12
45
20
top=2
[0]
[1]
[3]
[2]
16
12
45
20
top=3
[0]
[1]
[3]
[2]
Push(stack, top, 20) Push(stack, top, 45) Push(stack, top, 12)
Push(stack, top, 16) Push(stack, top, 35)
Stack Overflow
step - 1 : IF top = Max-1
Display “ OVERFLOW”
Go to step-4
step – 2: top++
Step – 3: stack[top] = value
Step - 4: End
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
16
POP operation
• pop() is a function used to delete an element from the stack.
• In a stack, the element is always deleted from top position.
• Visiting element from the stack is possible by calling pop().
• We can use the following steps to pop an element from the
stack...
• Step 1 - Check whether stack is EMPTY. (top = = -1)
• Step 2 - If it is EMPTY, then display “UNDERFLOW!
Deletion is not possible!" and terminate the function.
• Step 3 - If it is NOT EMPTY, then delete stack[top] and
decrement top value by one (top--).
step - 1 : IF top = -1
Display “ UNDERFLOW”
Go to step-5
step – 2: x = stack[top]
Step – 3: top--
Step – 4: return x
Step - 5: End
Time Complexity = O(1)
17
int pop(int *stack, int top)
{ int val;
if(top = = -1)
printf(“n underflown”);
else
{ val=stack[top];
top=top-1;
return val;
}
}
35
16
12
45
10
top
16
12
45
10
top
12
45
10
top
45
10
top
Deleted elements { } pop(stack, top)
Deleted elements {35}
pop(stack, top)
Deleted elements {35, 16}
pop(stack, top)
Deleted elements {35, 16, 12}
10
top
pop(stack, top)
Deleted elements {35, 16, 12, 45}
top
pop(stack, top)
Deleted elements {35, 16, 12, 45, 10}
[0]
[1]
[4]
[2]
[3]
[0]
step - 1 : IF top = -1
Display “ UNDERFLOW”
Go to step-6
step – 2: x = stack[top]
Step – 4: top--
Step – 5: return x
Step - 6: End
[0]
[1]
[2]
[3]
[0]
[1]
[2]
[0]
[1]
pop(stack, top)
Underflow
Dr Somaraju Suvvari
NITP -- CS3401
18
STACK PROGRAM
#include<stdio.h>
#include<conio.h>
#define SIZE 05
int stack[SIZE], top = -1;
void push(int);
int pop();
void display();
int peak()
void main()
{ int value, choice, ele;
while(1)
{ clrscr(); printf("nn***** MENU *****n");
printf(“1. Pushn 2. Popn 3. Displayn 4. Peak 5.Exit");
printf("nEnter your choice: "); scanf("%d",&choice);
switch(choice)
{case 1: printf("Enter the value to be insert: ");
scanf("%d",&value); push(value); break;
case 2: ele=pop(); printf(“n %d is deleted”,ele); break;
case 3: display(); break;
case 4: ele=peak(); printf(“n The top element is: %d”, ele); break;
case 5: exit(0);
default: printf("nWrong selection Try again!!!");
}// switch – case
}// while
} // main
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
19
void display()
{ if(top == -1)
printf("n Stack Underflow");
else
{ int i;
printf("n Stack elements are:n");
for(i=top; i>=0; i--)
printf(“%d ", stack[i]); }
}
Time Complexity = O(n)
void push(int value)
{ if(top == SIZE-1)
printf("n Stack Overflow … “);
else
{ top++; stack[top] = value;
printf("n Insertion success!!!"); }
}
int pop()
{ int element;
if(top == -1)
printf("n Stack Underflow”);
else
{ element=stack[top]; top--;
return element; }
}
STACK OPERATIONS (LOGIC)
int peak()
{ int val;
if (top==-1) printf(“n Stack Underflow”);
else
{ val=pop();
push(val);
return val;
}
}
Time Complexity = O(1)
Alternative Implementation of STACK using Arrays
Dr Somaraju Suvvari
NITP -- CS3401
20
Dr Somaraju Suvvari
NITP -- CS3401
21
#include <stdio.h>
// Define the stack
#define MAX 3
typedef struct STACK S;
struct STACK
{
int stack[MAX];
int top;
};
S s;
// Define the set of operations on the stack
void create(STACK *);
void push(S *, int);
int pop(S *);
void display(S *);
int peak(S*);
int is_Empty(S *);
int is_Full(S *);
int is_Empty(S *s)
{ return (s->top == -1); }
int is_Full(S *s)
{ return (s->top == MAX-1); }
void create(S *s1)
{ s1->top=-1; }
void push(S *s, int x)
{
if(is_Full(s))
{ printf("nSTACK OVER FLOW");
return;
}
s->stack[++s->top] = x;
}
STACK OPERATIONS (LOGIC)
Dr Somaraju Suvvari
NITP -- CS3401
22
void display(S *s)
{
int i;
if(is_Empty(s))
{ printf("nNo elements to display");
return;
}
printf("nThe elements in the stack are: ");
for(i=0; i <= s->top; i++)
{
printf("%d ",s->stack[i]);
}
}
int peak(S *s)
{
if(is_Empty(s))
{
return -1;
}
return(s->stack[s->top]);
}
STACK OPERATIONS (LOGIC)
int pop(S *s)
{
if(is_Empty(s))
{ printf("nSTACK UNDERFLOW");
return -1;
}
return s->stack[s->top--];
}
Dr Somaraju Suvvari
NITP -- CS3401
23
int main()
{ S s1; s1.top=-1;
push(&s1, 1); display(&s1);
push(&s1, 2); display(&s1);
push(&s1, 3);
printf("nThe top element is(-1: underflow):%d", peak(&s1));
display(&s1);
push(&s1, 4); display(&s1);
printf("nThe deleted one is (-1: Underflow): %d",pop(&s1));
display(&s1);
printf("nThe deleted one is (-1: Underflow): %d",pop(&s1));
display(&s1);
printf("nThe deleted one is (-1: Underflow): %d",pop(&s1));
display(&s1);
printf("nThe deleted one is (-1: Underflow): %d",pop(&s1));
return 0;
}
STACK OPERATIONS (LOGIC)
Implementation of STACK using Linked Lists
Dr Somaraju Suvvari
NITP -- CS3401
24
Dr Somaraju Suvvari
NITP -- CS3401
25
Implementation of STACK using Linked Lists
• The other way of implementing the stack is using the Linked List (SLL/ CSLL/
DLL/ CDLL).
• The push operation is the insertion of node into the linked list at the beginning.
• The pop operation is the deletion of node from the beginning (the header/ top
node)
Dr Somaraju Suvvari
NITP -- CS3401
26
STACK ADT (Linked List Based (SLL))
// Define node
typedef struct Node STACK;
struct Node
{ int data; // Assume we are storing integer data
STACK *next;
}*top = NULL; // Initially stack is empty
// Define the set of operations on stack
STACK* push(STACK *, int);
STACK* pop(STACK *);
void Display(STACK *)
int Peak(STACK *);
Dr Somaraju Suvvari
NITP -- CS3401
27
Push operation
PUSH(STACK *stack, Element Type value)
• push() is a function used to insert an element into the stack.
• The new element is always inserted at the beginning of the list and is pointed by top
(In List it is head / start) .
• Step 1 - create a new_node. If new_node creation failed, then display "Stack OVERFLOW!!!
Insertion is not possible!!!" and terminate the function.
• Step 2 - If stack is NOT FULL, then insert the value and assign NULL to new_nodenext.
• Step 3 – if top == NULL, then assign new_node to top and terminate the function;
• Step 4 – If top != NULL, then assign top to new_nodenext and new_node to top;
• Step 5 – Return top;
Time Complexity = O(1)
PUSH(STACK *top, int value)
x1000
new_node
step1
top
NULL
step 2
step3
top
10 NULL
new_node
NULL
x1000
x1000
step4
top
20 x3000
x2000
x500
30 NULL
x3000
10
x1000
new_node
x1000
NULL
x2000
1
0
1. new_nodenext=top
2. top=new_node
Dr Somaraju Suvvari
NITP -- CS3401
28
10 NULL x1000
new_node
x2000
x500
x500
// No Stack Overflow
Dr Somaraju Suvvari
NITP -- CS3401
29
STACK* push(STACK *top, int value)
{ STACK *new_node;
new_node = (STACK *) malloc (sizeof(STACK));
if(new_node == NULL)
printf("n Stack Overflow … “);
else
{ new_node  data = value; new_nodenext = NULL;
if(top == NULL) // This is the first node
top = new_node;
else // There exist some nodes
{ new_node next = top;
top = new_node;
printf("n Insertion success!!!");
}
return top;
}
Time Complexity O(1)
STACK OPERATION - PUSH (Logic)
Dr Somaraju Suvvari
NITP -- CS3401
30
Pop operation
POP(STACK *top)
• pop() is a function used to delete an element from the top of the stack.
• The element is always deleted at the beginning of the list and is pointed by top
(In List it is head / start) .
• Step 1 - If top is NULL, then display "Stack UNDERFLOW!!! Deletion is not possible!!!" and
terminate the function.
• Step 2 - If stack is NOT EMPTY, then declare a pointer temp and assign top to temp.
• Step 3 – if topnext == NULL, then assign NULL to top, delete the temp and terminate the function;
• Step 4 – If topnext != NULL, then assign tempnext to top and delete temp;
• Step 5 – Return top;
Time Complexity = O(1)
POP(STACK *top)
step1
top
step 2
top
x2000 30 NULL
x2000
Dr Somaraju Suvvari
NITP -- CS3401
31
x500
x500
// Display Stack Underflow
temp
step3
NULL
NULL
top
x500
step 4 20 x3000
x2000
top
x500
x2000
30 NULL
x3000
top
x500
X3000 30 NULL
x3000
Dr Somaraju Suvvari
NITP -- CS3401
32
STACK* pop(STACK *top)
{
int element;
STACK *t;
if(top == NULL)
printf("n Stack Underflow”);
else
{
element = topdata;
t = top;
top = top next;
free(t);
return element;
}
return top;
}
STACK OPERATION - POP (Logic)
Dr Somaraju Suvvari
NITP -- CS3401
33
void display(STACK *top)
{ if(top == NULL)
printf("n Stack Underflow");
else
{ STACK *t = top;
printf("n Stack elements are:n");
while(t != NULL)
{ printf(“%d ", tdata);
t = tnext; } // while
} // else
} // display
Time Complexity = O(n)
STACK OPERATIONS (Logic)
int peak(STACK *top)
{ int val;
if (top = = NULL) printf(“n Stack Underflow”);
else
{ val=top  data;
return val;
}
}
Time Complexity = O(1)
Dr Somaraju Suvvari
NITP -- CS3401
34
Parenthesis checking
• {(((([]))))} // Check the parenthesis are balanced or not
• if char is ‘(‘ or ‘{‘ or ‘[‘ then we push it onto the top of stack.
• If char is ‘)’ or ‘}’ or ‘]’ then we perform pop operation and if the popped character is the
matches with the starting bracket then fine otherwise parenthesis are not balanced and
terminate the task.
• If char is the end of string and if stack is empty, then it is balanced.
• In other cases, it is said to imbalanced.
Dr Somaraju Suvvari
NITP -- CS3401
35
ASSIGNMENT (Graded one)
1. Write the algorithm/pseudo code to implement stack using doubly linked list.
2. Demonstrate how to check the balancing of the following parenthesis expressions using stack
i. (a+b*(c/d)+e*(-f))
ii. a[(b*(c+d))]
iii. a/(b/(c)
3. Write the algorithm/pseudo code for how to reverse the given string using the stack operations push and pop.
Also demonstrate with an example.
4. Write the algorithm/pseudo code to convert the given decimal number into binary using the stack. Also
demonstrate with an example.
5. Write the algorithm/pseudo code to check the given string is palindrome or not using stack. Also demonstrate
with an example.
Dr Somaraju Suvvari
NITP -- CS3401
36
Notations – infix, prefix and postfix
• Any arthematic expression consists of operators and operands.
• The way we write the arithmetic expression is called notation;
• There are three different notations used to write the arthematic expression.
1. Infix Expression
2. Prefix Expression
3. Postfix expression
• We can convert the expression in one notation to another notation.
Dr Somaraju Suvvari
NITP -- CS3401
37
Infix Notation
• An expression is said to be in infix notation if the operators in the expression are placed in
between the operands on which the operator works.
• For example - a + b * c
• In the above example the operator is * is placed between the operands on which this operator
works, here b and c.
• Infix expressions are easy for humans to read, write and understand, but it is not the case for
computing devices.
• It is costly (in terms of space and time) to process the infix expressions in algorithms.
Dr Somaraju Suvvari
NITP -- CS3401
38
Prefix Notation
• An expression is said to be in prefix notation if the operators in the expression are
placed before the operands on which the operator works.
• For example: +a*bc
• In the above example the operator * is placed before the operands on which this
operator works, here b and c, similarly the + is placed before a and the result of
(b*c).
• Prefix notation is also called as Polish notation.
Dr Somaraju Suvvari
NITP -- CS3401
39
Postfix Notation
• An expression is said to be in postfix notation if the operators in the expression are
placed after the operands on which the operator works.
• For example - abc*+
• In the above example the operator * is placed after the operands on which this
operator works, here b and c, similarly the + is placed after a and the result of (b*c).
• Postfix notation is also called as Reverse Polish notation.
• Widely used notation for evaluating the expressions.
40
Evaluation of expressions
• When evaluating the arithmetic expression two things need be considered:
1. The precedence of the operator - If any operand is present in-between two different operators, the precedence
(priority) of the one operator over the other decides which operator should use the operand first or which
operator to be evaluated first. For example in the arithmetic expression a + b * c the operand b is surrounded by
the operators * and +. In computer languages the * enjoying the higher precedence than the +, so * takes b first.
2. The Associativity of the operator - It resolves the ties between the same precedence of operators in the
arithmetic expression, by considering whether the operators are evaluated from left to right or right to left. For
example in the expression a* b * c, here b is surrounded by two * operators, So which * should take b first or
which multiplication must be done first? It is decided by the associativity of *. So in computer languages * is a
left associative, so the first * will be performed first (multiply and b first then the result will be multiplied by c).
• It is not advantageous to evaluate the infix expressions, so first convert them to postfix or prefix
and then compute the expressions.
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
41
Precedence and Associativity of operators
Operator Description Associativity Precedence
()
[]
.

++ --
Parenthesis 1
(function calls)
Brackets (array subscript)
Members selection via object name
Member selection via pointer
Post increment and post decrement
Left to Right 14
++ --
+ -
! ~
(type)
*
&
Sizeof
Pre increment and pre decrement
Unary plus or minus
Logical negation and bitwise complement
Casting type
Dereference
Address
Size in bytes
Right to Left 13
* / % Arithmetic multiplication, division, modulus Left to Right 12
+ - Arithmetic addition, subtraction Left to Right 11
<< >> Bitwise shift left, shift right Left to Right 10
< <=
> >=
Relational less than, less than or equal to
Relational greater than, greater than or equal to
Left to Right 9
1. Parenthesis are used to override the precedence of operators and such parenthesis expressions can be nested and evaluated from inner to
outer
42
Precedence and Associativity of operators
Dr Somaraju Suvvari
NITP -- CS3401
Operator Description Associativity Precedence
= = != Relational equal to, not equal to Left to Right 8
& Bitwise AND Left to Right 7
^ Bitwise Exclusive OR Left to Right 6
| Bitwise Inclusive OR Left to Right 5
&& Logical AND Left to Right 4
|| Logical OR Left to Right 3
? : Ternary condition Right to Left 2
=
+= -+
*= /=
%= &=
^= |=
<<= >>=
Assignment
Addition/Subtraction Assignment
Multiplication/Division Assignment
Modulus/Bitwise AND assignment
Bitwise Exclusive/ Inclusive Assignment
Bitwise Shift Left/Right Assignment
Right to Left 1
, Comma Left to Right 0
1. Parenthesis are used to override the precedence of operators and such parenthesis expressions can be nested and evaluated from inner to
outer
43
Infix to postfix Conversion
1. Read all the symbols one by one from left to right in the given Infix Expression.
2. If the reading symbol is operand, then immediately send it to the output.
3. If the reading symbol is left parenthesis '(', then Push it on to the Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left
parenthesis is popped and print each popped symbol to the output.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However,
1. The associativity of the operators (current operator and the operator on top of the stack) is left
to right then first pop the operators which are already on the stack that have higher or equal
precedence than the current operator then pop it and send it to output, if open parenthesis is
there on top of the stack then push the operator into stack (even though the precedence of ‘(’ is
more than any other operator and it is the exceptional case).
2. The associativity of the operators (current operator and the operator on top of the stack) is right
to left then first pop the operators which are already on the stack that have lower precedence
than the current operator then pop it and send it to output, if open parenthesis is there on top of
the stack then push the operator into stack (even though the precedence of ‘(’ is more than any
other operator and it is the exceptional case)
6. If the input is over, so pop all the remaining symbols from the stack and output them.
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
44
Infix to postfix Conversion (Example)
Consider the infix arithmetic expression: a * (b + c + d)
Input (infix expression)
a * (b + c + d)
top
a
STACK
Output (postfix expression)
1
2
a is an operand
Action - Output a
Input (infix expression)
a * (b + c + d)
*
top
STACK
a
Output (postfix expression)
• * is an operator,
• Action - push it into stack
after removing the higher or
equal priority operators
from the top of the stack
Dr Somaraju Suvvari
NITP -- CS3401
45
Infix to postfix Conversion (Example)
Input (infix expression)
a * (b + c + d)
(
*
top
a
STACK
Output (postfix expression)
3
4
b is an operand, so output b
Input (infix expression)
a * (b + c + d)
(
*
top
STACK
a b
Output (postfix expression)
• ( is an open parenthesis
• Action - push it into
stack
*
top
STACK
Dr Somaraju Suvvari
NITP -- CS3401
46
Infix to postfix Conversion (Example)
Input (infix expression)
a * (b + c + d)
+
(
*
top
a b
STACK
Output (postfix expression)
5
6
c is an operand, so output c
Input (infix expression)
a * (b + c + d)
+
(
*
top
STACK
a b c
Output (postfix expression)
• + is an operator,
• Action - push it into stack after removing
the higher or equal priority operators from
the top of the stack. Present operator on
stack is ( and it has higher precedence than
+, but for ( we have exception, so push +
into stack,
Dr Somaraju Suvvari
NITP -- CS3401
47
Infix to postfix Conversion (Example)
Input (infix expression)
a * (b + c + d)
(
*
top
a b c +
STACK
Output (postfix expression)
7
+
(
*
top
STACK
a b c +
Output (postfix expression)
• + is an operator,
• Action - push it into stack after removing
the higher or equal priority operators
from the top of the stack. Present
operator on stack is + and it has equal
precedence with the input symbol +, so
pop the + from stack and output.
• + is an operator,
• Action – Now present top of the stack is ( and it has higher
precedence than input symbol +, +, but for ( we have an
exception, so push + into stack
+
(
*
top
STACK
Dr Somaraju Suvvari
NITP -- CS3401
48
Infix to postfix Conversion (Example)
Input (infix expression)
a * (b + c + d)
+
(
*
top
a b c + d
STACK
Output (postfix expression)
8
9
d is an operand, so output d
Input (infix expression)
a * (b + c + d)
*
top
STACK
a b c + d +
Output (postfix expression)
• ) is an operator,
• Action – Pop all the contents of stack until
respective left parenthesis is popped and
send each popped symbol to the output.
Dr Somaraju Suvvari
NITP -- CS3401
49
Infix to Postfix Conversion (Example)
Input (infix expression)
a * (b + c + d)
top
a b c + d + *
STACK
Output (postfix expression)
10
Input is over so pop all the
remaining symbols from the stack
and output them.
Dr Somaraju Suvvari
NITP -- CS3401
50
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define size 100
char stack[size];
int top=-1;
void push(char *, int, char);
char pop(char *, int);
int prio(char op);
int prio(char op)
{
if(op = = '+’ || op= = '-')
return 1;
if(op = = '*' || op = = ‘/’ || op==‘%’)
return 2;
if(op == ‘(‘ )
return 0;
else {printf(“Wrong operator”); exit(0);}
}
// Assume our expression contains only these operators
void push(char *stack, int top, char e)
{
if(top==size-1)
printf("n stack overflow");
else
stack[++top]=e;
}
char pop(char *stack, int top)
{
if(top==-1)
printf("n stack is underflow");
else
return stack[top--];
}
Infix to Postfix conversion (Logic)
Dr Somaraju Suvvari
NITP -- CS3401
51
void main()
{ char ie[100], pe[100]; int i, j=0;
printf("n Enter infix expression: "); gets(ie);
for(i=0; ie[i]!='0’; i++)
{ if(ie[i]=='(‘) push(ie[i]); // open parenthesis is pushed into the stack
else if( (ie[i] >= ‘a’ && ie[i] <= ‘z’) || (ie[i] >= ‘0’ && ie[i] <= ‘9’)) {pe[j++]=ie[i]; } //operand
else if(ie[i]==‘)’) // if it is closed parenthesis then pop all symbols from the stack upto open parenthesis encountered in the stack
{while(stack[top]!=‘(‘) { pe[j++]=pop(stack, top); } // Assume the given infix expression is parenthesis balanced one
pop(); }
else // if it is an operator
{ while(top > -1 && (prio(stack[top])>=prio(ie[i]) ) {pe[j++]=pop();}
push(ie[i]); }
}
while(top>-1) {pe[j++]=pop(); } // Leftover symbols from the stack are popped
pe[j]='0’;
printf("the infix expression %s converted to %s postfix expression",ie, pe);
}
Infix to Postfix conversion (Logic)
(Considered only operators which has left to right associativity)
52
Infix to Prefix Conversion
1. Reverse the given infix expression, for example the reverse of the infix expression a + b *c
is c * b + a. When reversing the parenthesis ‘)’ will become ‘(‘ and ‘(‘ will become ‘)’
(applicable to all types of brackets)
2. Apply the infix to postfix conversion algorithm on the reversed infix expression. For the
above example the resultant postfix expression is: cb*a+
3. Reverse the obtained postfix expression, and is the required prefix expression for the given
infix expression. For the above example, the infix expression is +a*bc
Dr Somaraju Suvvari
NITP -- CS3401
53
Prefix to Infix Conversion
1. Reverse the given prefix expression, for example the reverse of the prefix expression *cd is
dc*
2. Read the character by character of the reversed infix expression and repeat the step 3 and 4
till there are no characters in the reversed prefix expression .
3. If the character read is an operand then push the operand into the stack. (push d and c)
4. If the character read is an operator, op, then pop the top two symbols from the stack, the
first one is p1, and the second one is p2. Push the concatenated string p1 op p2 to stack.
(push c *d )
5. Now the value in the stack is the required infix expression. (c*d)
Dr Somaraju Suvvari
NITP -- CS3401
54
Prefix to Postfix Conversion
1. Reverse the given prefix expression, for example the reverse of the prefix expression *cd is
cd*.
2. Read the character by character of the reversed infix expression and repeat the step 3 and 4
till there are no characters in the reversed prefix expression .
3. If the character read is an operand then push the operand into the stack. (push d and c)
4. If the character read is an operator, op, then pop the top two symbols from the stack, the
first one is p1, and the second one is p2. Push the concatenated string p1 p2 op to stack.
(push c d * )
5. Now the value in the stack is the required postfix expression. (c d *)
Dr Somaraju Suvvari
NITP -- CS3401
55
Postfix to Infix Conversion
1. Read the character by character of the given postfix expression and repeat the step 2 and 3
till there are no characters in the postfix expression. For example assume the postfix
expression ab+;
2. If the character read is an operand then push the operand into the stack. (push a and b)
3. If the character read is an operator, op, then pop the top two symbols from the stack, the
first one is p1, and the second one is p2. Push the concatenated string p2 op p1 to stack.
(push a * b )
4. Now the value in the stack is the required infix expression. (a * b)
Dr Somaraju Suvvari
NITP -- CS3401
56
Postfix to Prefix Conversion
1. Read the character by character of the given postfix expression and repeat the step 2 and 3
till there are no characters in the postfix expression. For example assume the postfix
expression ab+;
2. If the character read is an operand then push the operand into the stack. (push a and b)
3. If the character read is an operator, op, then pop the top two symbols from the stack, the
first one is p1, and the second one is p2. Push the concatenated string op p2 p1 to stack.
(push a * b )
4. Now the value in the stack is the required postfix expression. (*a b)
Dr Somaraju Suvvari
NITP -- CS3401
Dr Somaraju Suvvari
NITP -- CS3401
57
Examples
S.No Infix Expression Prefix Expression Postfix Expression
1 a + b * c +a*bc a b c*+
2 (a + b) * (c + d) * + a b + c d a b + c d + *
3 a * b + c * d + * a b * c d a b * c d * +
4 a + b + c + d + + + a b c d a b + c + d +
5 (a + b) * c – d -*+a b c d ab + c * d-
6 a + b - c -+a b c ab + c -
58
Evaluation of Postfix Expression
Algorithm
1. Scan the symbols one by one from left to right in the given Postfix Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is binary operator (+ , - , * , / etc.,), then perform two pop operations
and do the operation specified by the reading symbol (if it is * do multiplication, etc) using
the two popped operands and push the result back on to the stack.
4. Repeat steps 2 & 3 till the postfix expression completes.
5. Finally! perform a pop operation and display the popped value as final result.
Dr Somaraju Suvvari
NITP -- CS3401
Infix exp: ( 5 + 3 ) * ( 8 -2)
Postfix: 5 3 + 8 2 - *
Dr Somaraju Suvvari
NITP -- CS3401
59
Evaluations of postfix Conversion (Example)
Reading Stack
Symbol
Initially
6
(operand)
push(stack, top, 6)
Infix exp: ( 6 + 2 ) * ( 8 - 2)
Postfix: 6 2 + 8 2 - *
t
o
p
t
o
p
6
Reading Stack
Symbol
2
(operand)
push(stack, top, 2)
+
(operator)
pop the top two
two operands from
Stack
op1 = pop(stack, top) //2
op2 = pop(stack, top) // 6
op3 = op2 + op1 // 8
push(stack, top, op3)
t
o
p
6
2
t
o
p
Reading Stack
Symbol
8
(operand)
push(stack, top, 8)
2
(operand)
push(stack, top, 2)
8
t
o
p
8
8
t
o
p
8
8
2
Dr Somaraju Suvvari
NITP -- CS3401
60
Evaluations of postfix Conversion (Example)
Infix exp: ( 6 + 2 ) * ( 8 - 2)
Postfix: 6 2 + 8 2 - *
Reading Stack
Symbol
-
(operator)
pop the top two
two operands from stack
op1 = pop(stack, top) //2
op2 = pop(stack, top) //8
op3 = op2 - op1 // 6
push(stack, top, op3)
*
(operator)
pop the top two
operands from stack
op1 = pop(stack, top) // 6
op2 = pop(stack, top // 8
op3 = op2 * op1
push(stack, top, op3)
t
o
p
48
t
o
p
8
6
The final value is : 48
Postfix Evaluation
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define size 100
int stack[size];
int top=-1;
void push(int);
int pop();
void main()
{ char pe[30];
int i,v,op1,op2;
printf("n enter any
postfix expression");
gets(pe);
for(i=0;pe[i]!='0';i++)
{ if(isalpha(pe[i]))
{printf("n enter value for %c", pe[i]);
scanf("%d",&v);
push(v);
}
else if( isdigit(pe[i]))
push(pe[i]-'0');
else
{ op2=pop();
op1=pop();
switch(pe[i])
{case '+‘ : push(op1+op2);break;
case '-‘ : push(op1-op2);break;
case '*‘ : push(op1*op2);break;
case '/': push(op1/op2);break;
case '%‘ : push(op1%op2);break;
default: printf("n invalid operation");
}
}
}
printf(“n The result is: %d",stack[top]);
}
void push(int e)
{
If(top==size-1)
printf("n stack overflow");
else
stack[++top]=e;
}
int pop()
{
if(top==-1)
printf("n stack underflow");
else
return stack[top--];
}
Postfix: X 4 + Y Z - *
X= 5, Y=8, Z=2
62
Thank You
Dr Somaraju Suvvari
NITP -- CS3401

More Related Content

Similar to 6 - STACKS in Data Structure and Algorithm.pptx (20)

PPT
The Stack in data structures .ppt
donemoremaregere376
 
DOCX
Ds
Acad
 
DOCX
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
PDF
04 stacks
Rajan Gautam
 
PPTX
Stack and Queue.pptx university exam preparation
RAtna29
 
PDF
Chapter 4 stack
jadhav_priti
 
PPTX
Stack in C.pptx
RituSarkar7
 
PDF
The Stack (Data Structccccccccccccccccccc
parwarsmko98
 
PDF
Stack
Amrutha Rajan
 
PDF
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
PDF
Stacks
amitphadikar2012
 
PPTX
Stack.pptx
AliRaza899305
 
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
PPTX
Data Structure.pptx
SajalFayyaz
 
PPTX
Lecture_5_Stack.pptx
LabibHossain5
 
PPTX
Data structure by Digvijay
Digvijay Singh Karakoti
 
PPT
Lec 4 Stack of Data Structures & Algorithms
haseebanjum2611
 
PDF
Stack
maamir farooq
 
PDF
Data structure.pdf
piyushagarwal279293
 
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 
The Stack in data structures .ppt
donemoremaregere376
 
Ds
Acad
 
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
04 stacks
Rajan Gautam
 
Stack and Queue.pptx university exam preparation
RAtna29
 
Chapter 4 stack
jadhav_priti
 
Stack in C.pptx
RituSarkar7
 
The Stack (Data Structccccccccccccccccccc
parwarsmko98
 
STACK ( LIFO STRUCTURE) - Data Structure
Yaksh Jethva
 
Stack.pptx
AliRaza899305
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
Balwant Gorad
 
Data Structure.pptx
SajalFayyaz
 
Lecture_5_Stack.pptx
LabibHossain5
 
Data structure by Digvijay
Digvijay Singh Karakoti
 
Lec 4 Stack of Data Structures & Algorithms
haseebanjum2611
 
Data structure.pdf
piyushagarwal279293
 
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
emathemathematics
 

Recently uploaded (20)

PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
Digital water marking system project report
Kamal Acharya
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Ad

6 - STACKS in Data Structure and Algorithm.pptx

  • 1. DATA STRUCTURES (CS3401) Dr. Somaraju Suvvari, Asst. Prof, Dept. of CSE, NIT Patna. [email protected]; [email protected]; Dr Somaraju Suvvari NITP -- CS3401 1
  • 2. The Course DATA STRUCTURES Dr Somaraju Suvvari NITP -- CS3401 2
  • 4. Dr Somaraju Suvvari NITP -- CS3401 4 UNIT IV: Stacks Basics of Stack data structure, Implementation of stack using array and linked list, Operations on stacks, Applications of Stacks, Notations – infix, prefix and postfix, Conversion and evaluation of arithmetic expressions using Stacks.
  • 5. 5 Data structure-stack • Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. • In a stack, adding and removing of elements are performed at a single position which is known as "top”. • That means, a new element is added at top of the stack and an element is removed from the top of the stack. • Inserting an element into stack at top is said to be PUSH operation. • Deleting element from top of the stack is said to be POP operation. • In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle or FILO (First in Last Out) A B C D top D->C->B->A The order in which elements gets removed from the stack Disk-A Disk-B Disk-C Disk-D [0] [1] [4] [2] [3] [5] top (presently) Dr Somaraju Suvvari NITP -- CS3401
  • 6. Dr Somaraju Suvvari NITP -- CS3401 6 Example of inserting elements into stack (PUSH operation) Insert the elements 10,45,12,16,35 and 50 into an empty stack 10 top top 45 10 top 12 45 10 top 16 12 45 10 top 35 16 12 45 10 top 50 35 16 12 45 10 top Inserting (push)
  • 7. Dr Somaraju Suvvari NITP -- CS3401 7 50 35 16 12 45 10 top 35 16 12 45 10 top 16 12 45 10 top 45 10 top 10 top top Deleted elements { } Deleted elements {50} Deleted elements {50, 35} Deleted elements {50 , 35, 16} Deleted elements {50 , 35, 16, 12, 45} Deleted elements {50 , 35, 16, 12, 45, 10} In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) principle. Example of Removing elements from the stack (POP operation) top 12 45 10 Deleted elements {50 , 35, 16, 12}
  • 8. Dr Somaraju Suvvari NITP -- CS3401 8 int main() { ….. … ABC(); …. … } ABC() { DEF(); } DEF() { ….. … GHI(); } GHI() { ….. … } When a function main calls function ABC, then main is inserted onto the top of stack. main will be removed once ABC is completed After GHI execution is completed, DEF will be removed from the top of stack. main ABC main DEF ABC main ABC main main When a function call is invoked, calling function will be pushed onto the stack. It will be remain in the stack until called function finishes. Application of stack (Importance of stack)
  • 9. Dr Somaraju Suvvari NITP -- CS3401 9 Stack Operations • Standard operations • Push • To insert an element at the top of stack • Pop • To remove element from the top of stack • Display (user defined) • To display the elements of stack • Usually once it is done stack will be empty. • Peak (user defined) • Returns the top of the element
  • 10. Dr Somaraju Suvvari NITP -- CS3401 10 Applications of STACK • Reversing a list • Parentheses checking • Conversion of an infix expression into a postfix expression • Evaluation of a postfix expression • Conversion of an infix expression into a prefix expression • Evaluation of a prefix expression • Recursion • Tower of Hanoi • DFS technique
  • 11. Dr Somaraju Suvvari NITP -- CS3401 11 Implementation of Stack • Simple Array based Implementation. • Linked list Implementation.
  • 12. Implementation of STACK using Arrays Dr Somaraju Suvvari NITP -- CS3401 12
  • 13. Dr Somaraju Suvvari NITP -- CS3401 13 STACK ADT (Array Based) // Define the stack #define MAX 100 typedef struct STACK S; Element Type stack[MAX]; int top=-1; // Define the set of operations on the stack void push(Element_Type[], int, Element Type); Element Type pop(Element_Type [], int); void Display(Element_Type [], int) Element_Type Peak(Element_Type [], int);
  • 14. Dr Somaraju Suvvari NITP -- CS3401 14 Push operation PUSH(Element Type *stack, int top, Element Type value) • push() is a function used to insert an element into the stack. • The new element is always inserted at top position. • Step 1 - Check whether stack is FULL. (top = = SIZE-1 or not) • Step 2 - If stack is FULL, then display "Stack OVERFLOW!!! Insertion is not possible!!!" and terminate the function. • Step 3 - If stack is NOT FULL, then increment top value by one (top++) and set stack[top] to value (stack[top] = value). step - 1 : IF top = Max-1 Display “ OVERFLOW” Go to step-4 step – 2: top++ Step – 3: stack[top] = value Step - 4: End Time Complexity = O(1)
  • 15. 15 • #define MAX 4 • int stack[MAX]; • int top=-1; • push(int *stack, int top, int ele) { if(top==MAX-1) { printf(“n Stack Overflow”); } else { top=top+1; stack[top]=ele; } } Insert 20,45,12,16, and 35 top=-1 [0] [1] [3] [2] 20 top=0 [0] [1] [3] [2] 45 20 top=1 [0] [1] [3] [2] 12 45 20 top=2 [0] [1] [3] [2] 16 12 45 20 top=3 [0] [1] [3] [2] Push(stack, top, 20) Push(stack, top, 45) Push(stack, top, 12) Push(stack, top, 16) Push(stack, top, 35) Stack Overflow step - 1 : IF top = Max-1 Display “ OVERFLOW” Go to step-4 step – 2: top++ Step – 3: stack[top] = value Step - 4: End Dr Somaraju Suvvari NITP -- CS3401
  • 16. Dr Somaraju Suvvari NITP -- CS3401 16 POP operation • pop() is a function used to delete an element from the stack. • In a stack, the element is always deleted from top position. • Visiting element from the stack is possible by calling pop(). • We can use the following steps to pop an element from the stack... • Step 1 - Check whether stack is EMPTY. (top = = -1) • Step 2 - If it is EMPTY, then display “UNDERFLOW! Deletion is not possible!" and terminate the function. • Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--). step - 1 : IF top = -1 Display “ UNDERFLOW” Go to step-5 step – 2: x = stack[top] Step – 3: top-- Step – 4: return x Step - 5: End Time Complexity = O(1)
  • 17. 17 int pop(int *stack, int top) { int val; if(top = = -1) printf(“n underflown”); else { val=stack[top]; top=top-1; return val; } } 35 16 12 45 10 top 16 12 45 10 top 12 45 10 top 45 10 top Deleted elements { } pop(stack, top) Deleted elements {35} pop(stack, top) Deleted elements {35, 16} pop(stack, top) Deleted elements {35, 16, 12} 10 top pop(stack, top) Deleted elements {35, 16, 12, 45} top pop(stack, top) Deleted elements {35, 16, 12, 45, 10} [0] [1] [4] [2] [3] [0] step - 1 : IF top = -1 Display “ UNDERFLOW” Go to step-6 step – 2: x = stack[top] Step – 4: top-- Step – 5: return x Step - 6: End [0] [1] [2] [3] [0] [1] [2] [0] [1] pop(stack, top) Underflow Dr Somaraju Suvvari NITP -- CS3401
  • 18. 18 STACK PROGRAM #include<stdio.h> #include<conio.h> #define SIZE 05 int stack[SIZE], top = -1; void push(int); int pop(); void display(); int peak() void main() { int value, choice, ele; while(1) { clrscr(); printf("nn***** MENU *****n"); printf(“1. Pushn 2. Popn 3. Displayn 4. Peak 5.Exit"); printf("nEnter your choice: "); scanf("%d",&choice); switch(choice) {case 1: printf("Enter the value to be insert: "); scanf("%d",&value); push(value); break; case 2: ele=pop(); printf(“n %d is deleted”,ele); break; case 3: display(); break; case 4: ele=peak(); printf(“n The top element is: %d”, ele); break; case 5: exit(0); default: printf("nWrong selection Try again!!!"); }// switch – case }// while } // main Dr Somaraju Suvvari NITP -- CS3401
  • 19. Dr Somaraju Suvvari NITP -- CS3401 19 void display() { if(top == -1) printf("n Stack Underflow"); else { int i; printf("n Stack elements are:n"); for(i=top; i>=0; i--) printf(“%d ", stack[i]); } } Time Complexity = O(n) void push(int value) { if(top == SIZE-1) printf("n Stack Overflow … “); else { top++; stack[top] = value; printf("n Insertion success!!!"); } } int pop() { int element; if(top == -1) printf("n Stack Underflow”); else { element=stack[top]; top--; return element; } } STACK OPERATIONS (LOGIC) int peak() { int val; if (top==-1) printf(“n Stack Underflow”); else { val=pop(); push(val); return val; } } Time Complexity = O(1)
  • 20. Alternative Implementation of STACK using Arrays Dr Somaraju Suvvari NITP -- CS3401 20
  • 21. Dr Somaraju Suvvari NITP -- CS3401 21 #include <stdio.h> // Define the stack #define MAX 3 typedef struct STACK S; struct STACK { int stack[MAX]; int top; }; S s; // Define the set of operations on the stack void create(STACK *); void push(S *, int); int pop(S *); void display(S *); int peak(S*); int is_Empty(S *); int is_Full(S *); int is_Empty(S *s) { return (s->top == -1); } int is_Full(S *s) { return (s->top == MAX-1); } void create(S *s1) { s1->top=-1; } void push(S *s, int x) { if(is_Full(s)) { printf("nSTACK OVER FLOW"); return; } s->stack[++s->top] = x; } STACK OPERATIONS (LOGIC)
  • 22. Dr Somaraju Suvvari NITP -- CS3401 22 void display(S *s) { int i; if(is_Empty(s)) { printf("nNo elements to display"); return; } printf("nThe elements in the stack are: "); for(i=0; i <= s->top; i++) { printf("%d ",s->stack[i]); } } int peak(S *s) { if(is_Empty(s)) { return -1; } return(s->stack[s->top]); } STACK OPERATIONS (LOGIC) int pop(S *s) { if(is_Empty(s)) { printf("nSTACK UNDERFLOW"); return -1; } return s->stack[s->top--]; }
  • 23. Dr Somaraju Suvvari NITP -- CS3401 23 int main() { S s1; s1.top=-1; push(&s1, 1); display(&s1); push(&s1, 2); display(&s1); push(&s1, 3); printf("nThe top element is(-1: underflow):%d", peak(&s1)); display(&s1); push(&s1, 4); display(&s1); printf("nThe deleted one is (-1: Underflow): %d",pop(&s1)); display(&s1); printf("nThe deleted one is (-1: Underflow): %d",pop(&s1)); display(&s1); printf("nThe deleted one is (-1: Underflow): %d",pop(&s1)); display(&s1); printf("nThe deleted one is (-1: Underflow): %d",pop(&s1)); return 0; } STACK OPERATIONS (LOGIC)
  • 24. Implementation of STACK using Linked Lists Dr Somaraju Suvvari NITP -- CS3401 24
  • 25. Dr Somaraju Suvvari NITP -- CS3401 25 Implementation of STACK using Linked Lists • The other way of implementing the stack is using the Linked List (SLL/ CSLL/ DLL/ CDLL). • The push operation is the insertion of node into the linked list at the beginning. • The pop operation is the deletion of node from the beginning (the header/ top node)
  • 26. Dr Somaraju Suvvari NITP -- CS3401 26 STACK ADT (Linked List Based (SLL)) // Define node typedef struct Node STACK; struct Node { int data; // Assume we are storing integer data STACK *next; }*top = NULL; // Initially stack is empty // Define the set of operations on stack STACK* push(STACK *, int); STACK* pop(STACK *); void Display(STACK *) int Peak(STACK *);
  • 27. Dr Somaraju Suvvari NITP -- CS3401 27 Push operation PUSH(STACK *stack, Element Type value) • push() is a function used to insert an element into the stack. • The new element is always inserted at the beginning of the list and is pointed by top (In List it is head / start) . • Step 1 - create a new_node. If new_node creation failed, then display "Stack OVERFLOW!!! Insertion is not possible!!!" and terminate the function. • Step 2 - If stack is NOT FULL, then insert the value and assign NULL to new_nodenext. • Step 3 – if top == NULL, then assign new_node to top and terminate the function; • Step 4 – If top != NULL, then assign top to new_nodenext and new_node to top; • Step 5 – Return top; Time Complexity = O(1)
  • 28. PUSH(STACK *top, int value) x1000 new_node step1 top NULL step 2 step3 top 10 NULL new_node NULL x1000 x1000 step4 top 20 x3000 x2000 x500 30 NULL x3000 10 x1000 new_node x1000 NULL x2000 1 0 1. new_nodenext=top 2. top=new_node Dr Somaraju Suvvari NITP -- CS3401 28 10 NULL x1000 new_node x2000 x500 x500 // No Stack Overflow
  • 29. Dr Somaraju Suvvari NITP -- CS3401 29 STACK* push(STACK *top, int value) { STACK *new_node; new_node = (STACK *) malloc (sizeof(STACK)); if(new_node == NULL) printf("n Stack Overflow … “); else { new_node  data = value; new_nodenext = NULL; if(top == NULL) // This is the first node top = new_node; else // There exist some nodes { new_node next = top; top = new_node; printf("n Insertion success!!!"); } return top; } Time Complexity O(1) STACK OPERATION - PUSH (Logic)
  • 30. Dr Somaraju Suvvari NITP -- CS3401 30 Pop operation POP(STACK *top) • pop() is a function used to delete an element from the top of the stack. • The element is always deleted at the beginning of the list and is pointed by top (In List it is head / start) . • Step 1 - If top is NULL, then display "Stack UNDERFLOW!!! Deletion is not possible!!!" and terminate the function. • Step 2 - If stack is NOT EMPTY, then declare a pointer temp and assign top to temp. • Step 3 – if topnext == NULL, then assign NULL to top, delete the temp and terminate the function; • Step 4 – If topnext != NULL, then assign tempnext to top and delete temp; • Step 5 – Return top; Time Complexity = O(1)
  • 31. POP(STACK *top) step1 top step 2 top x2000 30 NULL x2000 Dr Somaraju Suvvari NITP -- CS3401 31 x500 x500 // Display Stack Underflow temp step3 NULL NULL top x500 step 4 20 x3000 x2000 top x500 x2000 30 NULL x3000 top x500 X3000 30 NULL x3000
  • 32. Dr Somaraju Suvvari NITP -- CS3401 32 STACK* pop(STACK *top) { int element; STACK *t; if(top == NULL) printf("n Stack Underflow”); else { element = topdata; t = top; top = top next; free(t); return element; } return top; } STACK OPERATION - POP (Logic)
  • 33. Dr Somaraju Suvvari NITP -- CS3401 33 void display(STACK *top) { if(top == NULL) printf("n Stack Underflow"); else { STACK *t = top; printf("n Stack elements are:n"); while(t != NULL) { printf(“%d ", tdata); t = tnext; } // while } // else } // display Time Complexity = O(n) STACK OPERATIONS (Logic) int peak(STACK *top) { int val; if (top = = NULL) printf(“n Stack Underflow”); else { val=top  data; return val; } } Time Complexity = O(1)
  • 34. Dr Somaraju Suvvari NITP -- CS3401 34 Parenthesis checking • {(((([]))))} // Check the parenthesis are balanced or not • if char is ‘(‘ or ‘{‘ or ‘[‘ then we push it onto the top of stack. • If char is ‘)’ or ‘}’ or ‘]’ then we perform pop operation and if the popped character is the matches with the starting bracket then fine otherwise parenthesis are not balanced and terminate the task. • If char is the end of string and if stack is empty, then it is balanced. • In other cases, it is said to imbalanced.
  • 35. Dr Somaraju Suvvari NITP -- CS3401 35 ASSIGNMENT (Graded one) 1. Write the algorithm/pseudo code to implement stack using doubly linked list. 2. Demonstrate how to check the balancing of the following parenthesis expressions using stack i. (a+b*(c/d)+e*(-f)) ii. a[(b*(c+d))] iii. a/(b/(c) 3. Write the algorithm/pseudo code for how to reverse the given string using the stack operations push and pop. Also demonstrate with an example. 4. Write the algorithm/pseudo code to convert the given decimal number into binary using the stack. Also demonstrate with an example. 5. Write the algorithm/pseudo code to check the given string is palindrome or not using stack. Also demonstrate with an example.
  • 36. Dr Somaraju Suvvari NITP -- CS3401 36 Notations – infix, prefix and postfix • Any arthematic expression consists of operators and operands. • The way we write the arithmetic expression is called notation; • There are three different notations used to write the arthematic expression. 1. Infix Expression 2. Prefix Expression 3. Postfix expression • We can convert the expression in one notation to another notation.
  • 37. Dr Somaraju Suvvari NITP -- CS3401 37 Infix Notation • An expression is said to be in infix notation if the operators in the expression are placed in between the operands on which the operator works. • For example - a + b * c • In the above example the operator is * is placed between the operands on which this operator works, here b and c. • Infix expressions are easy for humans to read, write and understand, but it is not the case for computing devices. • It is costly (in terms of space and time) to process the infix expressions in algorithms.
  • 38. Dr Somaraju Suvvari NITP -- CS3401 38 Prefix Notation • An expression is said to be in prefix notation if the operators in the expression are placed before the operands on which the operator works. • For example: +a*bc • In the above example the operator * is placed before the operands on which this operator works, here b and c, similarly the + is placed before a and the result of (b*c). • Prefix notation is also called as Polish notation.
  • 39. Dr Somaraju Suvvari NITP -- CS3401 39 Postfix Notation • An expression is said to be in postfix notation if the operators in the expression are placed after the operands on which the operator works. • For example - abc*+ • In the above example the operator * is placed after the operands on which this operator works, here b and c, similarly the + is placed after a and the result of (b*c). • Postfix notation is also called as Reverse Polish notation. • Widely used notation for evaluating the expressions.
  • 40. 40 Evaluation of expressions • When evaluating the arithmetic expression two things need be considered: 1. The precedence of the operator - If any operand is present in-between two different operators, the precedence (priority) of the one operator over the other decides which operator should use the operand first or which operator to be evaluated first. For example in the arithmetic expression a + b * c the operand b is surrounded by the operators * and +. In computer languages the * enjoying the higher precedence than the +, so * takes b first. 2. The Associativity of the operator - It resolves the ties between the same precedence of operators in the arithmetic expression, by considering whether the operators are evaluated from left to right or right to left. For example in the expression a* b * c, here b is surrounded by two * operators, So which * should take b first or which multiplication must be done first? It is decided by the associativity of *. So in computer languages * is a left associative, so the first * will be performed first (multiply and b first then the result will be multiplied by c). • It is not advantageous to evaluate the infix expressions, so first convert them to postfix or prefix and then compute the expressions. Dr Somaraju Suvvari NITP -- CS3401
  • 41. Dr Somaraju Suvvari NITP -- CS3401 41 Precedence and Associativity of operators Operator Description Associativity Precedence () [] .  ++ -- Parenthesis 1 (function calls) Brackets (array subscript) Members selection via object name Member selection via pointer Post increment and post decrement Left to Right 14 ++ -- + - ! ~ (type) * & Sizeof Pre increment and pre decrement Unary plus or minus Logical negation and bitwise complement Casting type Dereference Address Size in bytes Right to Left 13 * / % Arithmetic multiplication, division, modulus Left to Right 12 + - Arithmetic addition, subtraction Left to Right 11 << >> Bitwise shift left, shift right Left to Right 10 < <= > >= Relational less than, less than or equal to Relational greater than, greater than or equal to Left to Right 9 1. Parenthesis are used to override the precedence of operators and such parenthesis expressions can be nested and evaluated from inner to outer
  • 42. 42 Precedence and Associativity of operators Dr Somaraju Suvvari NITP -- CS3401 Operator Description Associativity Precedence = = != Relational equal to, not equal to Left to Right 8 & Bitwise AND Left to Right 7 ^ Bitwise Exclusive OR Left to Right 6 | Bitwise Inclusive OR Left to Right 5 && Logical AND Left to Right 4 || Logical OR Left to Right 3 ? : Ternary condition Right to Left 2 = += -+ *= /= %= &= ^= |= <<= >>= Assignment Addition/Subtraction Assignment Multiplication/Division Assignment Modulus/Bitwise AND assignment Bitwise Exclusive/ Inclusive Assignment Bitwise Shift Left/Right Assignment Right to Left 1 , Comma Left to Right 0 1. Parenthesis are used to override the precedence of operators and such parenthesis expressions can be nested and evaluated from inner to outer
  • 43. 43 Infix to postfix Conversion 1. Read all the symbols one by one from left to right in the given Infix Expression. 2. If the reading symbol is operand, then immediately send it to the output. 3. If the reading symbol is left parenthesis '(', then Push it on to the Stack. 4. If the reading symbol is right parenthesis ')', then Pop all the contents of stack until respective left parenthesis is popped and print each popped symbol to the output. 5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it on to the Stack. However, 1. The associativity of the operators (current operator and the operator on top of the stack) is left to right then first pop the operators which are already on the stack that have higher or equal precedence than the current operator then pop it and send it to output, if open parenthesis is there on top of the stack then push the operator into stack (even though the precedence of ‘(’ is more than any other operator and it is the exceptional case). 2. The associativity of the operators (current operator and the operator on top of the stack) is right to left then first pop the operators which are already on the stack that have lower precedence than the current operator then pop it and send it to output, if open parenthesis is there on top of the stack then push the operator into stack (even though the precedence of ‘(’ is more than any other operator and it is the exceptional case) 6. If the input is over, so pop all the remaining symbols from the stack and output them. Dr Somaraju Suvvari NITP -- CS3401
  • 44. Dr Somaraju Suvvari NITP -- CS3401 44 Infix to postfix Conversion (Example) Consider the infix arithmetic expression: a * (b + c + d) Input (infix expression) a * (b + c + d) top a STACK Output (postfix expression) 1 2 a is an operand Action - Output a Input (infix expression) a * (b + c + d) * top STACK a Output (postfix expression) • * is an operator, • Action - push it into stack after removing the higher or equal priority operators from the top of the stack
  • 45. Dr Somaraju Suvvari NITP -- CS3401 45 Infix to postfix Conversion (Example) Input (infix expression) a * (b + c + d) ( * top a STACK Output (postfix expression) 3 4 b is an operand, so output b Input (infix expression) a * (b + c + d) ( * top STACK a b Output (postfix expression) • ( is an open parenthesis • Action - push it into stack * top STACK
  • 46. Dr Somaraju Suvvari NITP -- CS3401 46 Infix to postfix Conversion (Example) Input (infix expression) a * (b + c + d) + ( * top a b STACK Output (postfix expression) 5 6 c is an operand, so output c Input (infix expression) a * (b + c + d) + ( * top STACK a b c Output (postfix expression) • + is an operator, • Action - push it into stack after removing the higher or equal priority operators from the top of the stack. Present operator on stack is ( and it has higher precedence than +, but for ( we have exception, so push + into stack,
  • 47. Dr Somaraju Suvvari NITP -- CS3401 47 Infix to postfix Conversion (Example) Input (infix expression) a * (b + c + d) ( * top a b c + STACK Output (postfix expression) 7 + ( * top STACK a b c + Output (postfix expression) • + is an operator, • Action - push it into stack after removing the higher or equal priority operators from the top of the stack. Present operator on stack is + and it has equal precedence with the input symbol +, so pop the + from stack and output. • + is an operator, • Action – Now present top of the stack is ( and it has higher precedence than input symbol +, +, but for ( we have an exception, so push + into stack + ( * top STACK
  • 48. Dr Somaraju Suvvari NITP -- CS3401 48 Infix to postfix Conversion (Example) Input (infix expression) a * (b + c + d) + ( * top a b c + d STACK Output (postfix expression) 8 9 d is an operand, so output d Input (infix expression) a * (b + c + d) * top STACK a b c + d + Output (postfix expression) • ) is an operator, • Action – Pop all the contents of stack until respective left parenthesis is popped and send each popped symbol to the output.
  • 49. Dr Somaraju Suvvari NITP -- CS3401 49 Infix to Postfix Conversion (Example) Input (infix expression) a * (b + c + d) top a b c + d + * STACK Output (postfix expression) 10 Input is over so pop all the remaining symbols from the stack and output them.
  • 50. Dr Somaraju Suvvari NITP -- CS3401 50 #include<stdio.h> #include<string.h> #include<ctype.h> #define size 100 char stack[size]; int top=-1; void push(char *, int, char); char pop(char *, int); int prio(char op); int prio(char op) { if(op = = '+’ || op= = '-') return 1; if(op = = '*' || op = = ‘/’ || op==‘%’) return 2; if(op == ‘(‘ ) return 0; else {printf(“Wrong operator”); exit(0);} } // Assume our expression contains only these operators void push(char *stack, int top, char e) { if(top==size-1) printf("n stack overflow"); else stack[++top]=e; } char pop(char *stack, int top) { if(top==-1) printf("n stack is underflow"); else return stack[top--]; } Infix to Postfix conversion (Logic)
  • 51. Dr Somaraju Suvvari NITP -- CS3401 51 void main() { char ie[100], pe[100]; int i, j=0; printf("n Enter infix expression: "); gets(ie); for(i=0; ie[i]!='0’; i++) { if(ie[i]=='(‘) push(ie[i]); // open parenthesis is pushed into the stack else if( (ie[i] >= ‘a’ && ie[i] <= ‘z’) || (ie[i] >= ‘0’ && ie[i] <= ‘9’)) {pe[j++]=ie[i]; } //operand else if(ie[i]==‘)’) // if it is closed parenthesis then pop all symbols from the stack upto open parenthesis encountered in the stack {while(stack[top]!=‘(‘) { pe[j++]=pop(stack, top); } // Assume the given infix expression is parenthesis balanced one pop(); } else // if it is an operator { while(top > -1 && (prio(stack[top])>=prio(ie[i]) ) {pe[j++]=pop();} push(ie[i]); } } while(top>-1) {pe[j++]=pop(); } // Leftover symbols from the stack are popped pe[j]='0’; printf("the infix expression %s converted to %s postfix expression",ie, pe); } Infix to Postfix conversion (Logic) (Considered only operators which has left to right associativity)
  • 52. 52 Infix to Prefix Conversion 1. Reverse the given infix expression, for example the reverse of the infix expression a + b *c is c * b + a. When reversing the parenthesis ‘)’ will become ‘(‘ and ‘(‘ will become ‘)’ (applicable to all types of brackets) 2. Apply the infix to postfix conversion algorithm on the reversed infix expression. For the above example the resultant postfix expression is: cb*a+ 3. Reverse the obtained postfix expression, and is the required prefix expression for the given infix expression. For the above example, the infix expression is +a*bc Dr Somaraju Suvvari NITP -- CS3401
  • 53. 53 Prefix to Infix Conversion 1. Reverse the given prefix expression, for example the reverse of the prefix expression *cd is dc* 2. Read the character by character of the reversed infix expression and repeat the step 3 and 4 till there are no characters in the reversed prefix expression . 3. If the character read is an operand then push the operand into the stack. (push d and c) 4. If the character read is an operator, op, then pop the top two symbols from the stack, the first one is p1, and the second one is p2. Push the concatenated string p1 op p2 to stack. (push c *d ) 5. Now the value in the stack is the required infix expression. (c*d) Dr Somaraju Suvvari NITP -- CS3401
  • 54. 54 Prefix to Postfix Conversion 1. Reverse the given prefix expression, for example the reverse of the prefix expression *cd is cd*. 2. Read the character by character of the reversed infix expression and repeat the step 3 and 4 till there are no characters in the reversed prefix expression . 3. If the character read is an operand then push the operand into the stack. (push d and c) 4. If the character read is an operator, op, then pop the top two symbols from the stack, the first one is p1, and the second one is p2. Push the concatenated string p1 p2 op to stack. (push c d * ) 5. Now the value in the stack is the required postfix expression. (c d *) Dr Somaraju Suvvari NITP -- CS3401
  • 55. 55 Postfix to Infix Conversion 1. Read the character by character of the given postfix expression and repeat the step 2 and 3 till there are no characters in the postfix expression. For example assume the postfix expression ab+; 2. If the character read is an operand then push the operand into the stack. (push a and b) 3. If the character read is an operator, op, then pop the top two symbols from the stack, the first one is p1, and the second one is p2. Push the concatenated string p2 op p1 to stack. (push a * b ) 4. Now the value in the stack is the required infix expression. (a * b) Dr Somaraju Suvvari NITP -- CS3401
  • 56. 56 Postfix to Prefix Conversion 1. Read the character by character of the given postfix expression and repeat the step 2 and 3 till there are no characters in the postfix expression. For example assume the postfix expression ab+; 2. If the character read is an operand then push the operand into the stack. (push a and b) 3. If the character read is an operator, op, then pop the top two symbols from the stack, the first one is p1, and the second one is p2. Push the concatenated string op p2 p1 to stack. (push a * b ) 4. Now the value in the stack is the required postfix expression. (*a b) Dr Somaraju Suvvari NITP -- CS3401
  • 57. Dr Somaraju Suvvari NITP -- CS3401 57 Examples S.No Infix Expression Prefix Expression Postfix Expression 1 a + b * c +a*bc a b c*+ 2 (a + b) * (c + d) * + a b + c d a b + c d + * 3 a * b + c * d + * a b * c d a b * c d * + 4 a + b + c + d + + + a b c d a b + c + d + 5 (a + b) * c – d -*+a b c d ab + c * d- 6 a + b - c -+a b c ab + c -
  • 58. 58 Evaluation of Postfix Expression Algorithm 1. Scan the symbols one by one from left to right in the given Postfix Expression 2. If the reading symbol is operand, then push it on to the Stack. 3. If the reading symbol is binary operator (+ , - , * , / etc.,), then perform two pop operations and do the operation specified by the reading symbol (if it is * do multiplication, etc) using the two popped operands and push the result back on to the stack. 4. Repeat steps 2 & 3 till the postfix expression completes. 5. Finally! perform a pop operation and display the popped value as final result. Dr Somaraju Suvvari NITP -- CS3401 Infix exp: ( 5 + 3 ) * ( 8 -2) Postfix: 5 3 + 8 2 - *
  • 59. Dr Somaraju Suvvari NITP -- CS3401 59 Evaluations of postfix Conversion (Example) Reading Stack Symbol Initially 6 (operand) push(stack, top, 6) Infix exp: ( 6 + 2 ) * ( 8 - 2) Postfix: 6 2 + 8 2 - * t o p t o p 6 Reading Stack Symbol 2 (operand) push(stack, top, 2) + (operator) pop the top two two operands from Stack op1 = pop(stack, top) //2 op2 = pop(stack, top) // 6 op3 = op2 + op1 // 8 push(stack, top, op3) t o p 6 2 t o p Reading Stack Symbol 8 (operand) push(stack, top, 8) 2 (operand) push(stack, top, 2) 8 t o p 8 8 t o p 8 8 2
  • 60. Dr Somaraju Suvvari NITP -- CS3401 60 Evaluations of postfix Conversion (Example) Infix exp: ( 6 + 2 ) * ( 8 - 2) Postfix: 6 2 + 8 2 - * Reading Stack Symbol - (operator) pop the top two two operands from stack op1 = pop(stack, top) //2 op2 = pop(stack, top) //8 op3 = op2 - op1 // 6 push(stack, top, op3) * (operator) pop the top two operands from stack op1 = pop(stack, top) // 6 op2 = pop(stack, top // 8 op3 = op2 * op1 push(stack, top, op3) t o p 48 t o p 8 6 The final value is : 48
  • 61. Postfix Evaluation #include<stdio.h> #include<string.h> #include<ctype.h> #define size 100 int stack[size]; int top=-1; void push(int); int pop(); void main() { char pe[30]; int i,v,op1,op2; printf("n enter any postfix expression"); gets(pe); for(i=0;pe[i]!='0';i++) { if(isalpha(pe[i])) {printf("n enter value for %c", pe[i]); scanf("%d",&v); push(v); } else if( isdigit(pe[i])) push(pe[i]-'0'); else { op2=pop(); op1=pop(); switch(pe[i]) {case '+‘ : push(op1+op2);break; case '-‘ : push(op1-op2);break; case '*‘ : push(op1*op2);break; case '/': push(op1/op2);break; case '%‘ : push(op1%op2);break; default: printf("n invalid operation"); } } } printf(“n The result is: %d",stack[top]); } void push(int e) { If(top==size-1) printf("n stack overflow"); else stack[++top]=e; } int pop() { if(top==-1) printf("n stack underflow"); else return stack[top--]; } Postfix: X 4 + Y Z - * X= 5, Y=8, Z=2
  • 62. 62 Thank You Dr Somaraju Suvvari NITP -- CS3401