2012-12-06 44 views
0

我是java的初学者,因为它们的复杂性,我无法理解Java中的链表。所以我的代码非常简单。如何从循环链表中删除节点?

Node node, head, tail; 
head = null; // initializes the head of the linked list to null 
int counter = 0; 

String inputdata; // input 


do 
     { 
      System.out.print ("What name would you like stored? (\"quit\" to end) "); 
      inputdata = stdin.readLine(); 


      if (!inputdata.equals ("quit")) 
      { 
       node = new Node (inputdata); 
       node.next = head; 

       // update the head to point to the new front of the list 
       head = node; 
       count++; 
      } 
     } 
     while (!inputdata.equals ("quit")); // loop continues until "quit" selected 


     System.out.println(); 
     node = head; 



/////////////////////////////// 
    String delete; 
    boolean found; 
    System.out.println ("What node to delete?"); 
    delete = stdin.readLine(); 

do 
     { 
      for (int i = 0 ; i <= count ; i++) 
      { 

       if (delete.equals (node.data)) 
       { 
        found = true; 
        System.out.println ("It is found!"); 

       } 
      } 
     } 
     while (found = false); 

这是类

public class Node 
{ 
    Node next; 
    String data; 

    public Node (String data) 
    { 
     this.data = data; 
    } 
} 

我理解该算法是如何工作的。一个节点被搜索,当它被发现时,它指向节点之前的节点到搜索节点之后的节点。

enter image description here

每当我搜索节点我得到java.lang.nullpointer例外,基本上转化为我的代码是可怕的。

我需要帮助,因为无论何时我搜索如何做到这一点,我总是问自己“为什么放这个”或“什么是LS”或“为什么有多种方法,其中的变量n是多少”。

请告诉我我做错了什么,我需要做什么。

+0

虽然这可能不是解决方案(当我们不知道哪一行出现异常时,不能轻易给出解决方案),但搜索的while循环只会迭代一次。 'while while(found = false);'相当于'found = false; (while)(找到);'。相反,你应该检查等价性:'while while(found == false);'。 – Vulcan

+0

这看起来很简单。但是,当分配节点它看起来很棘手 –

回答

0
node = new Node (inputdata); 
      node.next = head; 

      // update the head to point to the new front of the list 
      head = node; 
      count++; 

1日你犯了一个节点,那你说从这个节点的下一个是头部...... ,然后你去说THET头是这个节点...所以你基本上是做头== ==节点node.next

这只是没有做:d

我的建议是:

//Init head and tail... 
if(head==null){ 
head = new Node("Head"); //use whatever data you want/need 
tail= new Node("Tail"); 
tail.next=head; 
head.next = tail; 
} 

//add a new node... 
newnode = new Node("Some data"); 
//since this is a one-way linked list, i suggest you walk from the head 
//and go until you meet the tail 
currNode = head; 
while(currNode.next.data.compareTo("Tail") != 0) 
{ 
    currNode = currNode.next; 
} 
//now add the new node here... 
newnode.next = currNode.next; 
currNode.next = newNode; 

这样,你总是把它添加到“寿终正寝”列表... 如果你想它在开始时添加,所以只是头部后使用:

newNode = new Node("Some data"); 
    newNode.next = head.next; 
    head.next = newNode; 

这adviseable,你有一个“限”像尾巴知道,当你在你的列表的末尾...

所以,现在您应该删除的工作,但我建议一些更多的东西:

currentNode = head; 
do 
    { 
     if(currentNode.next.data.compareTo(delete)==0){ //if the next one is the one i'm looking for, remove it and let the garbage collector take care of it 
      currentNode.next = currentNode.next.next; 
      break; //leave the loop 
     else 
      currentNode = currentNode.next; 
    } 
    while (currentNode.next.data.compareTo("Tail") != 0); 

有了这个while循环,你会遍历列表,直到最后/尾,并停止,如果:这不是什么t发现... 在你的例子中,它会一直出现在名单上,因为没有找到搜索到的节点

+1

唉拍摄,我忘了看看发布日期:/ 噢,希望它可以帮助未来的人.. – DaMachk