链接列表:未按预期工作

Linked list: Not working as expected

我创建了一个单链表,它给出了以下错误。不知道哪里不对,蚂蚁建议

错误/OP - 列表是 javaTest.LinkedListcreation@1540e19d

我不确定输出中的这个值是什么意思。

进程已完成,退出代码为 0

public  class LinkedList{

public static  void main (String[] a){
    LinkedListcreation L1 = new LinkedListcreation();
    L1.addNodeAtEnd("1");
    System.out.print("List is " + L1);
}

}

class LinkedListcreation {

int listcount;
node head;

LinkedListcreation() {
    head = new node(0);
    listcount=0;
}


node Temp;

void addNodeAtEnd(Object d){
    node Current = head;
    Temp = new node(d);
    while (Current.getNext()!= null){
        Current = Current.getNext();
    }

    Current.setNext(Temp);

    listcount++;
}

}


class node {

Object data;
node next;

node(Object d) {
    next = null;
    this.data=d;
}

node(Object d, node nextNode) {
    next = nextNode;
    this.data=d;
}

   public Object getdata(){
   return data;
   }


public void setdata(int d){
    data = d;
}

public node getNext(){
    return next;
}

public void setNext (node nextValue){
    next = nextValue;
}
}

您的代码没有问题,但是为了打印有关对象(本例中的列表)的有用信息,您需要重写 LinkedListcreation [=17] 中的 toString 方法=].

例如:

public String toString() {
    return "List with " + this.listcount + " nodes.";
}

您正在尝试打印列表对象而不是您添加的元素,而且您所看到的内容并非错误。检查 java 中的 toString() 方法以了解您看到的输出。

如下修改您的 main() 以查看您添加的元素。

 public static  void main (String[] a){
   LinkedListcreation L1 = new LinkedListcreation();
  L1.addNodeAtEnd("1");
   System.out.print("List is " + L1.head.next.data);

}

output : 列表是 1

代码有编译器错误。尝试下面更正的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class LinkedList
    {

        static void Main(String[] a){
           LinkedListcreation L1 = new LinkedListcreation();
           L1.addNodeAtEnd("1");
           Console.WriteLine("List is " + L1);
        }

    }

    public class LinkedListcreation
    {

        int listcount;
        node head;

        public LinkedListcreation()
        {
            head = new node(0);
            listcount = 0;
        }


        node Temp;

        public void addNodeAtEnd(Object d)
        {
            node Current = head;
            Temp = new node(d);
            while (Current.getNext() != null)
            {
                Current = Current.getNext();
            }

            Current.setNext(Temp);

            listcount++;
        }

    }


    public class node
    {

        Object data;
        node next;

        public node(Object d)
        {
            next = null;
            this.data = d;
        }

        node(Object d, node nextNode)
        {
            next = nextNode;
            this.data = d;
        }

        public Object getdata()
        {
            return data;
        }


        public void setdata(int d)
        {
            data = d;
        }

        public node getNext()
        {
            return next;
        }

        public void setNext(node nextValue)
        {
            next = nextValue;
        }
    }

}
​

正如大家所说,你必须覆盖 toString()。这里你有正确的实现:

public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    sb.append(head.data.toString());

    node n;
    while(n = head.getNext() != null)
        sb.append(", " + n.data.toString());

    sb.append("]");
    return sb.toString();
}

您的代码没有任何错误。如果您想打印列表中的节点,您只需在 LinkedListcreation class 中添加另一个函数,它将遍历您的列表并打印每个节点的数据。在您的 LinkedList 创建中添加此块 class.

public void printList(){
node current = head.next;
while(current!=null){
    System.out.println("node's data is: "+ current.getdata());
    current = current.getNext();
}

}

同样在您的主函数中使用列表的对象 L1 调用上述函数。

L1.printList();