1 / 16

Stack Implementations

Stack Implementations. Chapter 13. Chapter Contents. A Linked Implementation An Array-Based Implementation A Vector-Based Implementation. A Linked Implementation. When using a chain of linked nodes to implement a stack with each node reference one entry in stack.

elle
Download Presentation

Stack Implementations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack Implementations Chapter 13

  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 chainpublic LinkedStack() { topNode = null; } // end default constructor . . . private class Node implements java.io.Serializable{ private Object data; // entry in stackprivate Node next; // link to next node < Constructors and the methods getData, setData, getNextNode, and setNextNode are here. >. . .} // end Node

  6. A Linked Implementation: Push or Add to the top • Push a new node to the stack • allocate memory for newnode • 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 entriesprivate int topIndex; // index of top entryprivate static final int DEFAULT_MAX_SIZE = 50;public ArrayStack() { stack = new Object[DEFAULT_MAX_SIZE]; topIndex = -1; } // end default constructorpublic 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 constructorpublic VectorStack(int maxSize) { stack = new Vector(maxSize); } // end constructor . . .

  14. A Vector-Based Implementation publicvoid 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 StackQuestion 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 StackQuestion 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.

More Related