繁体   English   中英

为什么它不打印? 链表

[英]why it doesn't print ? LinkedList

我有一个问题,LinkedList 类中的方法不打印任何东西..我正在努力知道问题是什么,希望有人帮忙

主班

    public static void main(String[] args) {
        LLnode a = new LLnode(10);
        LLnode b = new LLnode(20);
        LLnode c = new LLnode(50);
        LinkedList List1 = new LinkedList();
      
      List1.printAllNodes();
    }
}

链表类

public class LinkedList {
   
   private LLnode head;
   
  public LLnode gethead() {
        return this.head;
    }
    public void sethead(LLnode LLnode) {
        this.head = LLnode;
    }
   // Constructor
   public LinkedList() {
      head = null;
   }
   // Example Method to check if list is empty
   public boolean isEmpty() {
      return head == null;
   }
   
   public void printAllNodes() {
    LLnode helpPtr = head;
    while (helpPtr != null) {
        System.out.print(helpPtr.getdata() + " ");
        helpPtr = helpPtr.getnext();
   }

为什么它不打印我这么努力

这是因为您从不向 LinkedList 添加任何节点。

代码可能如下。 请注意,节点将添加到列表的开头。

主要类:

public static void main(String[] args) {
    LLnode a = new LLnode(10);
    LLnode b = new LLnode(20);
    LLnode c = new LLnode(50);
    LinkedList List1 = new LinkedList();
    List1.add(a).add(b).add(c);
  
  List1.printAllNodes();
}

}

链表类:

public class LinkedList {
   
   private LLnode head;
   
  public LLnode gethead() {
        return this.head;
    }
    public void sethead(LLnode LLnode) {
        this.head = LLnode;
    }
   // Constructor
   public LinkedList() {
      head = null;
   }
   // Example Method to check if list is empty
   public boolean isEmpty() {
      return head == null;
   }
   
   public LinkedList add(LLnode node){
       LLnode oldHead = this.head();
       this.head = node;
       node.setNext(oldHead);
       return this;
   }

   public void printAllNodes() {
    LLnode helpPtr = head;
    while (helpPtr != null) {
        System.out.print(helpPtr.getdata() + " ");
        helpPtr = helpPtr.getnext();
   }
}

暂无
暂无

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

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