package BoxTools; /** * A List object maintains a linked list of elements of type T. * Each element is contained in a Link object, and these Link * objects are linked together. A List object has a pointer to these linked * Link objects, which are linked in one way. * A List object must be declared local, but the elements in it can be global. * List is implemented like a stack. The first element added to it appears at * the end of the list. *

Usage: A List object must be declared local. * @see Link * @version 1.1 * @author Tong Wen, LBNL * @since 1.0 */ template public class List{ /** the starting Link object, which contains the last element. */ protected template Link local firstLink; /** the current Link object in the list. */ protected template Link local currentLink; /** the number of elements contained in the list. */ protected int size=0; public inline List(){ firstLink=null; currentLink=firstLink; } public final inline local void clear(){ firstLink=null; currentLink=firstLink; size=0; } public final inline local void addElement(T newElement){ firstLink=new template Link(firstLink,newElement); currentLink=firstLink; size++; } public final inline local void advance(){ if (currentLink!=null) currentLink=currentLink.nextLink; } public final inline local T access(){return currentLink.element;} /** If curruentlink==null, using this method will cause a runtime exception. */ public final inline local void setElement(T element){currentLink.element=element;} public final inline local boolean endp(){return (currentLink==null);} public final inline local void reset(){currentLink=firstLink;} /** Returns the element at position index. * If index is out of range, the last element is returned. */ public final local T elementAt(int index){ if (index>=size) { Util.printErrMsg("List::elementAt( ): the index is out of range! The last element will be returned."); index=size; } int steps=size-index-1; reset(); for (int i=0;iindex. * If index is out of range, the last element is set. */ public final local void setElementAt(T newElement, int index){ if (index>=size) { Util.printErrMsg("List::elementAt( ): the index is out of range!"); return; } else{ int steps=size-index-1; reset(); for (int i=0;iList object. */ public final local template List local copy(){ template List local newList=new template List(); reset(); if (size>0){ newList.firstLink=new template Link(null,access()); newList.currentLink=newList.firstLink; template Link local tempLink; newList.size++; for (int i=1;i(); newList.currentLink.setNextLink(tempLink); advance(); tempLink.setElement(access()); newList.advance(); newList.size++; } } return newList; } public final inline local int size(){return this.size;} public final inline local boolean isEmpty(){return (size==0);} public final local T [] local toArray(){ T [] local array=new T [size]; for (int i=0;i