cse 246 data structures and algorithms n.
Skip this Video
Loading SlideShow in 5 Seconds..
CSE 246 Data Structures and Algorithms PowerPoint Presentation
Download Presentation
CSE 246 Data Structures and Algorithms

Loading in 2 Seconds...

play fullscreen
1 / 23

CSE 246 Data Structures and Algorithms - PowerPoint PPT Presentation

  • Uploaded on

CSE 246 Data Structures and Algorithms. Spring2011 Lecture#11. Stack Application. Run time Stack procedures Postfix Calculator Interpret infix with precedence. Stack Application.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'CSE 246 Data Structures and Algorithms' - brygid

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
stack application
Stack Application
  • Run time Stack procedures
  • Postfix Calculator
  • Interpret infix with precedence


stack application1
Stack Application
  • Almost invariably, programs compiled from modern high level languages (even C!) make use of a stack frame for the working memory of each procedure or function invocation.
  • When any procedure or function is called, a number of words - the stack frame - is pushed onto a program stack.


arithmetic expression
Arithmetic Expression
  • Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions.


arithmetic expressions
Arithmetic Expressions

Infix Expressions

An expression in which every binary operation appears between its operands


(i) a+b “+” is a binary operation and a and b are its operands

(ii) (a+b)*c

Prefix Expressions

An expression in which operator comes before its operands


(i) a+b = +ab

(ii) (a+b)*c = *+abc

(iii) a+(b*c) =+a*bc

Postfix Expressions

An expression in which operator comes after its operands


(i) a+b = ab+

(ii) (a+b)*c = ab+c*

(iii) a+(b*c) = abc*+


arithmetic expression validate
Arithmetic Expression validate
  • Pushing an item on to stack correspond to opening a scope, and popping an item from the stack corresponds to closing a scope.
  • When the stack is empty and scope ender encountered, so the parenthesis pattern is invalid.


infix notation
Infix notation
  • Infix notation: A * ( B + C ) / D
  • Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules.


postfix notation
Postfix notation
  • The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order.
  • Operators act on values immediately to the left of them.
  • RPN has the advantage of being extremely easy, and therefore fast, for a computer to analyze.


  • In the 1920's, Jan Lukasiewicz developed a formal logic system which allowed mathematical expressions to be specified without parentheses by placing the operators before (prefix notation) or after (postfix notation) the operands.
  • postfix notation for a calculator keyboard.
  • computer scientists realized that RPN or postfix notation was very efficient for computer math.
  • As a postfix expression is scanned from left to right, operands are simply placed into a last-in, first-out (LIFO) stack and operators may be immediately applied to the operands at the bottom of the stack.
  • Another advantage is consistency between machines.


practical implications
Practical implications
  • Calculations proceed from left to right
  • There are no brackets or parentheses, as they are unnecessary.
  • Operands precede operator. They are removed as the operation is evaluated.
  • When an operation is made, the result becomes an operand itself (for later operators)
  • There is no hidden state. No need to wonder if you hit an operator or not.


precedence of operators
Precedence of Operators
  • The five binary operators are: addition, subtraction, multiplication, division and exponentiation.
  • The order of precedence is (highest to lowest)
  • Exponentiation 
  • Multiplication/division *, /
  • Addition/subtraction +, -
converting infix to postfix
Converting Infix to Postfix
  • In case of ‘(A+B)*C’, the closing parenthesis indicates that ‘+’ must be performed first.
  • Assume the existence of a function ‘prcd(op1,op2)’ where op1 and op2 are two operators.
  • Prcd(op1,op2) returns TRUE if op1 has precedence over op2, FASLE otherwise.
  • Examples:
    • prcd(‘*’,’+’) is TRUE
    • prcd(‘+’,’+’) is TRUE
    • prcd(‘+’,’*’) is FALSE
  • Note: The order of operands in postfix is the same as the infix.
infix to postfix conversion algorithm
Infix to Postfix Conversion Algorithm
  • In english:Scan the Infix string from left to right.
  • Initialize an empty stack.
  • If the scanned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack.
  • If the scanned character is an Operator and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.
  • Repeat this step till all the characters are scanned.
  • After all characters are scanned, we have to add any character that the stack may have to the Postfix string. If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.


  • The calculation: ((1 + 2) * 4) + 3 can be written down like this in RPN:
  • The expression is evaluated in the following way (the Stack is displayed after Operation has taken place):
  • Input Stack Operation

1 1 Push operand

2 1, 2 Push operand

+ 3 Addition

4 3, 4 Push operand

* 12 Multiplication

3 12,3 Push operand

+ 15 Addition

1 2 + 4 * 3 +


infix to postfix conversion algorithm1
Infix to Postfix conversion Algorithm

Opstk = the empty stack

while(not end of input) {

symb=next input char;

if (symbol is operand)

add symb in postfix string

else {

while(!empty(opstk)&& prcd(stacktop(opstk), symb)) {

topsymb=pop(opstk); add topsymb to postfix string;



while(!empty(opstk)) {


add topsymb to postfix string;



evaluating postfix
Evaluating Postfix
  • Each operator in a postfix expression refers to the previous two operands.
  • Each time we read an operand, we push it on a stack.
  • When we reach an operator, we pop the two operands from the top of the stack, apply the operator and push the result back on the stack.
postfix expression evaluation algorithm
Postfix Expression evaluation Algorithm

In english:Scan the Postfix string from left to right.

  • Initialize an empty stack.
  • If the scanned character is an operand, add it to the stack. If the scanned character is an operator, there will be at least two operands in the stack.
  • If the scanned character is an Operator, then we store the top most element of the stack(topStack) in a variable temp. Pop the stack. Now evaluate topStack(Operator)temp. Let the result of this operation be retVal. Pop the stack and Push retVal into the stack.
  • Repeat this step till all the characters are scanned.
  • After all characters are scanned, we will have only one element in the stack. Return topStack as result


algorithm for postfix evalution
Algorithm for postfix evalution

Opndstk = the emty stack;

While (not end of input)


Symb = next input character;

If (symb is an operand)






Value = result of applying symb to opnd1 and opnd2;

Push(opndstk, value);



Return (pop(opndstk));


a stack interface in java
A Stack Interface in Java
  • The stack data structure is included as a "built-in" class in the java.util package of Java.
  • it is instructive to learn how to design and implement a stack "from scratch.“
  • Implementing an abstract data type in Java involves two steps
    • Define interface
    • Define exceptions for any error conditions that can arise.
    • Provide a concrete class that implements the methods of the interface associated with that ADT.


stack interface
Stack interface

public interface Stack <E> {

public int size();

public booleanisEmpty();

public E top() throws EmptyStackException;

public void push( E element);

public E pop() throws EmptyStackException;