CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
Infix, Postfix, Prefix.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Topic 15 Implementing and Using Stacks
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Stacks  Introduction  Applications  Implementations  Complex Applications.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Infix to Postfix Conversion
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CSC 205 Programming II Postfix Expressions

Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations Push – add an item to top Pop – remove the top item Peek – get the content of the top item Implementations and trade-offs

Array-based Implementation Instance variables needed Collection of data – Object[] items Two auxiliary variables Number of items on stack – int numItems Stack capacity – int CAPACITY Selected methods pop popAll

Arithmetic Expressions Three possible orders Infix: operator in between operands 2 * (3 + 4) Widely used in normal presentation Parentheses needed to remove ambiguity Prefix: operator proceeds operands * Postfix: operator following operands *

Evaluating Expressions Infix 2 * (3 + 4)  2 * (3 + 4)  2 * 7  14 Prefix *  *  * 7 2  14 Postfix *  *  2 7 *  14

Evaluating Postfix Expressions Using Stack Assumptions Correct postfix expression With arithmetic operators +, -, *, and / only The algorithm Initialize a stack Read the next input while there is one If (the next input is a number) Push it onto the stack Else Pop two numbers off the stack Carry out the calculation & push the result onto the stack

Converting Infix to Postfix Notation It’s easier to evaluate postfix expressions No need for a separate stack for operators: an operator is consumed when encountered Infix expressions can be converted into equivalent postfix ones then be evaluated Assumptions Correct infix expression +, -, *, and / only

Sample Expression 1 –fully parenthesized (a – ((b + (c * d)) / e))

Converting Infix to Postfix Notation – when fully parenthesized Initialize a stack (for operators and parentheses) Read the next input while there is one If (the next input is a left parenthesis) Push it onto the stack Else if (the next input is a number) Write it to the output Else if (the next input is an operator) Push it onto the stack Else Pop and output the top item (an operator) Pop and discard the top item (a left parenthesis)

Sample Expression 2 – parenthesized as needed a – (b + c * d) / e

Converting Infix to Postfix Notation – using precedence rules Initialize a stack (for operators and parentheses) Read the next input while there is one If (the next input is a left parenthesis) Push it onto the stack Else if (the next input is a number) Write it to the output Else if (the next input is an operator) Pop and print operators off the stack until Stack become empty The next item is a left parenthesis The next item is an operator of lower precedence Push the new operator onto stack Else Pop and output the top item (an operator) Pop and discard the top item (a left parenthesis)