// This file illustrates how to create a LinkedList data structure in // Java. Notice that the code is very similar to C++, except that 1) all // object variables are reference types rather than pointers and 2) accesses // to member variables are via the . operator rather than the -> operator. package LinkedList; // The following import statement is not required if you compile List.java // in the LinkedList directory or LinkedList's parent directory. However, // it is required if you compile List.java in any other directory. Therefore // it is a good idea to include it. import LinkedList.ListNode; public class List { ListNode head; ListNode cursor; public List() { head = new ListNode(null); head.next = head; cursor = head; } // insert v after the cursor public void insert(Object v) { ListNode newNode = new ListNode(v); newNode.next = cursor.next; cursor.next = newNode; cursor = newNode; } public boolean hasMoreElements() { return (cursor != head); } public void first() { cursor = head.next; } public Object nextElement() { Object nextValue = cursor.value; cursor = cursor.next; return nextValue; } }