// Insert at head of list.... very simple... import java.io.*; class ListElement { int value; ListElement next; } class Linklist1 { // Insert(head, newelem): // input: head points to the first element of a list // newelem points to a new element // output: insert newelem at the beginning of the list // and return the pointer to the next starting element // static ListElement Insert(ListElement head, ListElement newelem) { newelem.next = head; return(newelem); } public static void Print(ListElement head) { while (head != null) { System.out.print(head.value + " "); head = head.next; } System.out.println(); } public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); ListElement head, ptr; head = null; // Start with an empty list.... while(true) { ptr = new ListElement(); // Make a new list element System.out.print("Enter number: "); ptr.value = Integer.parseInt(stdin.readLine()); head = Insert(head, ptr); // Insert into list System.out.print("List: "); Print(head); System.out.println(); } } }