© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)

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 Chapter 11.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
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.
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.
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)
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,
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. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
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.
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.
Topic 3 The Stack ADT.
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 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
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.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
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.
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 5.
Stacks Chapter 6.
CC 215 Data Structures Stack ADT
Abstract Data Types (ADTs)
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Data Structures
Chapter 6 Stacks.
CSC 205 – Java Programming II
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.
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.
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)

© 2006 Pearson Addison-Wesley. All rights reserved7A-2 Developing an ADT During the Design of a Solution 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 Figure 7-1 Stack of cafeteria dishes

© 2006 Pearson Addison-Wesley. All rights reserved7A-3 Developing an ADT During the Design of a Solution ADT stack operations –Create an empty stack –Determine whether a stack is empty –Add a new item to the stack --- push(item) –Remove from the stack the item that was added most recently -- item = pop() –Remove all the items from the stack –Retrieve from the stack the item that was added most recently -- item = peek()

© 2006 Pearson Addison-Wesley. All rights reserved7A-4 Refining the Definition of the ADT Stack Pseudocode for the ADT stack operations createStack() // Creates an empty stack. isEmpty() // Determines whether a stack is empty. push(newItem) throws StackException // Adds newItem to the top of the stack. // Throws StackException if the insertion is // not successful.

© 2006 Pearson Addison-Wesley. All rights reserved7A-5 Refining the Definition of the ADT Stack Pseudocode for the ADT stack operations (Continued) pop() throws StackException // Retrieves and then removes the top of the stack. // Throws StackException if the deletion is not // successful. popAll() // Removes all items from the stack. peek() throws StackException // Retrieves the top of the stack. Throws // StackException if the retrieval is not successful

© 2006 Pearson Addison-Wesley. All rights reserved7A-6 Using the ADT Stack in a Solution displayBackward and readAndCorrect algorithms can be refined by using stack operations A program can use a stack independently of the stack’s implementation

© 2006 Pearson Addison-Wesley. All rights reserved7A-7 Simple Applications of the ADT Stack: 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

© 2006 Pearson Addison-Wesley. All rights reserved7A-8 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 “{”

© 2006 Pearson Addison-Wesley. All rights reserved7A-9 Checking for Balanced Braces Figure 7-3 Traces of the algorithm that checks for balanced braces

© 2006 Pearson Addison-Wesley. All rights reserved7A-10 Implementations of the ADT Stack Figure 7-4 Implementation of the ADT stack that use a) an array; b) a linked list; c) an ADT list java.util.Stack

© 2006 Pearson Addison-Wesley. All rights reserved7A-11 An Array-Based Implementation of the ADT Stack StackArrayBased class –Implements StackInterface –Instances Stacks –Private data fields An array of Objects called items The index top Figure 7-5 An array-based implementation

© 2006 Pearson Addison-Wesley. All rights reserved7A-12 To the javadocs! A Reference-Based Implementation of the ADT Stack A reference-based implementation –Required when the stack needs to grow and shrink dynamically java.util.Stack inherits from Vector a List. – How do we implement: pop, peek, push ?

© 2006 Pearson Addison-Wesley. All rights reserved7A-13 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 of a list represents the top of the stack –push(newItem) operation is implemented as add(1, newItem) –pop() operation is implemented as get(1) remove(1) –peek() operation is implemented as get(1)

© 2006 Pearson Addison-Wesley. All rights reserved7A-14 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 expressions –Convert the infix expression to postfix form –Evaluate the postfix expression

© 2006 Pearson Addison-Wesley. All rights reserved7A-15 Evaluating Postfix Expressions A postfix calculator –Requires you to enter postfix expressions Example: 2, 3, 4, +, * –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

© 2006 Pearson Addison-Wesley. All rights reserved7A-16 Evaluating Postfix Expressions Figure 7-8 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

© 2006 Pearson Addison-Wesley. All rights reserved7A-17 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

© 2006 Pearson Addison-Wesley. All rights reserved7A-18 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 Whenever you hit an close parenthesis add the operators onto the Stack until you get to the open parenthesis

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

