/** Source code example for "A Practical Introductionto DataStructures and Algori
thm Analysis, 3rd Edition (Java)"by Clifford A. ShafferCopyright 2008-2011 by Cl
ifford A. Shaffer*//** Array-based list implementation */class AList<E> implemen ts List<E> {private static final int defaultSize = 10; // Defaultsizeprivate int maxSize; // Maximum size of listprivate int listSize; // Current # of list item sprivate int curr; // Position of currentelementprivate E[] listArray; // Array holding listelements/** Constructors *//** Create a list with the default capaci ty. */AList() { this(defaultSize); }/** Create a new list object.@param size Max # of elements list can contain.*/@SuppressWarnings("unchecked") // Generic arra yallocationAList(int size) {maxSize = size;listSize = curr = 0;listArray = (E[]) new Object[size]; // CreatelistArray}public void clear() // Reinitialize the lis t{ listSize = curr = 0; } // Simply reinitialize values/** Insert "it" at curren t position */public void insert(E it) {assert listSize < maxSize : "List capacit y exceeded";for (int i=listSize; i>curr; i--) // Shift elements uplistArray[i] = listArray[i-1]; // to make roomlistArray[curr] = it;listSize++; // Increment li st size}/** Append "it" to list */public void append(E it) {assert listSize < ma xSize : "List capacity exceeded";listArray[listSize++] = it;}/** Remove and retu rn the current element */public E remove() {if ((curr<0) || (curr>=listSize)) // No currentelementreturn null;E it = listArray[curr]; // Copy the elementfor(int i=curr; i<listSize-1; i++) // Shift themdownlistArray[i] = listArray[i+1];listS ize--; // Decrement sizereturn it;}public void moveToStart() { curr = 0; } // Se t tofrontpublic void moveToEnd() { curr = listSize; } // Setat endpublic void pr ev() { if (curr != 0) curr--; } // Backuppublic void next() { if (curr < listSiz e) curr++; }/** @return List size */public int length() { return listSize; }/** @return Current position */public int currPos() { return curr; }/** Set current list position to "pos" */public void moveToPos(int pos) {assert (pos>=0) && (pos <=listSize) : "Pos out ofrange";curr = pos;}/** @return Current element */public E getValue() {assert (curr>=0) && (curr<listSize) :"No current element";return listArray[curr];}// Extra stuff not printed in the book./*** Generate a human-re adable representation ofthis list's contents* that looks something like this: < 1 2 3 | 4 5 6 >.The vertical* bar represents the current location of the fence.T his method* uses toString() on the individual elements.* @return The string repr esentation of this list*/public String toString(){// Save the current position o f the listint oldPos = currPos();int length = length();StringBuffer out = new St ringBuffer((length() + 1)* 4);moveToStart();out.append("< ");for (int i = 0; i < oldPos; i++) {out.append(getValue());out.append(" ");next();}out.append("| ");f or (int i = oldPos; i < length; i++) {out.append(getValue());out.append(" ");nex t();}out.append(">");moveToPos(oldPos); // Reset the fence to itsoriginal positi onreturn out.toString();}}