package rebelsky.linear; import java.lang.reflect.Array; /** * An array-based implementation of queues. * * @author Samuel A. Rebelsky * @author Margaret Louisa Poythress * @au * @version 1.0 of October 2004. */ public class LABS implements Stack { // +--------+-------------------------------------------------- // | Fields | // +--------+ /** The index of the "last" thing. */ int lastThing; /** The stuff actually in the stack. */ Object[] contents; // +--------------+-------------------------------------------- // | Constructors | // +--------------+ public LABS() { this.lastThing = -1; this.contents = new Object[2]; } // LABS() // +---------+------------------------------------------------- // | Methods | // +---------+ public void put(Object addMe) { if (lastThing+1 >= contents.length){ // Make a new array that's twice as big Object[] arrayNew = new Object[contents.length*2]; // Copy everything into the new array for (int i = 0; i < contents.length; i++) { // Set the ith value of arrayNew to the ith value of contents. arrayNew[i] = contents[i]; // java.lang.reflect.Array.set(arrayNew, i, java.lang.reflect.Array.get(contents, i)); // Array.set(arrayNew, i, Array.get(contents, i)); } // Make that new array our countents this.contents = arrayNew; // Add the value } 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() } // LABS()