© 2006 Pearson Addison-Wesley. All rights reserved7A-20 Application: A Search Problem High Planes Airline Company (HPAir) –Problem For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city

© 2006 Pearson Addison-Wesley. All rights reserved7A-21 Representing the Flight Data The flight map for HPAir is a graph –Adjacent vertices Two vertices that are joined by an edge –Directed path A sequence of directed edges Figure 7-10 Flight map for HPAir

© 2006 Pearson Addison-Wesley. All rights reserved7A-22 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 The ADT stack is useful in organizing an exhaustive search Backtracking can be used to recover from a wrong choice of a city

© 2006 Pearson Addison-Wesley. All rights reserved7A-23 A Nonrecursive Solution that Uses a Stack Figure 7-11 The stack of cities as you travel a) from P; b) to R; c) to X; d) back to R; e) back to P; f) to W

© 2006 Pearson Addison-Wesley. All rights reserved7A-24 A Nonrecursive Solution that Uses a Stack Figure 7-13 A trace of the search algorithm, given the flight map in Figure 6-9

© 2006 Pearson Addison-Wesley. All rights reserved7A-25 And now for something completely different - Java Generics Java uses a concept called Generics throughout the java Collections Framework (JCF). The Collections framework is the overall parent of all the data types we’ve been talking about: Collection<-List<-LinkedList, Collection<-List<-Vector<-Stack Generics –Develop classes and interfaces and defer certain data-type information Until you are actually ready to use the class or interface Definition of the class or interface is followed by –E represents the data type that client code will specify Stack myS = new Stack ();

© 2006 Pearson Addison-Wesley. All rights reserved7A-26 Java Generics A Stack without Generics: Stack st = new Stack(); st.push(“Java”); st.push(“is”); // public void push(Object obj); To get things out: Object o = st.pop(); // public Object pop(); String temp = (String)o; // casting to a String ** DANGEROUS -- COMPILER CANNOT PROTECT YOU! (You can protect yourself, by adding more code.. But why do that? Generics to the rescue!)**

© 2006 Pearson Addison-Wesley. All rights reserved7A-27 Java Generics A Stack without Generics: Stack st = new Stack(); st.push(“Java”); // public void push(Object o); st.push(“is”); st.push(new Integer(34)); To get things out: Object o = st.pop(); String temp = (String)o; // casting to a String ** Program crashes with RuntimeException when you get to the Integer on the Stack though!!! **

© 2006 Pearson Addison-Wesley. All rights reserved7A-28 Java Generics come to save the day! A Stack with Generics: Stack st = new Stack (); st.push(“Java”); st.push(“is”); st.push(new Integer(34)); // COMPILER ERROR! To get things out: String temp = st.pop(); // We know it’s a String.. No casting!

© 2006 Pearson Addison-Wesley. All rights reserved7A-29 The Java VM Stack From: Javaworld ( hood.html) Pushing and popping the stack frame –Your program invokes a method –the Java virtual machine creates a new stack frame for the method. Stack Frame Contains: –space for the method's local variables, –its operand stack, –any other information required by a particular virtual machine implementation. –When the JVM invokes a method, it creates a stack frame of the proper size for that method. –Adding a new frame onto the Java stack when a method is invoked is called "pushing" a stack frame; –Removing a frame when a method returns is called "popping" a stack frame.

© 2006 Pearson Addison-Wesley. All rights reserved7A-30 The Java VM Stack Method A Local vars Parameters Other Method B Local vars Parameters Other Method C Local vars Parameters Other Top Which method was called first? Second? Third? Method D Local vars “va” Other Method D Local vars “ava” Other Method D Local vars “Java” Other What is this an example of? Actual parameter shown instead of word “Parameters”

© 2006 Pearson Addison-Wesley. All rights reserved7A-31 Summary Can you write a Stack based method that takes a String and writes it backwards?

© 2006 Pearson Addison-Wesley. All rights reserved7A-32 Summary ADT stack operations have a last-in, first-out (LIFO) behavior Algorithms that operate on algebraic expressions are an important application of stacks A stack can be used to determine whether a sequence of flights exists between two cities A strong relationship exists between recursion and stacks