为什么它不打印?链表

huangapple go评论62阅读模式
英文:

why it doesn't print ? LinkedList

问题

i have a problem that the method in LinkedList class don't print anything.. and i'm trying hard to know what's the problem i hope someone help

the main class

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

LinkedList class

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

i have a problem that the method in LinkedList class don't print anything.. and i'm trying hard to know what's the problem i hope someone help

the main class

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

LinkedList class

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

why it dosn't print i tried so hard

答案1

得分: 4

这是因为您从未向您的链表中添加任何节点。

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

主类:

    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;
        }
       
       // 构造函数
       public LinkedList() {
          head = null;
       }
       
       // 示例方法以检查列表是否为空
       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();
            }
       }
    }

注意:上述代码中的注释已经进行了翻译。

英文:

This is because you never add any nodes to your LinkedList.

Code could be as follows. Beware, nodes will be added at the beginning of list.

Main class:

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

}

LinkedList class:

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

huangapple
  • 本文由 发表于 2020年10月1日 22:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64157338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定