CSE143 Sample Program handout #5 First version of ListNode.java ------------------------------ public class ListNode { public int data; public ListNode next; } First version of sample code ---------------------------- public class ListTest1 { public static void main(String[] args) { ListNode list = new ListNode(); list.data = 3; list.next = new ListNode(); list.next.data = 7; list.next.next = new ListNode(); list.next.next.data = 12; list.next.next.next = null; System.out.println(list.data + " " + list.next.data + " " + list.next.next.data); } } Second version of ListNode -------------------------- // ListNode is a class for storing a single node of a linked list storing // integer values. It has two public data fields for the data and the link to // the next node in the list and has three constructors. public class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } } Second version of sample code ----------------------------- public class ListTest2 { public static void main(String[] args) { ListNode list = new ListNode(3, new ListNode(7, new ListNode(12))); System.out.println(list.data + " " + list.next.data + " " + list.next.next.data); } }
Stuart Reges
Last modified: Fri Jan 15 08:58:04 PST 2016