Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.

Slides:



Advertisements
Similar presentations
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Advertisements

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CMPT 225 Stacks-part2.
CMPT 225 Stacks.
Chapter 3 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Lecture 6: Stacks 1. 2 The Abstract Data Type Specifications of an abstract data type for a particular problem –Can emerge during the design of the problem’s.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
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.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Abstract Data Types (ADTs)
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Data Structures
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Abstract Data Types (ADTs)
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Chapters 6 and 7 Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

Chapter 6 Stacks

© 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular problem –Can emerge during the design of the problem’s solution –Examples readAndCorrect algorithm displayBackward algorithm

© 2005 Pearson Addison-Wesley. All rights reserved6-3 Developing an ADT During the Design of a Solution ADT stack operations –Create an empty stack –Destroy a stack –Determine whether a stack is empty –Add a new item –Remove the item that was added most recently –Retrieve the item that was added most recently

© 2005 Pearson Addison-Wesley. All rights reserved6-4 Developing an ADT During the Design of a Solution Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO) property The last item placed on the stack will be the first item removed –Analogy A stack of dishes in a cafeteria

© 2005 Pearson Addison-Wesley. All rights reserved6-5 Refining the Definition of the ADT Stack Definitions for the ADT stack operations + createStack() +destroyStack() +isEmpty():boolean {query} +push(in newItem:StackItemType) throw StackException

© 2005 Pearson Addison-Wesley. All rights reserved6-6 Refining the Definition of the ADT Stack Definitions for the ADT stack operations +pop() throw StackException +pop(out stackTop:StackItemType) throw StackException +getTop(out stackTop:StackItemType) {query} throw StackException

© 2005 Pearson Addison-Wesley. All rights reserved6-7 Using the ADT Stack in a Solution A program can use a stack independently of the stack’s implementation –displayBackward and readAndCorrect algorithms can be refined using stack operations Define an ADT stack formally with axioms –Specify that the last item inserted is the first item to be removed (aStack.push(newItem)).pop()= aStack

© 2005 Pearson Addison-Wesley. All rights reserved6-8 Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces –An example of balanced braces abc{defg{ijk}{l{mn}}op}qr –An example of unbalanced braces abc{def}}{ghij{kl}m

© 2005 Pearson Addison-Wesley. All rights reserved6-9 Checking for Balanced Braces Requirements for balanced braces –Each time you encounter a “}”, it matches an already encountered “{” –When you reach the end of the string, you have matched each “{”

© 2005 Pearson Addison-Wesley. All rights reserved6-10 Checking for Balanced Braces Figure 6.3 Traces of the algorithm that checks for balanced braces

© 2005 Pearson Addison-Wesley. All rights reserved6-11 Recognizing Strings in a Language L = {w$w’ : w is a possible empty string of characters other than $, w’ = reverse(w) } A solution using a stack –Traverse the first half of the string, pushing each character onto a stack –Once you reach the $, for each character in the second half of the string, match a popped character off the stack

© 2005 Pearson Addison-Wesley. All rights reserved6-12 Implementations of the ADT Stack The ADT stack can be implemented using –An array –A linked list –The ADT list All three implementations use a StackException class to handle possible exceptions

© 2005 Pearson Addison-Wesley. All rights reserved6-13 Implementations of the ADT Stack Figure 6.4 Implementation of the ADT stack that use a) an array; b) a linked list; c) an ADT list

© 2005 Pearson Addison-Wesley. All rights reserved6-14 An Array-Based Implementation of the ADT Stack Private data fields –An array of items of type StackItemType –The index top Compiler-generated destructor and copy constructor Figure 6.5 An array-based implementation

© 2005 Pearson Addison-Wesley. All rights reserved6-15 A Pointer-Based Implementation of the ADT Stack A pointer-based implementation –Required when the stack needs to grow and shrink dynamically top is a reference to the head of a linked list of items A copy constructor and destructor must be supplied Figure 6.6 A pointer-based implementation

© 2005 Pearson Addison-Wesley. All rights reserved6-16 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 is the top –push(newItem) insert(1, newItem) –pop() remove(1) –getTop(stackTop) retrieve(1, stackTop)

© 2005 Pearson Addison-Wesley. All rights reserved6-17 An Implementation That Uses the ADT List Figure 6.7 An implementation that uses the ADT list

© 2005 Pearson Addison-Wesley. All rights reserved6-18 Comparing Implementations Fixed size versus dynamic size –An array-based implementation Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached –A pointer-based implementation Does not put a limit on the size of the stack

© 2005 Pearson Addison-Wesley. All rights reserved6-19 Comparing Implementations An implementation that uses a linked list versus one that uses a pointer-based implementation of the ADT list –Linked list approach is more efficient –ADT list approach reuses an already implemented class Much simpler to write Saves time

© 2005 Pearson Addison-Wesley. All rights reserved6-20 The STL Class stack Provides a size function Has two data type parameters –T, the data type for the stack items –Container, the container class that the STL uses in its implementation of the STL stack Uses the keyword explicit in the constructor to prevent invoking the constructor with the assignment operator

© 2005 Pearson Addison-Wesley. All rights reserved6-21 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 expression –Convert the infix expression to postfix form –Evaluate the postfix expression

© 2005 Pearson Addison-Wesley. All rights reserved6-22 Evaluating Postfix Expressions A postfix calculator –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

© 2005 Pearson Addison-Wesley. All rights reserved6-23 Evaluating Postfix Expressions Figure 6.8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

© 2005 Pearson Addison-Wesley. All rights reserved6-24 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

© 2005 Pearson Addison-Wesley. All rights reserved6-25 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

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

© 2005 Pearson Addison-Wesley. All rights reserved6-27 Application: A Search Problem High Planes Airline Company (HPAir) –For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city The flight map for HPAir is a graph –Adjacent vertices are two vertices that are joined by an edge –A directed path is a sequence of directed edges

© 2005 Pearson Addison-Wesley. All rights reserved6-28 Application: A Search Problem Figure 6.10 Flight map for HPAir

© 2005 Pearson Addison-Wesley. All rights reserved6-29 A Nonrecursive Solution That Uses a Stack The solution performs an exhaustive search –Beginning at the origin city, the solution will try every possible sequence of flights until either It finds a sequence that gets to the destination city It determines that no such sequence exists Backtracking can be used to recover from a wrong choice of a city

© 2005 Pearson Addison-Wesley. All rights reserved6-30 A Nonrecursive Solution That Uses a Stack Figure 6.13 A trace of the search algorithm, given the flight map in Figure 6-10

© 2005 Pearson Addison-Wesley. All rights reserved6-31 A Recursive Solution Possible outcomes of the recursive search strategy –You eventually reach the destination city and can conclude that it is possible to fly from the origin to the destination –You reach a city C from which there are no departing flights –You go around in circles

© 2005 Pearson Addison-Wesley. All rights reserved6-32 A Recursive Solution A refined recursive search strategy + searchR(in originCity:City, in destinationCity:City):boolean Mark originCity as visited if (originCity is destinationCity) Terminate -- the destination is reached else for (each unvisited city C adjacent to originCity) searchR(C, destinationCity)

© 2005 Pearson Addison-Wesley. All rights reserved6-33 The Relationship Between Stacks and 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

© 2005 Pearson Addison-Wesley. All rights reserved6-34 Summary ADT stack operations have a last-in, first- out (LIFO) behavior Stack applications –Algorithms that operate on algebraic expressions –Flight maps A strong relationship exists between recursion and stacks