CS Data Structures Chapter 6 Stacks Mehmet H Gunes

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)
CMPT 225 Stacks-part2.
CMPT 225 Stacks.
Chapter 3 Stacks.
© 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,
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)
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.
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 removed.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Topic 15 Implementing and Using Stacks
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
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)
Topic 3 The Stack ADT.
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.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
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 © Curt Hill Stacks An Useful Abstract Data Type.
Tail Recursion l The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. l Tail.
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.
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.
Programming with Recursion
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
Revised based on textbook author’s notes.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Chapter 13 Queues and Priority Queues
Programming with Recursion
Introduction to Data Structures
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Chapter 18-3 Recursion Dale/Weems.
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.
Stacks.
Stacks.
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
Chapter 18 Recursion.
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.
Presentation transcript:

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

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Developing an ADT During the Design of a Solution Read and correct algorithm Third operation required See whether ADT is empty © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Developing an ADT During the Design of a Solution Write-backward algorithm Fourth operation required Get item that was added to ADT most recently. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Specifications for the ADT Stack A stack of cafeteria plates LIFO: The last item inserted onto a stack is the first item out © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Specifications for the ADT Stack UML diagram for the class Stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Specifications for the ADT Stack A C++ interface for stacks © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Specifications for the ADT Stack A C++ interface for stacks © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Specifications for the ADT Stack Axioms for multiplication Axioms for ADT stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 { © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Checking for Balanced Braces Initial draft of a solution. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Checking for Balanced Braces Detailed pseudocode solution. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Checking for Balanced Braces Detailed pseudocode solution. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Checking for Balanced Braces Traces of algorithm that checks for balanced braces push { pop stack empty -> balanced © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Recognizing Strings in a Language Algorithm to recognize string in language L © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Recognizing Strings in a Language Algorithm to recognize string in language L © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Evaluating Postfix Expressions The effect of a postfix calculator on a stack when evaluating the expression 2 * (3 + 4) © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Evaluating Postfix Expressions A pseudocode algorithm that evaluates postfix expressions © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Infix to Postfix First draft of algorithm to convert infix to postfix © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Infix to Postfix A trace of the algorithm that converts the infix expression a − (b + c * d) /e to postfix form © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Infix to Postfix Pseudocode algorithm that converts infix to postfix © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Infix to Postfix Pseudocode algorithm that converts infix to postfix © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map Recall recursive search strategy. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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) © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map Strategy requires information about order in which it visits cities The stack of cities as you travel from P to W © 2017 Pearson Education, Hoboken, NJ. All rights reserved

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 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map The effect of revisits on the stack of cities © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map Final draft of algorithm. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map Final draft of algorithm. © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map A trace of the search algorithm, given the flight map © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map C++ implementation of searchS © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map C++ implementation of searchS © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Using Stack to Search a Flight Map Flight map for Checkpoint Question 8 © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK 43

Relationship Between Stacks and Recursion Key aspects of common strategy Visiting a new city Backtracking Termination © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Relationship Between Stacks and Recursion Visiting city P , then R , then X : (a) box trace versus (b) stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Relationship Between Stacks and Recursion Backtracking from city X to R to P : (a) box trace versus (b) stack © 2017 Pearson Education, Hoboken, NJ. All rights reserved

Stack Activation Frames The activation record stores the return address for this function call, the parameters, local variables, and the function’s return value, if non-void. The activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code. At this time the function’s return value, if non-void, is brought back to the calling block return address for use there.

What operation does Func(a, b) simulate? // Another recursive function int Func ( int a, int b ) // Pre: a and b have been assigned values // Post: Function value = ?? { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a , b - 1 ) ); // instruction 50 else // second general case result = Func ( - a , - b ); // instruction 70 } return result; What operation does Func(a, b) simulate?

x = Func(5, 2); // original call is instruction 100 Run-Time Stack Activation Records x = Func(5, 2); // original call is instruction 100 FCTVAL ? result ? b 2 a 5 Return Address 100 original call at instruction 100 pushes on this record for Func(5,2)

Run-Time Stack Activation Records x = Func(5, 2); // original call at instruction 100 FCTVAL ? result ? b 1 a 5 Return Address 50 result 5+Func(5,1) = ? b 2 a 5 Return Address 100 call in Func(5,2) code at instruction 50 pushes on this record for Func(5,1) record for Func(5,2)

Run-Time Stack Activation Records call in Func(5,1) code at instruction 50 pushes on this record for Func(5,0) FCTVAL ? result ? b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 Return Address 50 result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,1) record for Func(5,2)

Run-Time Stack Activation Records record for Func(5,2) record for Func(5,1) record for Func(5,0) is popped first with its FCTVAL FCTVAL 0 result 0 b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100

Run-Time Stack Activation Records FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,1) is popped next with its FCTVAL record for Func(5,2)

x = Func(5, 2); // original call at line 100 Run-Time Stack Activation Records x = Func(5, 2); // original call at line 100 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5 Return Address 100 record for Func(5,2) is popped last with its FCTVAL