Chapter 6 Stacks CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Slides:



Advertisements
Similar presentations
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Advertisements

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.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Queues and Priority Queues
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CMPT 225 Stacks.
Chapter 3 Stacks.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 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,
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 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Queues and Priority Queues
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)
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Stacks and Queues Introduction to Computing Science and Programming I.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
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,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Tree Implementations Chapter 16 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 6: Stacks Data Abstraction & Problem Solving with C++
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.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Dictionaries and Their Implementations Chapter 18 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Interlude 1 C++ Classes CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Click to edit Master text styles Stacks Data Structure.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Chapter 4 Link Based Implementations
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
Stacks.
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Chapter 16 Tree Implementations
Chapter 13 Queues and Priority Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS Data Structures Chapter 17 Heaps Mehmet H Gunes
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Sorted Lists and Their Implementations
Chapter 7 Implementations of the ADT Stack
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Queues and Priority Queue Implementations
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 CS Data Structures Mehmet H Gunes Modified from authors’ slides

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, © __/aw/streaming/ecs_carrano_dapscpp_6/Ch06a_The_ADT_Stack.m4v

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

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

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

Specifications for the ADT Stack A stack of cafeteria plates Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

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

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

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

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

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, © Stack__/aw/streaming/ecs_carrano_dapscpp_6/Ch06a_The_ADT_Stack.m4v

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

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

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

Chapter 7 Implementations of the ADT Stack

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

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

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

A Link-Based implementation A link-based implementation of a stack Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © Overview_of_LinkedStack__/aw/streaming/ecs_carrano_dapscpp_6/ Ch07b_Overview_LinkedStack.m4v

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

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