Java Reference
In-Depth Information
public static LinkedList createSortedList(Scanner in) {
LinkedList LL = new LinkedList();
System.out.printf("Enter some integers ending with 0\n");
int n = in.nextInt();
while (n != 0) {
LL.addInPlace(new NodeData(n));
n = in.nextInt();
}
return LL;
} //end createSortedList
} //end MergeLists
The following is a sample run of Program P3.6:
Enter some integers ending with 0
8 4 12 6 10 2 0
Enter some integers ending with 0
5 7 15 1 3 0
When we merge
2 4 6 8 10 12
with
1 3 5 7 15
we get
1 2 3 4 5 6 7 8 10 12 15
3.16 Circular and Two-Way Linked Lists
So far, our discussion has been primarily about one-way (singly linked) lists. Each node contains one pointer that tells
us the location of the next item. The last node has a null pointer, indicating the end of the list. While this is the most
commonly used type of list, two common variations are the circular list and the two-way (or doubly linked) list.
3.16.1 Circular Lists
In a circular list, we let the last item point back to the first, as follows:
top
Now, there is no null pointer to tell us when we have reached the end of the list, so we must be careful in
traversing that we do not end up in an infinite loop. In other words, say we were to write something like this:
Node curr = top;
while (curr != null) {
//do something with node pointed to by curr
curr = curr.next;
}
 
 
Search WWH ::




Custom Search