Stack Implementations Chapter 21. 2 Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

List Implementations That Use Arrays
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
Dictionary Implementations Chapter Chapter Contents Array-Based Implementations The Entries An Unsorted Array-Based Dictionary A Sorted Array-Based.
Dictionary Implementations Chapter 18 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Queue, Deque, and Priority Queue Implementations Chapter 14.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
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.
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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 Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Summary of lectures (1 to 11)
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Chapter 3 Array-Based Implementations CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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,
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 Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
Hashing as a Dictionary Implementation Chapter 19.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Introduction to Data Structures and Algorithms
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Queue, Deque, and Priority Queue Implementations Chapter 23.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Stacks II David Lillis School of Computer Science and Informatics
Stacks.
Queue, Deque, and Priority Queue Implementations
Stack Implementations
Stack Implementations
Stacks public interface Stack { public boolean empty();
Stack Implementations
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stack Implementations Chapter 21

2 Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation

3 A Linked Implementation When using a chain of linked nodes to implement a stack The first node should reference the stack's top Fig A chain of linked nodes that implements a stack.

4 A Linked Implementation Data field and constructor Each node an instance of class Node public class LinkedStack implements StackInterface, java.io.Serializable {private Node topNode; // references first node in chain public LinkedStack() {topNode = null; } // end default constructor... private class Node implements java.io.Serializable {private Object data; // entry in stack private Node next; // link to next node... } // end Node

5 A Linked Implementation Fig (a) A new node that references the top of the stack; (b) the new node is now at the top of the stack.

6 A Linked Implementation Fig The stack after the first node in the chain is deleted.

7 An Array-Based Implementation When using an array to implement a stack The array's first element should represent the bottom of the stack The last occupied location in the array represents the stack's top This avoids shifting of elements of the array if it were done the other way around

8 An Array-Based Implementation Fig An array that implements a stack; its first location references (a) the top of the stack; (b) the bottom of the stack

9 An Array-Based Implementation Data fields and constructors public class ArrayStack implements StackInterface, java.io.Serializable {private Object [] stack; // array of stack entries private int topIndex; // index of top entry private static final int DEFAULT_MAX_SIZE = 50; public ArrayStack() {stack = new Object[DEFAULT_MAX_SIZE]; topIndex = -1; } // end default constructor public ArrayStack(int maxSize) {stack = new Object[maxSize]; topIndex = -1; } // end constructor...

10 An Array-Based Implementation Fig Another array-based stack after top removed by (a) decrementing topIndex ; (b) setting stack[topIndex]=null and then decrementing topIndex

11 A Vector-Based Implementation When using a vector to implement a stack Vector's first element should represent the bottom of the stack Last occupied location in the vector represents the stack's top Based on an array that can be expanded dynamically Performance similar to array-based implementation

12 A Vector-Based Implementation Data fields and constructors import java.util.Vector; public class VectorStack implements StackInterface, java.io.Serializable {public VectorStack() {stack = new Vector(); // vector doubles in size if necessary } // end default constructor public VectorStack(int maxSize) {stack = new Vector(maxSize); } // end constructor...