• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help with delete() method for linked list

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Long
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Get off me! Here, read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic