package rebelsky.linear; /** * An implementation of stacks using arrays. Currently incomplete, * as it fails when the array fills. * * @author Samuel A. Rebelsky * @version 1.0 of October 2004. */ public class ArrayBasedStack implements Stack { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The index of the "last" thing. */ int lastThing; /** The stuff actually in the stack. */ Object[] contents; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public ArrayBasedStack() { this.lastThing = -1; this.contents = new Object[3]; } // ArrayBasedStack() // +---------+------------------------------------------------- // | Methods | // +---------+ public void put(Object addMe) { this.contents[this.lastThing+1] = addMe; this.lastThing += 1; } // put(Object) public Object get() { Object returnMe = this.contents[this.lastThing]; this.contents[this.lastThing] = null; // Actually remove it. this.lastThing -= 1; return returnMe; } // get() public Object peek() { return this.contents[this.lastThing]; } // peek() public boolean isEmpty() { return this.lastThing == -1; } // isEmpty() } // ArrayBasedStack()