简体   繁体   English

链接列表:无法正常工作

[英]Linked list: Not working as expected

I created a singly linked list , it giving below error . 我创建了一个单链表,它给出以下错误。 not sure what is wrong, ant suggestion 不确定出了什么问题,蚂蚁建议

Error/ OP - List is javaTest.LinkedListcreation@1540e19d 错误/ OP-列表为javaTest.LinkedListcreation@1540e19d

I am not sure what this value in Output is meant for . 我不确定Output中的这个值是什么意思。

Process finished with exit code 0 流程结束,退出代码为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;
}
}

Your code is all right, but in order to print useful information about an object (your list in this example), you need to override the toString method in your LinkedListcreation class. 您的代码可以,但是为了打印关于对象的有用信息(在此示例中为您的列表),您需要在LinkedListcreation类中重写toString方法。

For example: 例如:

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

you are trying to print the list object rather than the element which you have added and more over what you see is not error. 您正在尝试打印列表对象,而不是打印已添加的元素,并且所看到的更多内容不是错误。 check about toString() method in java to understand the output which you see. 检查Java中的toString()方法以了解您看到的输出。

Modify your main() as below to see the element you added. 如下修改main(),以查看添加的元素。

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

} }

output : List is 1 输出 :列表为1

As everybody said, you have to override toString() . 正如大家所说,您必须重写toString() Here you have the right implementation: 在这里,您有正确的实现:

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

Your code does not have any error. 您的代码没有任何错误。 If you want to print the nodes in your list you just have to add another function in you LinkedListcreation class which will iterate over your list and print each node's data. 如果要打印列表中的节点,只需在LinkedListcreation类中添加另一个函数即可,该函数将遍历列表并打印每个节点的数据。 Add this block in your LinkedListcreation class. 将此块添加到LinkedListcreation类中。

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

} }

Also in your main function call the above mentioned function using your list's object L1. 同样在您的主函数中,使用列表的对象L1调用上述函数。

L1.printList();

The code has compiler errors. 该代码有编译器错误。 Try corrected code below 请尝试以下更正的代码

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

}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM