A stack is a restricted data structure, because only a small number of operations are performed on it. Elements are removed from the stack in the reverse order to the order of their addition.  For more information on stack, go through this article.

In this article here, we’ll discuss real world applications of stack. First we’ll see infix to postfix conversion. What is actually done is in infix to postfix, an expression is scanned from user in infix form; it is converted into prefix or postfix form and then evaluated without considering the parenthesis and priority of the operators.

Algorithm for Infix to Postfix

  1. Examine the next element in the input.
  2. If it is operand, output it.
  3. If it is opening parenthesis, push it on stack.
  4. If it is an operator, then
    • If stack is empty, push operator on stack.
    • If the top of stack is opening parenthesis, push operator on stack
    • If it has higher priority than the top of stack, push operator on stack.
    • Else pop the operator from the stack and output it, repeat step 4
  5. If it is a closing parenthesis, pop operators from stack and output them until an opening parenthesis is encountered. pop and discard the opening parenthesis.
  6. If there is more input go to step 1
  7. If there is no more input, pop the remaining operators to output.

Example:

Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form

Evaluating a postfix expression

  • —Use a stack to evaluate an expression in postfix notation
  • The postfix expression to be evaluated is scanned from left to right
  • Each operator in a postfix string refers to the previous two operands in the string
  • —Each time we read an operand we push it into a stack. When we reach an operator, its operands will then be top two elements on the stack
  • We can then pop these two elements, perform the indicated operation on them, and push the result on the stack
  • —So that it will be available for use as an operand of the next operator
  • —Initialize an empty stack
  • —While character remain in the input stream
    • Read next character
    • If character is an operand, push it into the stack
    • Else, if character is an operator, pop top two characterss off the stack, apply the operator, and push the answer back into the stack
  • Pop the answer off the stack

Picture below depicts postfix expression using stack

Algorithm for evaluating a postfix expression

WHILE more input items exist
{
  If symb is an operand
  then push (operand_stk, symb)
  else   //symbol is an operator
  {
  Opnd1=pop(operand_stk);
  Opnd2=pop(operand_stk);
  Value = opnd2 symb opnd1
  Push(operand_stk, value);
  }  //End of else
} // end while
Result = pop (operand_stk);

Related Posts

Tagged with: C/C++ languageData structures
 

4 Responses to Applications of Stack in data structures

  1. gapsv says:

    Its too useful for me thanks for this type of information is published.

  2. usman cheema says:

    Very Good approach…
    Have great ideas about stack…

  3. siva says:

    very useful to all…

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds