File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Help with delete() method for linked list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with delete() method for linked list" Watch "Help with delete() method for linked list" New topic
Author

Help with delete() method for linked list

Joe Long
Greenhorn

Joined: Mar 07, 2004
Posts: 4
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);
}
}
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
I didn't test the code. But hope this might work
public boolean delete(int value)
{
Node preNode=listHead;
Node currentNode=listHead;
Node nextNode=listHead.link;
do {
if (value == currentNode.item) break;
preNode = currentNode;
currentNode = nextNode;
nextNode = nextNode.link;
}
while (nextNode != null);
//delete the node
currentNode = null;
preNode.link = nextNode;
} //end of method


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Joe Long
Greenhorn

Joined: Mar 07, 2004
Posts: 4
Hey thanks alot for looking at this and giving me some help, Sometimes all it takes is an extra pair of eyes. Code works great, when testing though if value to be deleted is not in the list, then list should remain unchanged, instead the code deletes the last node in the list. Any suggestions?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with delete() method for linked list