Copyright ©2012 by Pearson Education, Inc. All rights reserved

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
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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
COMPSCI 105 S Principles of Computer Science 13 Stacks.
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.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Infix, Postfix, Prefix.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
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.
Transforming Infix to Postfix
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Queues, Deques and Priority Queues Chapter 10 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
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.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
A List Implementation That Links Data
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Stacks.
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
A List Implementation That Links Data
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.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Copyright ©2012 by Pearson Education, Inc. All rights reserved Stacks Chapter 5 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced Delimiters in an Infix Algebraic Expression A Problem Solved: Transforming an Infix Expression to a Postfix Expression A Problem Solved: Evaluating Postfix Expressions A Problem Solved: Evaluating Infix Expressions Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents The Program Stack Java Class Library: The Class Stack Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe operations of ADT stack Use stack to decide whether delimiters in an algebraic expression are paired correctly Use stack to convert infix expression to postfix expression Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Use stack to evaluate postfix expression Use stack to evaluate infix expression Use a stack in a program Describe how Java run-time environment uses stack to track execution of methods Copyright ©2012 by Pearson Education, Inc. All rights reserved

Specifications of a Stack Organizes entries according to order added All additions added to one end of stack Added to “top” Called a “push” Access to stack restricted Access only top entry Remove called a “pop” Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 5-1 Some familiar stacks Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved ADT Stack Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved ADT Stack Copyright ©2012 by Pearson Education, Inc. All rights reserved

Specify Class Stack Interface Example usage Note source code, Listing 5-1 Example usage Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 5-2 A stack of strings after (a) push adds Jim; (b) push adds Jess; (c) push adds Jill; (d) push adds Jane; (e) push adds Joe; (f ) pop retrieves and removes Joe; (g) pop retrieves and removes Jane Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Stack to Process Algebraic Expressions Algebraic expressions composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Different precedence notations Infix a + b Prefix + a b Postfix a b + Copyright ©2012 by Pearson Education, Inc. All rights reserved

Using a Stack to Process Algebraic Expressions Precedence must be maintained Order of operators Use of parentheses (must be balanced) Use stacks to evaluate parentheses usage Scan expression Push symbols Pop symbols Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 5-3 The contents of a stack during the scan of an expression that contains the balanced delimiters { [ ( ) ] } Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 5-4 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) } Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 5-5 The contents of a stack during the scan of an expression that contains the unbalanced delimiters [ ( ) ] } Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 5-6 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ) ] Implementation of algorithm to check for balanced parentheses, Listing 5-2 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Infix to Postfix Manual algorithm for converting infix to postfix (a + b) * c Write with parentheses to force correct operator precedence ((a + b) * c) Move operator to right inside parentheses ((a b + ) c * ) Remove parentheses a b + c * Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Infix to Postfix Algorithm basics Scan expression left to right When operand found, place at end of new expression When operator found, save to determine new position Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 5-7 Converting the infix expression a + b * c to postfix form Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 5-8 Converting an infix expression to postfix form: a - b + c Copyright ©2012 by Pearson Education, Inc. All rights reserved

Figure 5-8 Converting an infix expression to postfix form: a ^ b ^ c Copyright ©2012 by Pearson Education, Inc. All rights reserved

Infix to Postfix Conversion Operand Append to end of output expression Operator ^ Push ^ onto stack Operators +, -, *, / Pop from stack, append to output expression Until stack empty or top operator has lower precedence than new operator Then push new operator onto stack Copyright ©2012 by Pearson Education, Inc. All rights reserved

Infix to Postfix Conversion Open parenthesis Push ( onto stack Close parenthesis Pop operators from stack and append to output Until open parenthesis is popped. Discard both parentheses Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 5-9 The steps in converting the infix expression a / b * (c + (d - e)) to postfix form Copyright ©2012 by Pearson Education, Inc. All rights reserved

Evaluating Postfix Expressions FIGURE 5-10 The stack during the evaluation of the postfix expression a b / when a is 2 and b is 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 5-11 The stack during the evaluation of the postfix expression a b + c / when a is 2, b is 4, and c is 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Evaluating Infix Expressions FIGURE 5-12 Two stacks during the evaluation of a + b * c when a is 2, b is 3, and c is 4: (a) after reaching the end of the expression; (b) while performing the multiplication; Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 5-12 Two stacks during the evaluation of a + b * c when a is 2, b is 3, and c is 4: (c) while performing the addition Copyright ©2012 by Pearson Education, Inc. All rights reserved

The Program Stack FIGURE 5-13 The program stack at three points in time: (a) when main begins execution; (PC is the program counter)

Copyright ©2012 by Pearson Education, Inc. All rights reserved The Program Stack FIGURE 5-13 The program stack at three points in time: (b) when methodA begins execution; (PC is the program counter) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved The Program Stack FIGURE 5-13 The program stack at three points in time: (c) when methodB begins execution; (PC is the program counter) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class Stack Has a single constructor Creates an empty stack Remaining methods – differences from our StackInterface are highlighted public T push(T item); public T pop(); public T peek(); public boolean empty(); Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 5 Copyright ©2012 by Pearson Education, Inc. All rights reserved