public class ListInsert { public static List makeList() { List h = null; List p; p = new List(); p.value = 55; p.next = h; h = p; p = new List(); p.value = 44; p.next = h; h = p; p = new List(); p.value = 33; p.next = h; h = p; p = new List(); p.value = 22; p.next = h; h = p; p = new List(); p.value = 11; p.next = h; h = p; return h; } public static void printList( List h ) { List ptr = h; while ( ptr != null ) { System.out.println( ptr.value ); ptr = ptr.next; } } public static void main(String[] args) { List head = makeList(); // initial list and make head point to first // element in the list System.out.println("Original list:"); printList(head); System.out.println("\n"); /* ======================================================== Create a new list element ======================================================== */ List ptr = new List(); // ptr points to a new list element ptr.value = 9999; // initialize value field /* ======================================================== Insert list element "ptr" at start of list ======================================================== */ ptr.next = head; // Make ptr.next points to first element of list head = ptr; // Make head points to the new list element System.out.println("After inserting list element at start:"); printList(head); System.out.println("\n"); } }