Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.

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

Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
COMPSCI 105 S Principles of Computer Science 13 Stacks.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
Stacks A stack is a data structure that only allows items to be inserted and removed at one end We call this end the top of the stack The other end is.
Chapter 3 Stacks.
Data Structures & Algorithms
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 reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
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 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
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.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
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)
Chapter 7 Stacks II CS Data Structures I COSC 2006
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
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.
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,
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
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 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
Queues Chapter 8 (continued)
Data Abstraction & Problem Solving with C++
Stacks Chapter 5.
Stacks Chapter 6.
CC 215 Data Structures Stack ADT
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Data Structures
Chapter 6 Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapters 6 and 7 Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Queues Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Queues.
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
Presentation transcript:

Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1

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  Remove from the stack the item that was added most recently  Remove all the items from the stack  Retrieve from the stack the item that was added most recently © 2006 Pearson Addison-Wesley. All rights reserved 7A-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 © 2006 Pearson Addison-Wesley. All rights reserved 7A-3 Figure 7-1 Stack of cafeteria dishes

Developing an ADT During the Design of a Solution  A queue  First in, first out (FIFO) property  The first item added is the first item to be removed © 2006 Pearson Addison-Wesley. All rights reserved 7A-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 reserved 7A-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 reserved 7A-6

Axioms (Optional)  Axioms are used to define an ADT formally  Example  Axiom to specify that the last item inserted into stack is the first item to be removed (stack.push(newItem)).pop() = stack © 2006 Pearson Addison-Wesley. All rights reserved 7A-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 reserved 7A-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 reserved 7A-9

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

Checking for Balanced Braces  The exception StackException  A Java method that implements the balanced-braces algorithm should do one of the following  Take precautions to avoid an exception  Provide try and catch blocks to handle a possible exception © 2006 Pearson Addison-Wesley. All rights reserved 7A-11

Implementations of the ADT Stack  The ADT stack can be implemented using  An array  A linked list  The ADT list  StackInterface  Provides a common specification for the three implementations  StackException  Used by StackInterface  Extends java.lang.RuntimeException © 2006 Pearson Addison-Wesley. All rights reserved 7A-12

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

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 © 2006 Pearson Addison-Wesley. All rights reserved 7A-14 Figure 7-5 An array-based implementation

A Reference-Based Implementation of the ADT Stack  A reference-based implementation  Required when the stack needs to grow and shrink dynamically  StackReferenceBased  Implements StackInterface  top is a reference to the head of a linked list of items © 2006 Pearson Addison-Wesley. All rights reserved 7A-15

A Reference-Based Implementation of the ADT Stack © 2006 Pearson Addison-Wesley. All rights reserved 7A-16 Figure 7-6 A reference-based implementation