2 - Infixtopostfix
2 - Infixtopostfix
What is stack?
A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an
abstract data type. A Stack works on the LIFO process (Last In First Out), i.e., the element that
was inserted last will be removed first.
First, let us see the properties of data structures that we already do know and build-up our
concepts towards the stack.
Array: Its a random-access container, meaning any element of this container can be
accessed instantly
Linked List: It's a sequential-access container, meaning that elements of this data
structure can only be accessed sequentially
Following a similar definition, a stack is a container where only the top element can be accessed
or operated upon.
Applications of Stack
The following are the applications of the stack:
Balancing of symbols: Stack is used for balancing a symbol. For example, we have
the following program:
int main()
{
cout<<"Hello";
cout<<"javaTpoint";
}
As we know, each program has an opening and closing braces; when the opening braces come,
we push the braces in a stack, and when the closing braces appear, we pop the opening braces
from the stack. Therefore, the net value comes out to be zero. If any symbol is left in the stack, it
means that some syntax occurs in a program.
String reversal: Stack is also used for reversing a string.
UNDO/REDO: It can also be used for performing UNDO/REDO operations.
Recursion: The recursion means that the function is calling itself again. To maintain
the previous states, the compiler creates a system stack in which all the previous
records of the function are maintained.
DFS (Depth First Search): This search is implemented on a Graph, and Graph uses
the stack data structure.
Backtracking: Suppose we have to create a path to solve a maze problem. If we are
moving in a particular path, and we realize that we come on the wrong way. In order
to come at the beginning of the path to create a new path, we have to use the stack
data structure.
Expression conversion: Stack can also be used for expression conversion. This is
one of the most important applications of stack. The list of the expression conversion
is given below:
Infix to prefix
Infix to postfix
Prefix to infix
Prefix to postfix
Postfix to infix
o Memory management: The stack manages the memory. The memory is assigned in the
contiguous memory blocks. The memory is known as stack memory as all the variables
are assigned in a function call stack memory. The memory size assigned to the program is
known to the compiler. When the function is created, all its variables are assigned in the
stack memory. When the function completed its execution, all the variables assigned in
the stack are released.
Convert Infix to Postfix notation
Infix expression:
The expression of the form “a operator b” (a + b) i.e., when an operator is in-between every pair
of operands.
Postfix expression:
The expression of the form “a b operator” (ab+) i.e., When every pair of operands is followed by
an operator.
The compiler scans the expression either from left to right or from right to left.
Consider the expression: a + b * c + d
The compiler first scans the expression to evaluate the expression b * c, then again scans
the expression to add a to it.
The result is then added to d after another scan.
The repeated scanning makes it very inefficient. Infix expressions are easily readable and
solvable by humans whereas the computer cannot differentiate the operators and parenthesis
easily so, it is better to convert the expression to postfix (or prefix) form before evaluation.
.2. Write a program to demonstrate the use of stack (implemented using linear array) in converting
arithmetic expression from infix notation to postfix notation
Aim:
To develop a program to demonstrate the use of stack (implemented using linear array) in
converting arithmetic expression from infix notation to postfix notation.
Program:
#include<iostream>
#include<stack>
using namespace std;
//Variable declaration
int precedence(char op)
{
if(op == '^')
return 3;
else if(op == '*' || op == '/')
return 2;
else if(op == '+' || op == '-')
return 1;
return -1;
}
//Method to convert infix to postfix
string infixToPostfix(string infix)
{
stack<char>s;
string postfix = "";
for(int i = 0; i<infix.length(); i++)
{
char c = infix[i];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
postfix += c;
} elseif(c == '(')
{
s.push(c);
} else if(c == ')')
{
while(!s.empty() &&s.top() != '(')
{
postfix += s.top();
s.pop();
}
if(!s.empty())
s.pop();
}
else
{
while(!s.empty() && precedence(s.top()) >= precedence(c))
{
postfix += s.top();
s.pop();
}
s.push(c);
}
}
while(!s.empty())
{
postfix += s.top();
s.pop();
}
return postfix;
}
//main method
int main()
{
string infix;
cout<< "Enter infix expression: ";
cin>>infix;
string postfix = infixToPostfix(infix);
cout<< "Postfix expression: " << postfix <<endl;
return 0;
}
Output: