/* Class IntRevolver is a cyclic data structure * where each cell holds an integer number. * The individual elements of this 'revolver' * contain the integer number and a pointer to * the next cell. * * For simplicity we access the cells of the * list elements directly (data being the integer * number, next being the pointer to the next * list element) instead of using methods. * * Carsten Butz, November 2002 */ class IntRevolver{ private IntListElement current = null; int getCurrent(){ if(current == null) return 0; else return current.data; } void insert(int d){ IntListElement nile = new IntListElement(d); if(current == null){ /* revolver must be empty */ current = nile; current.next = current; } else{ /* revolver contains at least one element */ nile.next = current.next; current.next = nile; } return; } IntListElement remove(){ /* returnElement contains the pointer that will be returned */ IntListElement returnElement = null; if(current != null){ /* revolver contains at least one element */ returnElement = current.next; if(current.next == current){ /* revolver contains exactly one element */ current = null; } else{ /* revolver contains more than one element */ current.next = returnElement.next; } } return returnElement; } void next(){ current = current.next; return; } int count(){ int howMany = 0; if(current != null){ /* There is at least one element. * Use start as a marker, use current * to run through all cells until you * are back to start. */ IntListElement start = current; do{ current = current.next; howMany++; }while(start != current); } return howMany; } IntListElement find(int key){ IntListElement r = null; boolean found = false; IntListElement start = current; do{ if(key == current.data){ r = current; found = true; } current = current.next; }while(start != current && !found); return r; } public String toString(){ String s = ""; if(current!=null){ IntListElement start = current; do{ s = s + current.data + "\n"; /* Use the following line if you want to * see the contents of your revolver printed * on one line. */ //s = s + current.data +"\t"; current = current.next; }while(start != current); } return s; } }