0% found this document useful (0 votes)
300 views7 pages

Experiment Title.: Infix To Postfix Notation

This document describes an experiment to convert arithmetic expressions from infix notation to postfix notation using a stack. It includes an overview of the practical, the algorithm, code to implement the conversion, and a discussion of the time complexity. The code takes an infix string as input, uses a stack to process it from left to right, and outputs the equivalent postfix string. It has a time complexity of O(N) to process the input of length N.

Uploaded by

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

Experiment Title.: Infix To Postfix Notation

This document describes an experiment to convert arithmetic expressions from infix notation to postfix notation using a stack. It includes an overview of the practical, the algorithm, code to implement the conversion, and a discussion of the time complexity. The code takes an infix string as input, uses a stack to process it from left to right, and outputs the equivalent postfix string. It has a time complexity of O(N) to process the input of length N.

Uploaded by

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

Experiment Title.

: Infix to Postfix Notation

Student Name: Harsh Bhardwaj UID: 19BCS1468


Branch: CSE Section/Group : 3(A)
Semester: 3 Date of Performance:
Subject Name: Data Structure Lab Subject Code: CSP-231

1. Aim/Overview of the practical:

Write a program to demonstrate the use of stack (Implemented using linear array) in
converting arithmetic expression from infix notation to postfix notation.

2. Task to be done:
We have to write a code which take input infix notation and after converting it into
Postfix notation display it.
3. Algorithm/Flowchart :

Step 1 : Scan the Infix Expression from left to right.


Step 2 : If the scanned character is an operand, append it with final Infix to Postfix string.
Step 3 : Else,
Step 3.1 : If the precedence order of the scanned(incoming) operator is greater than
the precedence order of the operator in the stack (or the stack is empty or the stack contains
a ‘(‘ or ‘[‘ or ‘{‘), push it on stack.
Step 3.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.)
Step 4 : If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack.
Step 5 : If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and and output it until a
‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis.
Step 6 : Repeat steps 2-6 until infix expression is scanned.
Step 7 : Print the output
Step 8 : Pop and output from the stack until it is not empty.

4. Code for experiment/practical:

#include<iostream>
#include<string>
#define MAX 50
using namespace std;

class Stack
{
public:
char s[50];
int top;

Stack()
{
top=-1;
}

void push(char o)
{
if(top==MAX-1)
{
cout<<"Over Flow !!!";
}

else
{
top++;
s[top]=o;
}
}

char pop()
{
char ch;
if(top==-1)
{
cout<<"Under Flow !!!";
}
else
{
ch=s[top];
s[top]='\0';
top--;
return(ch);
}
return 0;
}

char Top()
{
if(top<0)
{
return 0;
}
else
{
char c=s[top];
return c;
}

}
};

int priority (char a)


{
if(a == '+' || a =='-')
{
return(1);
}

if(a == '*' || a =='/')


{
return(2);
}

return 0;
}

string convert(string in)


{
Stack o;
int i=0;
string p ="";
while(in[i]!='\0')
{
if(in[i]>='a' && in[i]<='z'|| in[i]>='A'&& in[i]<='Z')
{
p.insert(p.end(),in[i]);
i++;
}
else if(in[i]=='(' || in[i]=='{' || in[i]=='[')
{
o.push(in[i]);
i++;
}
else if(in[i]==')' || in[i]=='}' || in[i]==']')
{
if(in[i]==')')
{
while(o.Top()!='(')
{
p.insert(p.end(),o.pop());
}
o.pop();
i++;
}
if(in[i]==']')
{
while(o.Top()!='[')
{
p.insert(p.end(),o.pop());
}
o.pop();
i++;
}

if(in[i]=='}')
{
while(o.Top()!='{')
{
p.insert(p.end(),o.pop());
}
o.pop();
i++;
}
}
else
{
if(o.top==-1)
{
o.push(in[i]);
i++;
}

else if( priority(in[i]) <= priority(o.Top()))


{
p.insert(p.end(),o.pop());

while(priority(o.Top()) == priority(in[i]))
{
p.insert(p.end(),o.pop());
if(o.top < 0)
{
break;
}
}
o.push(in[i]);
i++;
}
else if(priority(in[i]) > priority(o.Top()))
{
o.push(in[i]);
i++;
}
}
}
while(o.top!=-1)
{
p.insert(p.end(),o.pop());
}

return p;
}

int main()
{
string in,p;
cout<<"Enter the infix expression:"<<endl;
cin>>in;
p = convert(in);
cout<<"The Converted postfix string is:"<<endl<<p;
return 0;
}
5.Discussion /Observation/Complexity :

The time complexity of an infix to postfix expression conversion algorithm is mathematically


found to be O(N). ... Parentheses are simply ignored in the conversion of infix to
postfix expression. Explanation: When a parenthesis is encountered, it is placed on the operator
stack.

6. Result/Output/Writing Summary:

Learning outcomes (What I have learnt):

1.Learn about Infix Notation.

2. Learn about Postfix Notation.

3. Learn about Stack.

4. Learn how to convert Infix Notation to Postfix Notation.

Evaluation Grid:

Sr. No. Parameters Marks Obtained Maximum Marks


1. Demonstration and Performance 5
(Pre Lab Quiz)
2. Worksheet 10
3. Post Lab Quiz 5

You might also like