使用toString打印链接列表

Sha*_*awn 6 java linked-list pretty-print tostring

好的,所以我想学习如何打印链表.我有我需要用于列表的所有方法,但我无法弄清楚如何显示节点的值.现在我的main方法中没有任何内容,因为我在主要方法中尝试调用非静态方法时遇到错误.我有一个toString方法,显示列表的内容.我如何调用此toString来显示每个节点的值?任何建议将不胜感激.

这是节点类:

public class LinkedListNode
{

    private int data;
    private LinkedListNode next;


    public LinkedListNode(int data)
    {
        this.data = data;
        this.next = null;
    }

    public int getData()
    {
        return data;
    }

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

    public LinkedListNode getNext()
    {
        return next;
    }

    public void setNext(LinkedListNode n)
    {
        next = n;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是LinkedList类,它包含操作列表的main和方法:

public class LinkedList {

    public LinkedListNode head;

    public static void main(String[] args) {

    LinkedList l = new LinkedList();
    l.insertFront(0);
    System.out.println(l.toString());

    }

    public LinkedList() {
        this.head = null;
    }

    public int removeFront(){
        if(head == null){
            System.out.println("Error - Attempting to call removeFront() on empty list");
            return 0;
        }else{
            int temp = head.getData();
            head = head.getNext();  
            return temp;
        }

    }

    public void insertFront(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            newNode.setNext(head);
            head = newNode;
        }       
    }

    public void insertBack(int data){
        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }       
    }

    public int removeBack(){
        if(head == null){
            System.out.println("Error - Attempting to call removeBack() on empty list");
            return 0;
        }else if (head.getNext() == null){
            int temp = head.getData();
            head = null;
            return temp;
        }else{

            LinkedListNode current = head;
            while(current.getNext().getNext() != null){
                current = current.getNext();
            }
            int temp = current.getNext().getData();
            current.setNext(null);
            return temp;
        }       
    }

    public String toString(){
        String retStr = "Contents:\n";

        LinkedListNode current = head;
        while(current != null){
            retStr += current.getData() + "\n";
            current = current.getNext();

        }

        return retStr;
    }

    public LinkedListNode getHead() {
        return head;
    }

    public void setHead(LinkedListNode head) {
        this.head = head;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

public static void main(String[] args) {

    LinkedList list = new LinkedList();
    list.insertFront(1);
    list.insertFront(2);
    list.insertFront(3);
    System.out.println(list.toString());
}

String toString() {
            String result = "";
            LinkedListNode current = head;
            while(current.getNext() != null){
                result += current.getData();
                if(current.getNext() != null){
                     result += ", ";
                }
                current = current.getNext();
            }
            return "List: " + result;
}
Run Code Online (Sandbox Code Playgroud)


Twe*_*les 5

正如其他一些答案和评论中所指出的,这里缺少的是对 JVM System 类的调用,以打印出 toString() 方法生成的字符串。

LinkedList myLinkedList = new LinkedList();
System.out.println(myLinkedList.toString());
Run Code Online (Sandbox Code Playgroud)

这将完成工作,但我不建议这样做。如果我们查看 Object 类的 javadoc,我们会发现 toString() 的描述:

返回对象的字符串表示形式。一般来说,toString 方法返回一个“以文本方式表示”该对象的字符串。结果应该是简洁但信息丰富的表示形式,易于人们阅读。建议所有子类都重写此方法。

那里添加的重点是我自己的。您正在创建一个包含链接列表的整个状态的字符串,使用您的类的人可能没有想到这一点。我建议进行以下更改:

  1. 将 toString() 方法添加到 LinkedListNode 类中。
  2. 更新 LinkedList 类中的 toString() 方法以使其更加简洁。
  3. 将一个名为 printList() 的新方法添加到您的 LinkedList 类中,该方法执行您当前期望 toString() 执行的操作。

在链表节点中:

public String toString(){
   return "LinkedListNode with data: " + getData();
}
Run Code Online (Sandbox Code Playgroud)

在链表中:

public int size(){
    int currentSize = 0;
    LinkedListNode current = head;
    while(current != null){
        currentSize = currentSize + 1;
        current = current.getNext();
    }

    return currentSize;
}

public String toString(){
    return "LinkedList with " + size() + "elements.";
}

public void printList(){
    System.out.println("Contents of " + toString());

    LinkedListNode current = head;
    while(current != null){
        System.out.println(current.toString());
        current = current.getNext();
    }

}
Run Code Online (Sandbox Code Playgroud)