Java Data Structures: Sorted Lists and Hash Tables
Sorted Linked List Insertion
The following method, insertAtSorted(T x), demonstrates how to insert an element into a sorted linked list while maintaining the correct order.
public void insertAtSorted(T x) {
if (this.length == 0) {
this.firstNode = nn;
this.lastNode = nn;
} else if (((Comparable)x).compareTo(this.firstNode.data) < 0) {
nn.next = this.firstNode;
this.firstNode = nn;
} else {
SNode<T> curr = this.firstNode;
while (((Comparable)x).compareTo(curr.next.data) >= 0) {
curr = curr.next;
}
nn.next = curr.next;
curr.next = nn;
}
this.length++;
}Priority Queue Enqueue Implementation
The enqueuer method handles inserting items...