簡體   English   中英

如何從循環鏈表中刪除節點?

[英]How can I remove a node from a circular linked list?

我是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;
    }
}

我了解該算法的工作原理。 搜索一個節點,當找到它時,將其之前的節點指向被搜索節點之后的節點。

在此處輸入圖片說明

每當我搜索節點時,都會得到java.lang.nullpointer異常,這些異常基本上會轉化為我的代碼,這是可怕的。

我需要幫助,因為每當我搜索如何執行此操作時,我總是對自己問:“為什么放這個”或“什么是LS”或“為什么有多個方法,其中的變量n為多少”。

請告訴我我做錯了什么,我需要做什么。

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

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

首先創建一個節點,然后說該節點的下一個節點是頭...然后說該節點是頭...所以基本上是在使頭==節點==節點。下一個

那只是不做: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;

建議您有一個“限制器”,例如尾巴,以知道何時位於列表的末尾...

因此,現在您的刪除應該可以了,但是我建議您多做一些事情:

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循環中,您將遍歷列表直到尾部/尾部,如果找不到則停止搜索...在您的示例中,它將一直遍歷列表,因為未找到搜索到的節點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM