package username.lists; import java.util.Vector; /** * Simple Lists implemented with Vectors (as the name implies). * * @author Samuel A. Rebelsky * @author CSC152 2006S */ public class VectorBasedSimpleList implements SimpleList { // +------------------+------------------------------------- // | Design Decisions | // +------------------+ /* (1) We store the values in the list in a vector. (2) Positions are therefore integer indices into the Vector. */ // +--------+----------------------------------------------- // | Fields | // +--------+ /** * The vector in which all the values are stored. */ Vector contents; // +--------------+----------------------------------------- // | Constructors | // +--------------+ public VectorBasedSimpleList() { this.contents = new Vector(); } // VectorBasedSimpleList // +----------------+--------------------------------------- // | Public Methods | // +----------------+ /** * Get the position of the front of this list. * * @return pos * A position that represents the front of the list. * @pre * @post */ public Position front() { return new VectorPosition(0); } // front() /** * Get the position of the end of the list. * * @return pos * A position that represents the end of the list. * @pre * @post */ public Position rear() { return new VectorPosition(this.contents.size()-1); } // rear() /** * Get the position that follows pos. * * @return succ * The position of the successor of the value at pos. * @pre * @post */ public Position successor(Position pos) { return null; // STUB } // successor(Position) /** * Get the position that precedes pos. * * @return pred * The position of the predecessor of the value at pos. * @pre * @post */ public Position predecessor(Position pos) { return null; // STUB } // predecessor(Position) /** * Get the value at a particular position. * * @return val * The value at that position in the list. * @pre * @post */ public V get(Position pos) { return this.contents.get(((VectorPosition) pos).pos); } // get(Position) /** * Determine if a position represents the first position in * the list. * * @return * True, if pos is the first position. False, otherwise. * @pre * @post */ public boolean isFirst(Position pos) { return true; // STUB } // isFirst(Position) /** * Determine if a position represents the last position in * the list. * * @return * True, if pos is the last position. False, otherwise. * @pre * @post */ public boolean isLast(Position pos) { return true; // STUB } // isLast() /** * Determine if the list is empty. * * @return * true, if the list is empty; false, otherwise. */ public boolean isEmpty() { return this.contents.size() == 0; } // isEmpty() /** * Add a value to the list. * * @pre * @post */ public void add(V value) { this.contents.add(value); } // add(V) /** * Delete the value at a particular position. * * @pre * @post */ public void delete(Position pos) { // Note: Position should be a VectorPosition, which has one // field, an int called pos this.contents.remove(((VectorPosition) pos).pos); } // delete(Position) public void removeAll() { // STUB } // removeAll() // +-----------------+-------------------------------------- // | Private Methods | // +-----------------+ } // interface SimpleList