Download presentation
Presentation is loading. Please wait.
Published byWilliam Myles Newton Modified over 9 years ago
1
Chapter 6 Stacks CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides
2
Contents The Abstract Data Type Stack Simple Uses of a Stack Using Stacks with Algebraic Expressions Using a Stack to Search a Flight Map The Relationship Between Stacks and Recursion Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 http://mediaplayer.pearsoncmg.com/_ph_cc_ecs960544_set.title.6a_The_ADT_Stack __/aw/streaming/ecs_carrano_dapscpp_6/Ch06a_The_ADT_Stack.m4v
3
The Abstract Data Type Stack Developing an ADT during the design of a solution Consider entering keyboard text – Mistakes require use of backspace abcdd efgg We seek a programming solution to read these keystrokes Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
4
The Abstract Data Type Stack Pseudocode of first attempt Requires – Add new item to ADT – Remove most recently added item Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
5
Specifications for the ADT Stack We have identified the following operations: – See whether stack is empty. – Add a new item to stack. – Remove from the stack item added most recently. – Get item that was added to stack most recently. Stack uses LIFO principle – Last In First Out Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
6
Specifications for the ADT Stack A stack of cafeteria plates Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
7
Abstract Data Type: Stack A finite number of objects – Not necessarily distinct – Having the same data type – Ordered by when they were added Operations – isEmpty() – push(newEntry) – pop() – peek() Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
8
Abstract Data Type: Stack View C++ Stack interface, Listing 6-1Listing 6-1 UML diagram for the class Stack Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
9
Axioms for the ADT Stack new Stack()).isEmpty() = true new Stack()).pop() = false new Stack()).peek() = error aStack.push(item)).isEmpty() = false aStack.push(item)).peek() = item aStack.push(item)).pop() = true Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
10
Simple Uses of a Stack Traces of the algorithm that checks for balanced braces Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
11
Simple Uses of a Stack Recognizing strings in a language Consider L = { s$s' : s is a possibly empty string of characters other than $, s' = reverse( s )} View algorithm to verify a string for a given language, Listing 6-AListing 6-A Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 http://mediaplayer.pearsoncmg.com/_ph_cc_ecs960544_set.title.6a_The_ADT_ Stack__/aw/streaming/ecs_carrano_dapscpp_6/Ch06a_The_ADT_Stack.m4v
12
Using Stacks with Algebraic Expressions Evaluating postfix expressions The effect of a postfix calculator on a stack when evaluating the expression 2 * (3 + 4) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
13
Using Stacks with Algebraic Expressions Converting infix expressions to equivalent postfix expressions Possible pseudocode solution Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
14
Using Stacks with Algebraic Expressions A trace of the algorithm that converts the infix expression a – ( b + c * d ) / e to postfix form Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 View pseudocode algorithm that converts infix to postfix Listing 6-B
15
Chapter 7 Implementations of the ADT Stack
16
Contents An Array-Based Implementation A Link-Based implementation Implementations That Use Exceptions Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
17
An Array Based Implementation Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 Using an array to store a stack’s entries: (a) a preliminary sketch; (b) implementation details
18
An Array Based Implementation Consider a header file for an array based implementation, Listing 7-1Listing 7-1 View the accompanying implementation file, Listing 7-2 Listing 7-2 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
19
A Link-Based implementation A link-based implementation of a stack Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013 http://mediaplayer.pearsoncmg.com/_ph_cc_ecs960544_set.title.7b_ Overview_of_LinkedStack__/aw/streaming/ecs_carrano_dapscpp_6/ Ch07b_Overview_LinkedStack.m4v
20
A Link-Based implementation View header file for an link-based implementation, Listing 7-3Listing 7-3 Note the accompanying implementation file, Listing 7-4 Listing 7-4 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
21
Implementations That Use Exceptions Note header file for the class PrecondViolatedExcep, Listing 7-5Listing 7-5 View the accompanying implementation file, Listing 7-6 Listing 7-6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.