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

我了解算法的工作原理。搜索一个节点,当它找到它时,它将在它之前的节点指向正在搜索的节点之后的节点。

在此处输入图像描述

每当我搜索节点时,我都会得到 java.lang.nullpointer 异常,这些异常基本上会转化为我的代码,这很糟糕。

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

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

4

1 回答 1

0
 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循环,您将遍历列表直到结束/尾部,如果找不到则停止...在您的示例中,它将永远在列表中四处走动,因为未找到搜索的节点

于 2013-07-26T08:00:08.727 回答