//Reads natural numbers, prepending them into a linked list. //Stops reading after a 0 is read. At that point, the list is printed. //The net effect is that a list of numbers gets printed in the //reverse order from which they were read. class LinkedList extends Object { var nat data; //content of a list node var LinkedList next; //(implicit pointer to) the remainder of the list //set the data value in the list's head node to newData //returns newData nat setData(nat newData) { data=newData; } //prepend a new node to the list //the new node will contain the newData //always returns 0 nat prepend(nat newData) { var LinkedList currList; currList = new LinkedList; currList.data = data; currList.next = next; data = newData; next = currList; 0; } //print the list //always returns 0 nat printList(nat unused) { //print the data in this list node printNat(data); //if there's more to the list, recursively print the remainder if(next==null) then {0;} else {next.printList(0);}; } } main { var nat input; //create a linked list var LinkedList ll; ll = new LinkedList; //read data into the list input = readNat(); ll.setData(input); while(!(input==0)) { input = readNat(); ll.prepend(input); }; //print the list ll.printList(0); }