首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Java中添加到链表

在Java中添加到链表
EN

Stack Overflow用户
提问于 2018-09-30 07:28:11
回答 3查看 386关注 0票数 1

代码可以编译并运行,但addToEnd方法不起作用,在我看来似乎是正确的,但我不确定错误在哪里,有人可以指导我需要修复代码的内容或位置吗

下面是我的另一个类的代码

 public class LinkedList {
 private Node head;


 /**
  * constructor
  * pre: none
  * post: A linked list with a null item has been created.
  */
 public LinkedList() {
  head = null;
 }

 /** 
  * Activity: finds size of the Linked List.
  * Pre-Condition: none
   * Post-Condition: The size of the list is returned
  */
 public int size() {
   int counter  = 0;
   Node current = head;

   while(current != null) {
    counter++;
    current = current.getNext();
  }
   return counter;   
}

  /** 
  * Adds a node to the end of the linked list.
  * pre: String parameter
  * post: The linked list has a new node at the end.
  */
 public void addAtEnd(String s) {
  Node current = head;
  Node newNode = new Node(s);
   if(head == null) {
     head = newNode;
     head.setNext(null);
   } 
   else {
     while(current.getNext() == null) {
       current.setNext(newNode);
       current = newNode;
   } 
 }
 }

 private class Node {
 private String data;
 private Node next;


  /**
   * constructor
   * pre: none
   * post: A node has been created.
   */
  public Node(String newData) {
   data = newData;
   next = null;
  }


  /**
   * The node pointed to by next is returned
   * pre: none
   * post: A node has been returned.
   */
  public Node getNext() {
   return(next);
  }


  /**
   * The node pointed to by next is changed to newNode
   * pre: none
   * post: next points to newNode.
   */
  public void setNext(Node newNode) {
   next = newNode;
  }


  /**
   * The node pointed to by next is returned
   * pre: none
   * post: A node has been returned.
   */
  public String getData() {
  return(data);
  }
 }
}

这是我的主类代码,Blume和Dahl从未被添加到列表中:

 public class LinkedListDemo {

 public static void main(String[] args) {
 LinkedList list = new LinkedList();

  list.addAtFront("Sachar");
  list.addAtFront("Osborne");
  list.addAtFront("Suess");
  System.out.println("List has " + list.size() + " items.");
  System.out.println(list);

  list.addAtEnd("Blume");
  list.addAtEnd("Dahl");
  System.out.println(list);
}
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-30 07:36:45

 public void addAtEnd(String s) {
  Node current = head;
  Node newNode = new Node(s);
   if(head == null) {
     head = newNode;
     head.setNext(null);
   } 
   else {// problem is here. You need to find the  node that has getNext()==null, 
         //so you need to loop all nodes where get next != null
     while(current.getNext() == null) { 
       current.setNext(newNode);
       current = newNode;
   } 
 }

更改为此

else{ 
    //iterate to the last node
    while(current.getNext() != null) { 
    current = current.getNext(); 
    } 
    //Append the new node to the end
    current.setNext(newNode);  
}
票数 1
EN

Stack Overflow用户

发布于 2018-09-30 07:36:08

while (current.getNext() == null) {
    current.setNext(newNode);
    current = newNode;
}

这是不正确的;因为您没有跟踪列表的尾部,所以需要遍历整个列表,然后在末尾添加newNode (我相信您已经理解了这一点)。为此,只需将current设置为current.getNext(),直到current.getNext()null,然后调用current.setNext(newNode);

Node next;

while ((next = current.getNext()) != null) {
    current = next;
}

current.setNext(newNode);
票数 1
EN

Stack Overflow用户

发布于 2018-09-30 07:39:03

public void addAtEnd(String s) {
    Node current = head;
    Node newNode = new Node(s);
    if(current == null) {
        head = newNode;
    } else {
        while(current.getNext() != null) {
            current = current.getNext();
        }
        current.setNext(newNode); 
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52573356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档