I have tried a bunch of different ways to implement a delete method here,I cant get anything to work. Still pretty new at this programming stuff, but for some reason i cant get the code right. would someone please point me in the right direction!
Thanks
Notes & Caveats:
-The Node class has been made into a private inner class so that this class
is entirely self-contained.
******************************************************************************/
public class OrderedList
{
private Node listHead;
//-----------------------------------------------------------------------------
// constructor creates a Node to serve as the list head.
public OrderedList()
{
listHead = new Node();
}
//-----------------------------------------------------------------------------
// Method: void insert(int value)
// Insert a new value at its proper place in the list (acsending order).
public void insert(int value)
{
Node prev = null;
Node node = listHead;
Node newNode = new Node(value);
do
{
if (value < node.item) break;
prev = node;
node = node.link;
}
while (node != null);
newNode.link = prev.link;
prev.link = newNode;
}
//-----------------------------------------------------------------------------
// Method:
String delete(int value)i
// delete a value from the list if it exists. If the value is not found no
// the list is unchanged.
public boolean delete(int value)
{
//-----------------------------------------------------------------------------
// Method: boolean query(int value)
// Returns true if the specified value is in the list, otherwise false.
public boolean query(int value)
{
for (Node node = listHead.link; node != null; node = node.link)
{
if (value == node.item) return(true);
}
return false;
}
//-----------------------------------------------------------------------------
// Method: String toString()
// create a String contains all the values in the list delimited by newlines
public String toString()
{
String list = "";
for (Node node = listHead.link; node != null; node = node.link)
{
list += node.item+"\n";
}
return list;
}
//=============================================================================
// Node class defintion as an inner class.
// The data field is an int, but typically it would be an object reference.
// There are 3 constructors; two require a value for the data field, and the
// third uses the minimum integer value (this is useful for the head node).
private class Node
{
int item;
Node link;
//-----------------------------------------------------------------------------
// constructors
public Node()
{
this(Integer.MIN_VALUE, null);
}
public Node(int newItem)
{
this(newItem, null);
}
public Node(int newItem, Node newLink)
{
item = newItem;
link = newLink;
}
}
//=============================================================================
// driver for the OrderedList class
public static void main(String[] args)
{
OrderedList myList = new OrderedList();
// build a list using the insert() method
myList.insert(50);
myList.insert(40);
myList.insert(70);
myList.insert(20);
myList.insert(30);
myList.insert(90);
myList.insert(80);
myList.insert(10);
System.out.println(myList);
myList.delete(10);
}
}