Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

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.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
CMPT 225 Stacks-part2.
Chapter 3 Stacks.
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.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Infix, Postfix, Prefix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
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)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
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 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
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.
CHP-3 STACKS.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
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.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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
Queues Chapter 8 (continued)
Stacks Chapter 6.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Introduction to Data Structures
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Topic 15 Implementing and Using Stacks
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapters 6 and 7 Stacks.
Data Structures and Algorithms 2/2561
Chapter 7 © 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:

Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved

A Reference-Based Implementation of the ADT Stack Required when the stack needs to grow and shrink dynamically StackReferenceBased Implements StackInterface top is a reference to the head of a list of items © 2011 Pearson Addison-Wesley. All rights reserved

An Implementation That Uses the ADT List The ADT list can be used to represent the items in a stack If the item in position 1 of a list represents the top of the stack push(newItem) operation is implemented as add(1, newItem) pop() operation is implemented as remove(1) peek() operation is implemented as get(1) © 2011 Pearson Addison-Wesley. All rights reserved

An Implementation That Uses the ADT List Figure 7-7 An implementation that uses the ADT list © 2011 Pearson Addison-Wesley. All rights reserved

Comparing Implementations All of the implementations are ultimately array based or reference based Fixed size versus dynamic size An array-based implementation Uses fixed-sized arrays Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached A reference-based implementation Does not put a limit on the size of the stack © 2011 Pearson Addison-Wesley. All rights reserved

Comparing Implementations The worst case scenario for the array-based implementation is when the array stack size limit is reached. A common practice is to create a new array with size (2*MAX) and copy all the elements of the first array in the new array. The operation is expensive in terms of space and time (order of O(n)). © 2011 Pearson Addison-Wesley. All rights reserved

The Java Collections Framework Class Stack JCF contains an implementation of a stack class called Stack (generic) Derived from Vector Includes methods: peek, pop, push, and search search returns the 1-based position of an object on the stack © 2011 Pearson Addison-Wesley. All rights reserved

Application: Algebraic Expressions When the ADT stack is used to solve a problem, the use of the ADT’s operations should not depend on its implementation To evaluate an infix expressions Convert the infix expression to postfix form Evaluate the postfix expression © 2011 Pearson Addison-Wesley. All rights reserved

Evaluating Postfix Expressions A postfix calculator Requires you to enter postfix expressions Example: 2, 3, 4, +, * When an operand is entered, the calculator Pushes it onto a stack When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack © 2011 Pearson Addison-Wesley. All rights reserved

Postfix To Infix Algorithm Create an empty stack called odstack for keeping operators and a list for output. Scan the expression from left to right. If the token is an operand, push it on the odstack. If the token is an operator (op) pop from the stack (operand2) pop from the stack (operand1) compute the result using operand1 (op) operand2 Push the result into the odstack 3. When the input expression has been completely processed, check the odstack for the resulting infix expression Example: 234+* © 2006 Pearson Addison-Wesley. All rights reserved

Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4) © 2011 Pearson Addison-Wesley. All rights reserved

Evaluating Postfix Expressions To evaluate a postfix expression which is entered as a string of characters Simplifying assumptions The string is a syntactically correct postfix expression No unary operators are present No exponentiation operators are present Operands are single lowercase letters that represent integer values © 2011 Pearson Addison-Wesley. All rights reserved

Converting Infix Expressions to Equivalent Postfix Expressions An infix expression can be evaluated by first being converted into an equivalent postfix expression Facts about converting from infix to postfix Operands always stay in the same order with respect to one another An operator will move only “to the right” with respect to the operands All parentheses are removed © 2011 Pearson Addison-Wesley. All rights reserved

Infix to Postfix Algorithm Create an empty stack called opstack for keeping operators and a list for output. Scan the expression from left to right. If the token is an operand, append it to the end of the output list. If the token is a left parenthesis, push it on the opstack. If the token is a right parenthesis, pop the opstack until the corresponding left parenthesis is removed. If the token is an operator remove any operators already on the opstack that have higher or equal precedence and append them to the output list. Push the operator on the opstack. However, first 3. Then the input expression has been completely processed, check the  opstack. Any operators still on the stack can be removed and appended to the end of the output list Example: a - (b + c * d)/e © 2006 Pearson Addison-Wesley. All rights reserved

Converting Infix Expressions to Equivalent Postfix Expressions Figure 7-9 A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form © 2011 Pearson Addison-Wesley. All rights reserved

^ *, /, % +, - Precedence Precedence © 2006 Pearson Addison-Wesley. All rights reserved

Examples © 2006 Pearson Addison-Wesley. All rights reserved

The Relationship Between Stacks and Recursion The ADT stack has a hidden presence in the concept of recursion Typically, stacks are used by compilers to implement recursive methods During execution, each recursive call generates an activation record that is pushed onto a stack Stacks can be used to implement a nonrecursive version of a recursive algorithm © 2011 Pearson Addison-Wesley. All rights reserved

Summary ADT stack operations have a last-in, first-out (LIFO) behavior Algorithms that operate on algebraic expressions are an important application of stacks Stacks can be used to implement a nonrecursive version of a recursive algorithm © 2011 Pearson Addison-Wesley. All rights reserved