【问题标题】:linked list java refernce链表java参考
【发布时间】:2017-04-15 02:11:56
【问题描述】:

我有一个链表 java 实现。但有一部分我不明白。我的课是

class Node {
    int data;
    Node next;
    Node(int d) {
        data = d;
        next = null;
    }
}

我要插入一个 fn

public static  Node insert(Node head,int data) {
  Node current = head;

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

    current.next = new Node(data);

    return head;
  } else {
    return head=new Node(data);
  }
}

我不明白的是,首先我们将 head 设置为当前变量。 并将下一个节点传递给当前对象进行遍历。

我的问题是它是如何工作的,因为 current 有 head 的参考,所以当你从技术上分配另一个值时,你正在改变 head。我可以看到 int data.if 我将 current.data 更新为 0 然后我看到 head 受到影响..

可能是一个低于标准的问题,但请帮助我了解这里发生了什么......

【问题讨论】:

    标签: java linked-list insert implementation


    【解决方案1】:

    这个函数基本上是添加新元素作为链表集合的 HEAD 元素。每个元素都有一个对 NEXT 元素的引用,因此只要下一个元素不存在,它就会遍历,然后将其设置为新元素(具有您传递给函数的数据)。 我不确定我是否理解您的担忧,但是通过更改“当前”变量,您只是将“引用”更改为对象,而不是更改对象本身。因此,只要下一项不存在,您只需更改引用,然后创建一个新对象并设置为前头所引用的对象(并且该对象成为新头)

    【讨论】:

      【解决方案2】:

      我重新排列了代码,使其更易于理解,并添加了 cmets:

      /**
       *Insert a leaf node in a linked list
       *@param head represents the head node of the list.
       *@return the head node 
       */
      public static  Node insert(Node head,int data) {
      
           //if head does not exist, create it and return it 
          if (head==null) {
              return head=new Node(data);
          }
      
          else{//head exist
      
              //search for the end of the linked list (leaf, has no next node)
              Node current = head;
              while(current.next!=null){
                  current = current.next;  
              }
              //at the end of loop the current.next == null (leaf)
              //add new node as leaf
              current.next = new Node(data);
              return head; //return head unchanged 
          } 
      
      }
      

      我希望它有助于澄清。

      【讨论】:

        【解决方案3】:

        当您将某个节点(不仅仅是值)分配给当前节点时,您不会更改该特定节点。在这里,您已将 head 指定为当前节点。但这并不意味着两个节点现在是相同的。他们还是不同的。 head 节点将始终具有相同的值,直到有人专门键入 head = [enter another node here],将不同的节点分配给 头节点。在 Java 中,= 表示赋值,== 表示等于。所以赋值和等于是两个不同的概念。

        单链表示例: 1 -> 2 -> 3 -> NULL (我们知道head = Node(1)) 现在,假设用户调用insert(head, 4)

        执行步骤:

        1. Node current = head,所以 current == Node(1) 和 head == Node(1)(current 和 head 是两个不同的节点,但现在具有相同的值)
        2. head为null,所以执行if语句中的语句
        3. current.next == Node(2) 所以它不为空。在while循环中执行语句
        4. current = current.next 所以当前分配给 Node(2)
        5. current.next == Node(3) 所以它不为空。在while循环中执行语句
        6. current = current.next 所以当前分配给 Node(3)
        7. current.next == NULL,所以停止 while 循环
        8. current.next = new Node(4),所以我们将 Node(4) 分配给 current.next
        9. 现在我们返回从头开始的列表。并且 head = Node(1) 仍然存在。

        结果:1​​ -> 2 -> 3 -> 4 -> NULL

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-04
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多