Stacks Chapter 6.

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

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.
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.
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)
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
CMPT 225 Stacks.
Chapter 3 Stacks.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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,
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:
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.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
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.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
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.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 5.
Chapter 5 Recursion as a Problem-Solving Technique
COMPSCI 107 Computer Science Fundamentals
Abstract Data Types (ADTs)
Stacks.
CSE 373: Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Visit for more Learning Resources
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.
Stacks.
Topic 15 Implementing and Using Stacks
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:

Stacks Chapter 6

The Abstract Data Type Stack Operations on a stack Last-in, First-out behavior. Applications demonstrated Evaluating algebraic expressions Searching for a path between two points

Developing an ADT During the Design of a Solution Consider typing a line of text on a keyboard Use of backspace key to make corrections You type Corrected input will be Must decide how to store the input line.

Developing an ADT During the Design of a Solution Initial draft of solution. Two required operations Add new item to ADT Remove item added most recently

Developing an ADT During the Design of a Solution Read and correct algorithm. Third operation required See whether ADT is empty

Developing an ADT During the Design of a Solution Write-backward algorithm Fourth operation required Get item that was added to ADT most recently.

Specifications for the ADT Stack See whether stack is empty. Add new item to the stack. Remove from and discard stack item that was added most recently. Get copy of item that was added to stack most recently.

Specifications for the ADT Stack FIGURE 6-1 A stack of cafeteria plates LIFO: The last item inserted onto a stack is the first item out

Specifications for the ADT Stack FIGURE 6-2 UML diagram for the class Stack

Specifications for the ADT Stack LISTING 6-1 A C++ interface for stacks

Specifications for the ADT Stack LISTING 6-1 A C++ interface for stacks

Specifications for the ADT Stack Axioms for multiplication Axioms for ADT stack

Checking for Balanced Braces Example of curly braces in C++ language Balanced Not balanced Requirements for balanced braces For each }, must match an already encountered { At end of string, must have matched each {

Checking for Balanced Braces Initial draft of a solution.

Checking for Balanced Braces Detailed pseudocode solution.

Checking for Balanced Braces Detailed pseudocode solution.

Checking for Balanced Braces FIGURE 6-3 Traces of algorithm that checks for balanced braces

Recognizing Strings in a Language Given a definition of a language, L Special palindromes Special middle character $ Example ABC$CBA ɛ L, but AB$AB ɛ L A stack is useful in determining whether a given string is in a language Traverse first half of string Push each character onto stack Reach $, undo, pop character, match or not

Recognizing Strings in a Language Algorithm to recognize string in language L

Recognizing Strings in a Language Algorithm to recognize string in language L

Using Stacks with Algebraic Expressions Strategy Develop algorithm to evaluate postfix Develop algorithm to transform infix to postfix These give us capability to evaluate infix expressions This strategy easier than directly evaluating infix expression

Evaluating Postfix Expressions Infix expression 2 * (3 + 4) Equivalent postfix 2 3 4 + * Operator in postfix applies to two operands immediately preceding Assumptions for our algorithm Given string is correct postfix No unary, no exponentiation operators Operands are single lowercase letters, integers

Evaluating Postfix Expressions FIGURE 6-4 The effect of a postfix calculator on a stack when evaluating the expression 2 * (3 + 4)

Evaluating Postfix Expressions A pseudocode algorithm that evaluates postfix expressions

Infix to Postfix Important facts Operands always stay in same order with respect to one another. Operator will move only “to the right” with respect to the operands; If in the infix expression the operand x precedes the operator op, Also true that in the postfix expression the operand x precedes the operator op . All parentheses are removed.

Infix to Postfix First draft of algorithm to convert infix to postfix

Infix to Postfix Determining where to place operators in postfix expression Parentheses Operator precedence Left-to-right association Note difficulty Infix expression not always fully parenthesized Precedence and left-to-right association also affect results

Infix to Postfix Figure 6-5 A trace of the algorithm that converts the infix expression a − (b + c * d) /e to postfix form

Infix to Postfix Pseudocode algorithm that converts infix to postfix

Infix to Postfix Pseudocode algorithm that converts infix to postfix

Using Stack to Search a Flight Map FIGURE 6-6 A flight map

Using Stack to Search a Flight Map Recall recursive search strategy.

Using Stack to Search a Flight Map Possible outcomes of exhaustive search strategy Reach destination city, decide possible to fly from origin to destination Reach a city, C from which no departing flights You go around in circles Use backtracking to recover from a wrong choice (2 or 3)

Using Stack to Search a Flight Map Strategy requires information about order in which it visits cities Figure 6-7 The stack of cities as you travel from P to W

Using Stack to Search a Flight Map Stack will contain directed path from Origin city at bottom to … Current visited city at top When to backtrack No flights out of current city Top of stack city already somewhere in the stack (already visited => acycle)

Using Stack to Search a Flight Map FIGURE 6-8 The effect of revisits on the stack of cities

Using Stack to Search a Flight Map Final draft of algorithm.

Using Stack to Search a Flight Map Final draft of algorithm.

Stack to Search a Flight Map FIGURE 6-9 A trace of search, given the flight map

Using Stack to Search a Flight Map C++ implementation of searchS

Using Stack to Search a Flight Map C++ implementation of searchS

Using Stack to Search a Flight Map Figure 6-10 Flight map for Checkpoint Question 8

Relationship Between Stacks and Recursion Key aspects of common strategy Visiting a new city Backtracking Termination

Relationship Between Stacks and Recursion FIGURE 6-11 Visiting city P , then R , then X : (a) box trace versus (b) stack

Relationship Between Stacks and Recursion FIGURE 6-12 Backtracking from city X to R to P : (a) box trace versus (b) stack

End Chapter 6