package GCB; /* Define the head of a linked list the head contains a pointer to the first list entry the last list entry the count of entries in the list getCount() returns the count of entries on the list getFirst() returns first entry on the list getLast() returns last entry on the list setHead(entry) places entry on the head of the list getHead() returns (and removes) the head of the list setTail(entry) places entry on the tail of the list getTail() returns (and removes) the tail of the list remove(entry) removes the entry from the list */ public class ListHead { // interal attributes static final String CODE_FILE = "ListHead"; static final int CODE_REVISION = 2; private ListEntry first, // start of the linked list last; // end of the linked list private int count; // number of entries on linked list // internal methods void display() { ListEntry e = (ListEntry) getFirst(); while(e!=null) { System.out.println(e.toString()); e = (ListEntry) e.getNext(); } } // external methods // constructors public ListHead() { first = last = null; count = 0; } public int getCount() {return count;} // get number of entries on list public ListEntry getFirst() {return first;} // get first entry public ListEntry getLast() {return last;} // get last entry public void setHead(ListEntry e) { // place at head of list if(count==0) // list is empty first = last = e; else { // put head after new entry first.setPrior(e); e.setNext(first); first = e; } count++; e.setHead(this); } public ListEntry getHead() { // get the head of the list if(count==0) return null; // list is empty ListEntry e = first; // pop first entry first = e.getNext(); count--; if(count==0) last = null; else first.setPrior(null); e.clean(); return e; } public void setTail(ListEntry e) { // place at tail of list if(count==0) // list is empty first = last = e; else { // put tail before new entry last.setNext(e); e.setPrior(last); last = e; } count++; e.setHead(this); } public ListEntry getTail() { // get the tail of the list if(count==0) return null; // list is empty ListEntry e = last; // cut last entry last = e.getPrior(); count--; if(count==0) first = null; else last.setNext(null); e.clean(); return e; } public void remove(ListEntry e) { // remove the entry from a list if(e.getPrior()==null) { // head of the list if(first!=e) { System.out.println(CODE_FILE+ " attempt to remove entry (head?) from wrong list!"); System.out.println(CODE_FILE+" entry="+e.toString()); System.out.println(CODE_FILE+" entry="+e.getClass()); System.out.println(CODE_FILE+" head="+this.toString()); System.out.println(CODE_FILE+" head="+this.getClass()); Thread.dumpStack(); return; } getHead(); // pop this off the list return; } if(e.getNext()==null) { // end of the list if(last!=e) { System.out.println(CODE_FILE+ " attempt to remove entry (tail?) from wrong list!"); System.out.println(CODE_FILE+" entry="+e.toString()); System.out.println(CODE_FILE+" entry="+e.getClass()); System.out.println(CODE_FILE+" head="+this.toString()); System.out.println(CODE_FILE+" head="+this.getClass()); Thread.dumpStack(); return; } getTail(); // pull this off the list return; } ListEntry l = getFirst(); // find on the list while(l!=null) { if(l==e) { (e.getPrior()).setNext(e.getNext()); // correct forward linkage (e.getNext()).setPrior(e.getPrior()); // correct backward linkage e.clean(); count--; return; } l = l.getNext(); } System.out.println(CODE_FILE+ " attempt to remove entry (middle?) from wrong list!"); System.out.println(CODE_FILE+" entry="+e.toString()); System.out.println(CODE_FILE+" entry="+e.getClass()); System.out.println(CODE_FILE+" head="+this.toString()); System.out.println(CODE_FILE+" head="+this.getClass()); Thread.dumpStack(); } }