public class Node { private Object item; private Node next; public Node(Object item) { this.item = item; next = null; } public Node(Object item, Node next) { this.item = item; this.next = next; } /** * @return the item */ public Object getItem() { return item; } /** * @param item * the item to set */ public void setItem(Object item) { this.item = item; } /** * @return the next */ public Node getNext() { return next; } /** * @param next * the next to set */ public void setNext(Node next) { this.next = next; } public static void printList(Node list) { if (list != null) { System.out.println(list.getItem()); printList(list.getNext()); } } public static void printListBackwards(Node list) { if (list != null) { printListBackwards(list.getNext()); System.out.println(list.getItem()); } } public static void main(String[] args) { // Answer to question on slide 14 of lecture 14 Node a, b, c, d; a = new Node(4); b = new Node(5); c = new Node(2); d = new Node(7); Node head = a; a.setNext(b); b.setNext(c); c.setNext(d); for (Node current = head; current != null; current = current.getNext()) System.out.println(current.getItem()); // Another answer to the same question head = new Node(4, new Node(5, new Node(2, new Node(7)))); for (Node current = head; current != null; current = current.getNext()) System.out.println(current.getItem()); // Or we can print the same thing out recursively printList(head); printListBackwards(head); } }