Monday, October 8, 2007

The Linked List Maker method

The methods missing from yesterday's post... public class IntList { // Names of simple Containers public int head; public IntList tail; // Constructor function /** List Cell containing (HEAD, TAIL). */ public IntList (int head, IntList tail) { this.head = head; //Scheme car this.tail = tail; //Scheme cdr } /* Converts an Array of Integers into a Linked List with * the same elements. * @param: an Array of integers with > 1 elements */ static IntList makeList (int a []) { IntList X, Z; X = new IntList (a[0], null); int i; for (i = 1, Z = X; i < z =" Z.tail){" tail =" new" i =" 0;" x =" new" z =" X;" z =" makelistrhelper"> a.length -1 ){ return Z; } else { Z.tail = new IntList (a[i], null); return makelistrhelper (Z.tail, a, ++i); } } static int countList (IntList Z) { int countL = 0; countL = countListH (Z, countL); return countL; } static int countListH (IntList Z, int countL){ if (Z == null) return countL; else { return countListH (Z.tail, ++countL); } } static void printList (IntList Z) { int i = 0; IntList M; for (M = Z; M != null; M = M.tail ) { System.out.println("Head" + "-" + i +" ---- " + M.head); i++; } } }

No comments:

Just some daily notes ...

Powered By Blogger