【问题标题】:Trying to figure out size of a linked list null pointer error试图找出链表空指针错误的大小
【发布时间】:2019-01-06 05:27:33
【问题描述】:

对于下面的代码,我想知道为什么链表的大小总是给我一个空指针异常以及为什么我的 pushEnd 方法在最后推送一个新节点不起作用,它在几个之后添加一个元素节点并摆脱休息。

class Node {
int data;
Node next;
Node(int data){
    this.data = data;
}

}

public class LinkedList {
Node head;  

/* Inserts a new Node at front of the list. */
public Node push(int data) 
{ 
    Node newNode = new Node(data); 
    newNode.next = head; 
    return head = newNode; 
}

public Node pushEnd(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
    }

    newNode.next = null;

    while(head != null) {
        head = head.next;
        head.next = newNode;
        return newNode;
        }
    return head;
}

public int getSize() {
    int size = 0;
    while(this.head != null) {
        size++;
        head = head.next;
    }
    return size;
}


public void printList() {
    while (this.head !=null) {
        System.out.print(head.data + "-->");
        head = head.next;
    }       
    System.out.println(head);
}

}

public class Tester {


public static void main(String[] args) {

    LinkedList ll = new LinkedList();

    ll.push(35);
    ll.push(100);
    ll.push(14);
    ll.push(44);
    ll.push(10);
    ll.push(8);


    System.out.println("Created Linked list is:"); 
    ll.printList(); 



    System.out.println(ll.getSize());

}

}

我想弄清楚链表的大小,并能够在最后添加节点。

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    您的while 循环直接修改head 变量。这会导致您的其他代码失败,因为现在 head 指向列表中的最后一个节点。

    创建一个新的局部变量以在 while 循环中使用(而不是直接修改 head)。那应该可以解决它!

    【讨论】:

      【解决方案2】:

      您正在更改 head 引用,因此您得到的输出不正确。你应该让temp引用head,使用temp进行操作,不会影响head。应该是这样的:

      public class LinkedList {
          Node head;
      
          /* Inserts a new Node at front of the list. */
          public void push(int data) {
              Node newNode = new Node(data);
              newNode.next = head;
              head = newNode;
          }
      
          public void pushEnd(int data) {
              Node newNode = new Node(data);
              if (head == null) {
                  head = newNode;
                  return;
              }
      
              newNode.next = null;
              Node temp = head;
              while (temp.next != null) {
                  temp = temp.next;
              }
              temp.next = newNode;
          }
      
          public int getSize() {
              int size = 0;
              Node temp = head;
              while (temp != null) {
                  size++;
                  temp = temp.next;
              }
              return size;
          }
      
      
          public void printList() {
              Node temp = this.head;
              while (temp != null) {
                  System.out.print(temp.data + "-->");
                  temp = temp.next;
              }
              System.out.println(temp);
          }
      }
      
      class Node {
          int data;
          Node next;
      
          Node(int data) {
              this.data = data;
          }
      }
      

      【讨论】:

      • 欢迎@madil26
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 2017-06-02
      • 2020-08-07
      相关资源
      最近更新 更多