You are on page 1of 6

INFIX TO POSTFIX COVERSION USING

STACKS

Divyam Agrawal
CSE
20103088
The conversion of Infix expression to postfix expression is easily performed by
using Stack linear data structure. The following algorithm is followed to perform
the conversion:-

1. Scan the infix expression from left to right.


2. If the scanned character is an operand, output it.
3. Else,
1 If the precedence of the scanned operator is greater than the
precedence of the operator in the stack(or the stack is empty or
the stack contains a ‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater
than or equal to in precedence than that of the scanned operator.
After doing that Push the scanned operator to the stack. (If you
encounter parenthesis while popping then stop there and push the
scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and output it
until a ‘(‘ is encountered, and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
TIME AND SPACE COMPLEXITY

• TIME COMPLEXITY
The algorithm runs across the stack once per element in the stack , but if it
encounters a closing bracket or an operator , it runs a backward loop until
a specified condition is fulfilled. So the time complexity is ,

Time complexity : - 𝑶(𝒏𝟐 )

• SPACE COMPLEXITY
The algorithm requires only a stack to store the input string of infix
expression and depends on the size of the input. It also requires an
additional variable irrespective of the size of input. So the space
complexity is,

Space Complexity : - 𝑶(𝒏)


CODE IMPLEMENTATION

#include<bits/stdc++.h>
using namespace std;

class Stack
{
public:
char Stack[1000];
int t = 0;
void push(char x)
{
t++;
Stack[t-1] = x;
}
void pop()
{
t--;
}
char top()
{
char a = Stack[t-1];
return a;
}
bool Empty()
{
bool d;
if(t==0)
{
d = true;
}
else
{
d = false;
}
return d;
}
};

int prec(char a)
{
int s;
if(a == '^')
{
s = 3;
}
else if(a == '/' || a == '*')
{
s = 2;
}
else if(a == '+' || a== '-')
{
s = 1;
}
else
{
s = -1;
}
return s;
}

void intopost(string s)
{
Stack s1;
string result;
for(int i =0; i<s.size() ; i++)
{
char c = s[i];

if((tolower(c) >= 'a' && tolower(c) <= 'z')||(c >= '1'


&& c <= '9'))
{
result+=c;
}
else if (c == '(')
{
s1.push(c);
}
else if(c == ')')
{
while(s1.top() != '(')
{
result+=s1.top();
s1.pop();
}
s1.pop();
}
else
{
while(!s1.Empty() && prec(c)<=prec(s1.top()))
{
result+=s1.top();
s1.pop();
}
s1.push(c);
}

while(!s1.Empty())
{
result+=s1.top();
s1.pop();
}

cout<<" "<<result<<endl<<endl;
}

int main()
{
string s;
cout<<" Infix Expression : ";
s = "a+b*c^d/e-f";
cout<<s<<endl<<" Postfix Expression : ";
intopost(s);
cout<<" Infix Expression : ";
s = "a+b*(c^d-e)^(f+g*h)-i";
cout<<s<<endl<<" Postfix Expression : ";
intopost(s);
cout<<endl<<endl<<" Divyam Agrawal"<<endl<<" CSE"<<endl<<"
20103088";
return 0;
}
RESULTS

You might also like