Stack Implementations

Slides:



Advertisements
Similar presentations
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Advertisements

CS Data Structures II Review COSC 2006 April 14, 2017
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Data Structures & Algorithms
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Chapter 7 Stacks II CS Data Structures I COSC 2006
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Iterators (short version) Chapter 15 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Queues and Priority Queue Implementations Chapter 14 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.
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.
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks Chapter 5.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 12: Data Structures
Stacks.
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Pointers and Linked Lists
Implementations of the ADT Stack
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Data Structures and Database Applications Stacks in C#
Stack Implementations
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
List Implementations Chapter 9.
CSE 214 – Computer Science II Stacks
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Chapter 7 Implementations of the ADT Stack
Recall: stacks and queues
Chapter 5 Stack (part 1).
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Bag Implementations that Use Arrays
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Abstract Data Types Stacks CSCI 240
A List Implementation That Links Data
List Implementations that Use Arrays
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

Stack Implementations Chapter 6 Data Structures and Abstractions with Java, 4e, Global Edition Frank Carrano © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation Each operation involves top of stack push pop peek Head of linked list easiest, fastest to access Let this be the top of the stack © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation FIGURE 6-1 A chain of linked nodes that implements a stack © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation LISTING 6-1 An outline of a linked implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation LISTING 6-1 An outline of a linked implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation FIGURE 6-2 (a) A new node that references the node at the top of the stack; © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation (b) FIGURE 6-2 (b) the new node is now at the top of the stack © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation Definition of push © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation FIGURE 6-3 The stack (a) before the first node in the chain is deleted © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation (b) FIGURE 6-3 The stack (b) after the first node in the chain is deleted © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation Definition of pop © 2016 Pearson Education, Ltd.  All rights reserved.

Linked Implementation Definition of rest of class. © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation Each operation involves top of stack push pop peek End of the array easiest to access Let this be top of stack Let first entry be bottom of stack © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation FIGURE 6-4 An array that implements a stack; its first location references (a) the top entry in the stack; © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation FIGURE 6-4 An array that implements a stack; its first location references (b) the bottom entry in the stack © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation LISTING 6-2 An outline of an array-based implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation LISTING 6-2 An outline of an array-based implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation Adding to the top. © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation Retrieving the top, operation is O(1) © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation FIGURE 6-5 An array-based stack after its top entry is removed by (a) decrementing topIndex; © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation FIGURE 6-5 An array-based stack after its top entry is removed by (b) setting stack[topIndex] to null and then decrementing topIndex © 2016 Pearson Education, Ltd.  All rights reserved.

Array-Based Implementation Removing the top © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation Vector: an object that behaves like a high-level array Index begins with 0 Methods to access or set entries Size will grow as needed Use vector’s methods to manipulate stack © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation FIGURE 6-6 A client using the methods given in StackInterface; these methods interact with a vector’s methods to perform stack operations © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. The Class Vector Constructors Has methods to add, remove, clear Also methods to determine Last element Is the vector empty Number of entries © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation LISTING 6-3 An outline of a vector-based implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation LISTING 6-3 An outline of a vector-based implementation of the ADT stack © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation Adding to the top © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation Retrieving the top © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation Removing the top © 2016 Pearson Education, Ltd.  All rights reserved.

Vector Based Implementation The rest of the class. © 2016 Pearson Education, Ltd.  All rights reserved.

Stack Implementations Chapter 6 © 2016 Pearson Education, Ltd.  All rights reserved.