Here is the code for the list:
public class SortedList {
private DoubleNode head = null;
private int listLength;
public static void main(String[] args) {
SortedList list = new SortedList();
list.insert(4);
list.insert(5);
list.insert(6);
list.insert(2);
list.insert(-5);
list.insert(20);
list.insert(-20);
list.insert(7);
list.insert(-29);
list.insert(23);
list.insert(-100);
list.insert(-99);
list.insert(-101);
list.insert(10);
System.out.println(list.toString());
}
public void insert(double value) {
if(listLength == 0 || value <= head.getData()) {
head = new DoubleNode(value, head);
listLength++;
}
else {
DoubleNode temp = new DoubleNode(value, null);
DoubleNode current = head;
//while the given value is less than the current nodes value, go to the next node
for (int i = 1; value > current.getData() && current.getLink() != null; i++) {
current = current.getLink();//set current as the next node
}
temp.setLink(current.getLink()); //set temp's link as current's link
current.setLink(temp);
listLength++;
}
}
public String toString() {
String answer = "[ ";
for (DoubleNode current = head; current != null; current = current
.getLink()) {
answer += current.getData() + " ";
}
answer += "]";
return answer;
}
}
and here is the code for the node:
// File: DoubleNode.java based on the DoubleNode class by Michael Main
/**************************************************************************
* DoubleNode provides a node for a linked list with double data in each node.
*
* @note
* Lists of nodes can be made of any length, limited only by the amount of
* free memory in the heap.
*
* @author Michael Main
* shortened by Beth Katz and Stephanie Elzer to be only the basics
*
* @version
* February 2007
***************************************************************************/
public class DoubleNode
{
// Invariant of the DoubleNode class:
// 1. The node's double data is in the instance variable data.
// 2. For the final node of a list, the link part is null.
// Otherwise, the link part is a reference to the next node of the list.
private double data;
private DoubleNode link;
/**
* Initialize a node with a specified initial data and link to the next
* node. Note that the initialLink may be the null reference, which
* indicates that the new node has nothing after it.
* @param initialData
* the initial data of this new node
* @param initialLink
* a reference to the node after this new node--this reference may be
* null to indicate that there is no node after this new node.
* @postcondition
* This node contains the specified data and link to the next node.
**/
public DoubleNode(double initialData, DoubleNode initialLink)
{
data = initialData;
link = initialLink;
}
/**
* Accessor method to get the data from this node.
* @param - none
* @return
* the data from this node
**/
public double getData( )
{
return data;
}
/**
* Accessor method to get a reference to the next node after this node.
* @param - none
* @return
* a reference to the node after this node (or the null reference if
* there is nothing after this node)
**/
public DoubleNode getLink( )
{
return link;
}
/**
* Modification method to set the data in this node.
* @param newData
* the new data to place in this node
* @postcondition
* The data of this node has been set to newData.
**/
public void setData(double newData)
{
data = newData;
}
/**
* Modification method to set the link to the next node after this node.
* @param newLink
* a reference to the node that should appear after this node in the
* linked list (or the null reference if there is no node after this node)
* @postcondition
* The link to the node after this node has been set to newLink. Any other
* node (that used to be in this link) is no longer connected to this node.
**/
public void setLink(DoubleNode newLink)
{
link = newLink;
}
}
Thanks

New Topic/Question
Reply



MultiQuote






|