【问题标题】:Is this the correct visualization of a LinkedList?这是 LinkedList 的正确可视化吗?
【发布时间】:2020-04-30 12:13:37
【问题描述】:

我目前正在研究 Linked List,我在 stackoverflow、geeksforgeeks 中查看了一些问题,我只想澄清我的理解是否正确。在我展示我的可视化之后,我还有几个问题。

这里有一个简单的java程序介绍链表

public class LinkedList {
    Node head;
    public static void main (String [] args){
        LinkedList list = new LinkedList();
        list.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);

        list.head.next = second;
        second.next = third;
    }
}

class Node{
    int data;
    Node next;

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

这将创建一个 LinkedList (1,2,3)

据我了解,这里是创建LinkedList的步骤,如果我错了,请纠正我

  1. 您有 2 个班级,即 class LinkedListclass Node
  2. class LinkedList 中我声明了一个可以稍后使用的对象Node head;
  3. class Node 内部是另一个对象声明Node next; 和一个将接受数据的构造函数(我创建了一组节点)
  4. 回到class LinkedList,我实例化了一个object list,它将成为节点的容器,所以它就像class Node的对象在class LinkedList的对象内部

这是我的可视化:

我的问题是:

  1. 为什么我不能将头部创建为Node head = new Node(1); 而不是Node head;
  2. 据我了解,对象list 被创建为class Node 对象的容器,对吗?

  3. 最后为什么我必须在 Node 构造函数中声明Node next;,为什么我必须设置为next = null;

【问题讨论】:

  • 1.因为他们可能正在引入数据结构,请注意您正在创建一个LinkedList(不是Node);你看到了目的地,他们向你展示了起点。 2.似乎是。 3. 你没有。 next = null 将是默认值。您确实需要声明Node next; - 所以一个Node 可以指向下一个。您的可视化似乎不正确。 1 2 3 不在 Node 构造函数中。这是一个typical visualization(节点将是两个框)(来自 geekforgeek 的图像 - 他们使用 ABCD 而不是 1234)。
  • 现在我明白为什么 next = null;对不起,我傻了,差点忘了它稍后会链接到 (list.head.next = second;) 谢谢!

标签: java data-structures linked-list


【解决方案1】:
  1. 您可以使用 Node head = new Node(1); 创建一个新节点,这只是他们介绍了单链表的外观。
  2. 是的
  3. 三个节点已被动态分配,因此默认值为next = null;,因为稍后您将链接每个节点:llist.head.next = second; second.next = third

这是一个完整的表示:

     /* Start with   the empty list. */
     LinkedList2 llist = new LinkedList2(); 

     llist.head = new Node(1); 
     Node second = new Node(2); 
     Node third = new Node(3); 

     /* Three nodes have been allocated dynamically. 
       We have references to these three blocks as head,   
       second and third 

       llist.head        second              third 
          |                |                  | 
          |                |                  | 
      +----+------+     +----+------+     +----+------+ 
      | 1  | null |     | 2  | null |     |  3 | null | 
      +----+------+     +----+------+     +----+------+ */

     llist.head.next = second; // Link first node with the second node 

     /*  Now next of the first Node refers to the second.  So they 
         both are linked. 

      llist.head        second              third 
         |                |                  | 
         |                |                  | 
     +----+------+     +----+------+     +----+------+ 
     | 1  |  o-------->| 2  | null |     |  3 | null | 
     +----+------+     +----+------+     +----+------+ */

     second.next = third; // Link second node with the third node 

     /*  Now next of the second Node refers to third.  So all three 
         nodes are linked. 

      llist.head        second              third 
         |                |                  | 
         |                |                  | 
     +----+------+     +----+------+     +----+------+ 
     | 1  |  o-------->| 2  |  o-------->|  3 | null | 
     +----+------+     +----+------+     +----+------+ */

【讨论】:

    【解决方案2】:
    1. 您似乎混淆了创建头部(new Node(1))、分配头部(= 运算符)和声明头部(Node head;)的想法。你的类定义只需要声明这个实例变量,构造函数处理这个对象的创建和赋值。

    2. Node 类的目的是包含值和指向下一个对象的链接。因此,没有理由将您的 LinkedList 类视为该列表中所有 Node 对象的容器,就像其他列表类型一样。

    3. next = null; 是不必要的,因为null 是实例变量的默认值。

    【讨论】:

    • 我对这一行感到困惑 llist.head = new Node(1);为什么要使用 LinkedList 对象?不就是所有Node对象的容器吗?
    • 在您的main() 方法中,您将new Node(1) 分配给实例变量head,因为这是您的LinkedList 需要引用的唯一节点。对其他节点的引用都保存在前一个节点中,所以LinkedList不需要知道它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 2018-01-22
    • 2011-08-28
    • 2013-02-18
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多