public class sep23 { public static void main(String[] args) { intList A, B, C; A = new intList(); A.front = new cell(1,new cell(3,new cell(5,new cell(7,null)))); // A.print(); A.insertAt(6,2); // insert 6 at position 2 A.insertAt(3,3); // insert 3 at position 3 A.print(); A.delete(3); A.print(); A.delete(1); A.print(); } // end main } // end class sep23 class cell // "cons" cell { int head; // datum cell tail; // pointer to rest of the list public cell(int h, cell t) { head = h; tail = t; } } // end class cell class intList { cell front; // points to a list public void print() { cell i; // pointer to go through the list for(i=front;i != null;i=i.tail) System.out.print(i.head + " "); System.out.println(""); } public void insert_front(int newint) { front = new cell(newint,front); } public void insertAt(int newint, int pos) { // assume 0 is front of the list cell current = front; int counter = 0; if (pos == 0) front = new cell(newint,front); else { while ( (current != null) && (counter < (pos -1)) ) { counter = counter + 1; current = current.tail; } if (current != null) { cell newcell = new cell(newint,current.tail); current.tail = newcell; } } // end else } // end insertAt // delete first cell containing x at head public void delete(int x) { cell current = front; if (front != null) { if (front.head == x) front = front.tail; else { while ( (current.tail != null) && (current.tail.head != x) ) { current = current.tail; } if (current.tail != null) current.tail = current.tail.tail; } // end else } // end outer if } } // end wrapper class intList