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

2 - Infixtopostfix

The document discusses stacks, including what a stack is, its properties and common operations like push, pop, peek etc. It also discusses applications of stacks like balancing symbols, string reversal, recursion etc. and provides an example program to convert an infix notation arithmetic expression to postfix notation using a stack implemented as a linear array.

Uploaded by

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

2 - Infixtopostfix

The document discusses stacks, including what a stack is, its properties and common operations like push, pop, peek etc. It also discusses applications of stacks like balancing symbols, string reversal, recursion etc. and provides an example program to convert an infix notation arithmetic expression to postfix notation using a stack implemented as a linear array.

Uploaded by

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

STACK

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.

The following are some common operations Implemented on the stack:


 push(): When we insert an element in a stack then the operation is known as a push. If
the stack is full then the overflow condition occurs.
 pop(): When we delete an element from the stack, the operation is known as a pop. If the
stack is empty means that no element exists in the stack, this state is known as an
underflow state.
 isEmpty(): It determines whether the stack is empty or not.
 isFull(): It determines whether the stack is full or not.'
 peek(): It returns the element at the given position.
 count(): It returns the total number of elements available in a stack.
o change(): It changes the element at the given position.
o display(): It prints all the elements available in the stack.

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.

Why postfix representation of the expression?

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.

How to convert an Infix expression to a Postfix expression?

Below are the steps to implement the above idea:


1. Scan the infix expression from left to right.
2. If the scanned character is an operand, put it in the postfix expression.
3. Otherwise, do the following
 If the precedence and associativity of the scanned operator are greater than the
precedence and associativity of the operator in the stack [or the stack is empty or
the stack contains a ‘(‘ ], then push it in the stack. [‘^‘ operator is right associative
and other operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].
Check especially for a condition when the operator at the top of the stack
and the scanned operator both are ‘^‘. In this condition, the precedence of
the scanned operator is higher due to its right associativity. So it will be
pushed into the operator stack.
In all the other cases when the top of the operator stack is the same as the
scanned operator, then pop the operator from the stack because of left
associativity due to which the scanned operator has less precedence.
 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 a ‘(‘, push it to the stack.
5. If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and
discard both the parenthesis.
6. Repeat steps 2-5 until the infix expression is scanned.
7. Once the scanning is over, Pop the stack and add the operators in the postfix expression
until it is not empty.
8. Finally, print the postfix expression.

Additional Examples of Infix, Prefix, and Postfix


Infix Expression Prefix Expression Postfix Expression
A+B*C+D ++A*BCD ABC*+D+
(A + B) * (C + D) *+AB+CD AB+CD+*
A*B+C*D +*AB*CD AB*CD*+
A+B+C+D +++ABCD AB+C+D+

.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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy