Stacks Chapter 5 Adapted from Pearson Education, Inc.

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.
Stacks Chapter 11.
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
Stacks Example: Stack of plates in cafeteria.
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.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
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.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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
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
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.
© 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.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 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.
Stacks  Introduction  Applications  Implementations  Complex Applications.
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.
Review Use of Stack Introduction Stack in our life Stack Operations
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Introduction to Data Structures
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Stacks.
(Part 2) Infix, Prefix & Postfix
Stacks Chapter 5.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
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.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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:

Stacks Chapter 5 Adapted from Pearson Education, Inc.

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 The Program Stack Java Class Library: The Class Stack Adapted from Pearson Education, Inc.

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 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 Adapted from Pearson Education, Inc.

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” Adapted from Pearson Education, Inc.

ADT Stack Adapted from Pearson Education, Inc.

ADT Stack Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. A Stack Interface public interface StackInterface < T > { /** Adds a new entry to the top of this stack. @param newEntry an object to be added to the stack */ public void push (T newEntry); /** Removes and returns this stacks top entry. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop (); /** Retrieves this stacks top entry. @return either the object at the top of the stack or null if the stack is empty */ public T peek (); /** Detects whether this stack is empty. @return true if the stack is empty */ public boolean isEmpty (); /** Removes all entries from this stack */ public void clear (); } // end StackInterface Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Stack Usage Example Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. Stack Usage Example Adapted from Pearson Education, Inc.

Algebraic Expressions Algebraic expressions are composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Different precedence notations Infix a + b Prefix + a b Postfix a b + 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 Adapted from Pearson Education, Inc.

Algebraic Expressions Algebraic expressions are composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Precedence must be maintained According to order of operators According to parentheses When parentheses are used, they must be balanced Different notations Infix a + b Prefix + a b Postfix a b + Adapted from Pearson Education, Inc.

Parentheses Balance Checker Scan expression from left to right When you encounter: ( { [  Push opening parenthesis symbols onto stack ) } ]  Pop symbol from stack and check if it matches Figure 5-3 The contents of a stack during the scan of an expression that contains the balanced delimiters { [ ( ) ] } Adapted from Pearson Education, Inc.

Parentheses Balance Checker Figure 5-4 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) } Adapted from Pearson Education, Inc.

Parentheses Balance Checker Figure 5-5 The contents of a stack during the scan of an expression that contains the unbalanced delimiters [ ( ) ] } Can not pop to match } Adapted from Pearson Education, Inc.

Parentheses Balance Checker Figure 5-6 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ) ] Reached end of string Adapted from Pearson Education, Inc.

Infix vs. Postfix Expressions In-fix expressions: Operator comes between two operands  a + b Precedence of operators determined as follows: + and -  have lowest precedence, are evaluated left-to-right * and /  have higher precedence, are evaluated left-to-right ^  has highest precedence, is evaluated right-to-left Parentheses  override precedence Post-fix expressions: Operator comes after two operands  a b + Precedence is determined by order of operators in the expression No parentheses are used in postfix expressions Adapted from Pearson Education, Inc.

Infix to Postfix Conversion - Manual Steps: Put parentheses according to correct operator precedence Move operator to right inside parentheses Remove parentheses Example: (a + b) * c ( ( a + b ) * c ) ( ( a b + ) c * ) a b + c * Notice the following: Postfix expressions do NOT need parentheses to specify precedence The order of operands in the postfix expression remains the same as in the infix expression Example: a + b * c ( a + ( b * c ) ) ( a ( b c * ) + ) a b c * + Adapted from Pearson Education, Inc.

Infix to Postfix Conversion - Algorithm Algorithm basics Scan infix expression left to right When operand found, place it at end of new postfix expression When operator found, save it until new position is determined Why? The 2nd operand must be output before the operator Maybe there will be an operator with higher precedence, then it needs to be output first Adapted from Pearson Education, Inc.

Infix to Postfix Conversion - Algorithm 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 Push new operator onto stack Open parenthesis Push ( onto stack Close parenthesis Pop operators from stack and append to output Until open parenthesis is popped. Discard both parentheses Keep popping all operators that have higher or equal precedence Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (1) Figure 5-7 Converting a + b * c to postfix form Next Char Postfix Operator Stack (bottom to top) a (empty) + b a b * + * c a b c (end of infix) a b c * a b c * + Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (1) Figure 5-7 Converting a + b * c to postfix form Next Char Postfix Operator Stack (bottom to top) a (empty) + b a b * + * c a b c (end of infix) a b c * a b c * + Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) The only thing that can pop an open parenthesis is a matching closing parenthesis Next Char Postfix Operator Stack (bottom to top) 7 (empty) + 2 72 ^ +^ ( +^( 1 721 +^(+ 3 7213 Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) Next Char Postfix Operator Stack (bottom to top) 3 7213 +^(+ ) 7213+ +^ / 7213+^ +/ ( +/( 6 7213+^6 + +/(+ 5 7213+^65 * +/(+* Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) Are we done? We are only done when we reach the end of the infix expression AND the stack is empty. Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

Infix to Postfix Conversion – Example (2) Converting the following to postfix form 7 + 2 ^ ( 1 + 3 ) / ( 6 + 5 * 3 – 5 ) Can you evaluate this? 7 + 2 ^ 4 / (6 + 5 * 3 – 5) = 7 + 16 / (6 + 15 – 5) = 7 + 16 / (21 – 5) = 7 + 16 / 16 = 7 + 1 = 8 Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

Postfix Evaluation - Algorithm Any operator operates on its two previous operands Example - manual 7 2 1 3 + ^ 6 5 3 * + 5 - / + 7 2 4 ^ 6 5 3 * + 5 - / + 7 16 6 15 + 5 - / + 7 16 21 5 - / + 7 16 16 / + 7 1 + 8 Algorithm basics Scan postfix expression left to right When operand found, push it onto operand stack When operator found: Pop stack for 2nd operand Pop stack for 1st operand Evaluate Push result onto operand stack Adapted from Pearson Education, Inc.

Evaluating Postfix Expression – Example (1) 7 2 1 3 + ^ 6 5 3 * + 5 - / + 1 + 3 2 ^ 4 7 2 7 1 2 7 3 1 2 7 427 167 6 167 56167 5 * 3 6 + 15 21 - 5 16 / 16 7 + 1 356167 156167 21167 5 21 167 16167 17 8 Adapted from Pearson Education, Inc.

Evaluating Infix Expressions evaluation of a + b * c when a is 2, b is 3, and c is 4 after reaching the end of the expression while performing the multiplication; while performing the addition Adapted from Pearson Education, Inc.

The Program Stack when main begins execution when methodA begins execution when methodB begins execution Adapted from Pearson Education, Inc.

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(); Adapted from Pearson Education, Inc.

Adapted from Pearson Education, Inc. End Chapter 5 Adapted from Pearson Education, Inc.