public class LinkedGroup
1: /**
2: @author Frank M. Carrano
3: @author Timothy M. Henry
4: @version 5.0
5: */
6: public class LinkedGroup<T extends Comparable<? super T>>
7: {
8: private Node firstNode;
9: int length; // Number of objects in the group
10:
11: // . . .
13: private void insertInOrder(Node nodeToInsert)
14: {
15: T item = nodeToInsert.getData();
16: Node currentNode = firstNode;
17: Node previousNode = null;
19: // Locate insertion point
20: while ( (currentNode != null) &&
21: (item.compareTo(currentNode.getData()) > 0) )
22: {
23: previousNode = currentNode;
24: currentNode = currentNode.getNextNode();
25: } // end while
27: // Make the insertion
28: if (previousNode != null)
29: { // Insert between previousNode and currentNode
30: previousNode.setNextNode(nodeToInsert);
31: nodeToInsert.setNextNode(currentNode);
32: }
33: else // Insert at beginning
34: {
35: nodeToInsert.setNextNode(firstNode);
36: firstNode = nodeToInsert;
37: } // end if
38: } // end insertInOrder
39:
40: private class Node
41: {
42: private T data; // Entry in bag
43: private Node next; // Link to next node
44:
45: private Node(T dataPortion)
46: {
47: this(dataPortion, null);
48: } // end constructor
49:
50: private Node(T dataPortion, Node nextNode)
51: {
52: data = dataPortion;
53: next = nextNode;
54: } // end constructor
55:
56: private T getData()
57: {
58: return data;
59: } // end getData
60:
61: private void setData(T newData)
62: {
63: data = newData;
64: } // end setData
65:
66: private Node getNextNode()
67: {
68: return next;
69: } // end getNextNode
70:
71: private void setNextNode(Node nextNode)
72: {
73: next = nextNode;
74: } // end setNextNode
75: } // end Node
76: } // end LinkedChainList