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

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, Queues, and Linked Lists
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
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.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
Summary of lectures (1 to 11)
Queue, Deque, and Priority Queue Implementations.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 3 Introduction to Collections – Stacks Modified
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
© 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,
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,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Linked Structures, LinkedStack
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 367 Introduction to Data Structures Lecture 5.
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.
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.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Click to edit Master text styles Stacks Data Structure.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
Stacks II David Lillis School of Computer Science and Informatics
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
Stacks.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack Implementations
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Implementations
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stack Implementations Chapter 12

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 with each node reference one entry in stack A chain of linked nodes that implements a stack.

4 A Linked Implementation When a chain has only a head reference, we can add, remove or retrieve its first node faster than any other node. The first node should reference the stack's top

5 A Linked Implementation Data field and constructor Each node an instance of class Node: a private class defined within LinkedStack 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

6 A Linked Implementation: Push or Add to the top Push a new node to the stack (a) allocate memory for newnode (b) topNode references newNode.

7 A Linked Implementation: Pop or Remove The stack after the first node in the chain is deleted. public T pop() { T top = peek(); If (topNode != null) topNode = topNode.getNextNode(); return top; }

8 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 when we push or remove elements from stack

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

10 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... To indicate an empty stack, we set topIndex = -1 as initial value.

11 An Array-Based Implementation: Pop Another array-based stack after top removed by (a) decrementing topIndex ; (b) setting stack[topIndex]=null and then decrementing topIndex to be safe

12 A Vector-Based Implementation When using a vector to implement a stack like using array, but easier 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

13 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...

14 A Vector-Based Implementation public void push(T newEntry) { stack.add(newEntry); } public T pop() { T top = null; If( !isEmpty() ) { top = stack.lastElement(); stack.remove(stack.size()-1); } return top; } No need to maintain an index to the top entry, however, we can infer this index from the vector’s size. Also, the vector expands as necessary, so we do not have to worry about expanding the array when full.

15 Solve Problem using Stack Question 1: A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. Describe how you could use a stack to test whether a string is a palindrome.

16 Solve Problem using Stack Question 2: Suppose that you read a binary string—that is, a string of 0s and 1s—one character at a time. Describe how you could use a stack but no arithmetic to see whether the number of 0s is equal to the number of 1s. When these counts are not equal, state how you could tell which character—0 or 1—occurs most frequently and by how much its count exceeds the other’s.