Sorted Lists
JAVA CODE TO INSERT AN ITEM IN A SORTED LIST
To insert an item in a sorted list, the algorithm must first search through the list until it finds the appropriate place to put the item: this is just before the first item that’s larger, as shown in Figure 5.12.
Once the algorithm finds where to put it, the item can be inserted in the usual way by changing next in the new link to point to the next link, and changing next in the previous link to point to the new link. However, there are some special cases to consider: the link might need to be inserted at the beginning of the list, or it might need to go at the end. Let’s look at the code:
public void insert(double key) // insert in order {
Link newLink = new Link(key); // make new link Link previous = null; // start at first Link current = first;
// until end of list, while(current != null && key > current.dData)
{ // or key > current, previous = current;
current = current.next; // go to next item }
if(previous==null) // at beginning of list first = newLink; // first −−> newLink else // not at beginning
previous.next = newLink; // old prev −−> newLink newLink.next = current; // newLink −−> old currnt
} // end insert()
We need to maintain a previous reference as we move along, so we can modify the previous link’s next
field to point to the new link. After creating the new link, we prepare to search for the insertion point by setting current to first in the usual way. We also set previous to null; this is important because later we’ll use this null value to determine whether we’re still at the beginning of the list.
The while loop is similar to those we’ve used before to search for the insertion point, but there’s an added condition. The loop terminates when the key of the link currently being examined (current.dData) is no longer smaller than the key of the link being inserted (key); this is the most usual case, where a key is inserted somewhere in the middle of the list.
However, the while loop also terminates if current is null. This happens at the end of the list (the next
field of the last element is null), or if the list is empty to begin with (first is null).
Once the while loop terminates, we may be at the beginning, the middle, or the end of the list, or the list may be empty.
If we’re at the beginning or the list is empty, previous will be null; so we set first to the new link. Otherwise, we’re in the middle of the list or at the end, and we set previous.next to the new link. In any case we set the new link’s next field to current. If we’re at the end of the list, current is null, so the new link’s next field is appropriately set to this value.
THE sortedList.Java PROGRAM
The sortedList.java example shown in Listing 5.6 presents a SortedList class with insert(),
remove(), and displayList() methods. Only the insert() routine is different from its counterpart in nonsorted lists.
Previous Table of Contents Next
MWSS: Data Structures and Algorithms in Java
by Robert Lafore
Waite Group Press, Macmillan Computer Publishing ISBN: 1571690956 Pub Date: 03/20/98
Previous Table of Contents Next
Listing 5.6 The sortedList.java Program
// sortedList.java
// demonstrates sorted list
// to run this program: C>java SortedListApp import java.io.*; // for I/O
//////////////////////////////////////////////////////////////// class Link
{
public double dData; // data item
public Link next; // next link in list // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public Link(double dd) // constructor
{ dData = dd; }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public void displayLink() // display this link { System.out.print(dData + “ “); }
} // end class Link
//////////////////////////////////////////////////////////////// class SortedList
{
private Link first; // ref to first item on list // −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public SortedList() // constructor
{ first = null; }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public boolean isEmpty() // true if no links { return (first==null); }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public void insert(double key) // insert in order
{
Link newLink = new Link(key); // make new link Link previous = null; // start at first Link current = first;
// until end of list, while(current != null && key > current.dData)
{ // or key > current, previous = current;
current = current.next; // go to next item }
if(previous==null) // at beginning of list first = newLink; // first −−> newLink else // not at beginning previous.next = newLink; // old prev −−> newLink newLink.next = current; // newLink −−> old currnt } // end insert()
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public Link remove() // return & delete first link { // (assumes non−empty list)
Link temp = first; // save first first = first.next; // delete first return temp; // return value }
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public void displayList()
{
System.out.print(“List (first−−>last): “);
Link current = first; // start at beginning of list while(current != null) // until end of list,
{
current.displayLink(); // print data
current = current.next; // move to next link }
System.out.println(“”); }
} // end class SortedList
//////////////////////////////////////////////////////////////// class SortedListApp
{
public static void main(String[] args)
{ // create new list SortedList theSortedList = new SortedList(); theSortedList.insert(20); // insert 2 items theSortedList.insert(40);
theSortedList.displayList(); // display list
theSortedList.insert(10); // insert 3 more items theSortedList.insert(30);
theSortedList.insert(50);
theSortedList.displayList(); // display list
theSortedList.remove(); // remove an item
theSortedList.displayList(); // display list } // end main()
} // end class SortedListApp
In main() we insert two items with key values 20 and 40. Then we insert three more items, with values 10, 30, and 50. These are inserted at the beginning of the list, in the middle, and at the end; showing that the
insert() routine correctly handles these special cases. Finally, we remove one item to show removal is always from the front of the list. After each change the list is displayed. Here’s the output from
sortedList.java:
List (first−−>last): 20 40
List (first−−>last): 10 20 30 40 50 List (first−−>last): 20 30 40 50