views:

63

answers:

2

im learning linked lists in java but am stuck on the following

Before deleting Hobnobs: bourbon, shortbread, Hobnobs, oreos

After deleting Hobnobs: bourbon, shortbread, oreos

i would like to create a delete method that will delete the intermediate node "Hobnobs".

ive got this so far

 public class Biscuit {

 private BiscuitNode first;
 public Biscuit( )
 {
  this.first=null;
 }
 public BiscuitNode getFirst() {
  return first;
 }
 public void insert(BiscuitNode first) {
  this.first = first;
 }

 public void deleteFirst()
 {
  this.first.setSucc(this.first);
 }

 public void delete(String BiscuitName)
 {

the Hobnobs is "BiscuitNode secondLast=new BiscuitNode("Hobnobs", last);"

+1  A: 

Walk the chain of nodes in a loop using getSucc() on the current node. Then, once you find the node that matches, make its predecessor point to its successor. E.g. if we want to delete node C from the following list:

A--->B--->C--->D

becomes

A--->B--->D

You should be able to figure it out.

Pierre-Antoine LaFayette
sorry i dont understand
See http://stackoverflow.com/questions/10042/how-do-i-implement-a-linked-list-in-java.
Pierre-Antoine LaFayette
thank you mate, i get it now.
+1  A: 

Add a recursive method to BiscuitNode, maybe named deleteYourSuccessorIfItMatches(String pattern) that checks if it's successor matches the pattern and if it does, the successor of the current node would be set to the successor of the successor, else the method is called recursively on the successor. The calling method, Biscuit.delete(String pattern) has to do the same thing on the first node.

maligree