Stack Implementations - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Stack Implementations

Description:

Stack Implementations Chapter 21 Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation A Linked Implementation When ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 13
Provided by: SteveA285
Category:

less

Transcript and Presenter's Notes

Title: Stack Implementations


1
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. 21-1 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 lt Constructors and the methods getData,
setData, getNextNode, and setNextNode are
here. gt . . . // end Node
5
A Linked Implementation
Fig. 21-2 (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. 21-3 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. 21-4 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
ObjectDEFAULT_MAX_SIZE topIndex -1 //
end default constructor public ArrayStack(int
maxSize) stack new ObjectmaxSize topInde
x -1 // end constructor . . .
10
An Array-Based Implementation
Fig. 21-5 Another array-based stack after top
removed by (a) decrementing topIndex (b) setting
stacktopIndexnull 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.Vectorpublic 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 . . .
Write a Comment
User Comments (0)
About PowerShow.com