0% found this document useful (0 votes)
218 views21 pages

Stacks: Stack: Definitions, Primitive Operations

Stacks can be used to convert infix expressions to postfix form and then evaluate postfix expressions. A stack implements a LIFO data structure using an array. Elements are pushed onto and popped off the top of the stack. Converting an infix expression to postfix involves reading tokens from left to right and outputting operands or pushing/popping operators based on precedence. Evaluation of a postfix expression involves pushing operands onto a stack and popping operands to apply operators.

Uploaded by

Priyanshu Dimri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
218 views21 pages

Stacks: Stack: Definitions, Primitive Operations

Stacks can be used to convert infix expressions to postfix form and then evaluate postfix expressions. A stack implements a LIFO data structure using an array. Elements are pushed onto and popped off the top of the stack. Converting an infix expression to postfix involves reading tokens from left to right and outputting operands or pushing/popping operators based on precedence. Evaluation of a postfix expression involves pushing operands onto a stack and popping operands to apply operators.

Uploaded by

Priyanshu Dimri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Stacks

Stack: Definitions, Primitive operations


Push
Pop
Applications:
Conversion of Infix Expression to Postfix
form
Evaluation of Postfix Expressions
Recursion and Stacks
What is a stack?
Stores a set of elements in a particular order
Stack principle: LAST IN FIRST OUT
= LIFO
It means: the last element inserted is the first one to be
removed
Example

Which is the first element to pick up?


Last In First Out

E top
D top D D
C top C C C
B top B B B B
A top A A A A A
Array-based Stack Implementation

Allocate an array of some size (pre-defined)


Maximum N elements in stack
Bottom stack element stored at element 0
last index in the array is the top
Increment top when one element is pushed,
decrement after pop
Stack Implementations
#include <stdio.h>
#define MAXSIZE 5  
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;  
void push(void);
int pop(void);
void display(void);
Stack Implementations
void main ()
{
int choice;
int option = 1;
s.top = -1;

printf ("STACK OPERATION\n");


while (option)
{
printf ("------------------------------------------\n");
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");

printf ("Enter your choice\n");


scanf ("%d", &choice);
Stack Implementations
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
}
Stack Implementations
/* Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
Stack Implementations
/* Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
Stack Implementations
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
Applications

Infix to Postfix conversion


[Evaluation of Expressions]
Evaluation of Expressions

X=a/b-c+d*e-a*c

a = 4, b = c = 2, d = e = 3

Interpretation 1:
((4/2)-2)+(3*3)-(4*2)=0 + 8+9=1

Interpretation 2:
(4/(2-2+3))*(3-4)*2=(4/3)*(-1)*2=-2.66666…

How to generate the machine instructions corresponding to a


given expression?
precedence rule + associative rule
User Compiler

Postfix: no parentheses, no precedence


Conversion of an Infix Expression into Postfix Form
We are reading token from Left to Right and Postfix
expression is generated.
Entered Token may be –
Alphabet from A-Z or a-z
Numerical Digit from 0-9
Operator
Opening And Closing Braces ( , )
1. If Entered Character is Alphabet then Following Action Should
be taken- Print Alphabet as Output
2. If Entered Character is Digit then Following Action Should be
taken- Print Digit as Output
3. If Entered Character is Opening Bracket then Following Action
Should be taken-
Push ‘(‘ Onto Stack.
If any Operator Appears before ‘)’ then Push it onto Stack.
If Corresponding ‘)’ bracket appears then Start Removing
Elements [Pop] from Stack till ‘(‘ is removed.
Conversion of an Infix Expression into Postfix Form

4. If Entered Character is Operator then Following Action


Should be taken-
Check whether there is any Operator already present in
Stack or not.
If Stack is Empty then Push Operator Onto Stack.
If Present then Check Whether Priority of Incoming
Operator is greater than Priority of Topmost Stack
Operator.
If Priority of Incoming Operator is Greater then Push
Incoming Operator Onto Stack.
Else Pop Operator From Stack again goto Step 4.
Example

• 3+4*5/6
• (Postfix: 3 4 5 * 6 / +)

• (300+23)*(43-21)/(84+7)
• (Postfix: 300 23 + 43 21 - * 84 7 + /)
Evaluation of Postfix Expression
1. Read the Postfix Expression from left to right.
2. If an operand is encountered PUSH it onto the STACK.
3. If an operator (⊕) is encountered
a. POP the top operand from the STACK and name it
as B
b. POP the next operand from the STACK and name it
as A
c. Perform C=A ⊕B
d. PUSH ‘C’ onto the STACK
4. Repeat steps 1-3 until end of the postfix expression.
5. POP the only element available on the STACK, this is
the value of the postfix expression.
Evaluation of Postfix Expression
Example 1
• Infix expression = 3+2*9-5
• Postfix expression = 3 2 9 * + 5 -
• Answer = 16
Example 2
• Infix Expression: (300+23)*(43-21)/(84+7)
• Postfix Expression: 300 23 + 43 21 - * 84 7 + /
• Answer = 78.08791208791209
Example 3
• Infix Expression: 300+23-20*(43-21)
• Postfix Expression: 300 23 + 20 43 21 - * -
• Answer = -117

You might also like