Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我试图理解在JavaScript的单链表实现中传递引用(指针)的概念_Javascript_Algorithm_Linked List - Fatal编程技术网

我试图理解在JavaScript的单链表实现中传递引用(指针)的概念

我试图理解在JavaScript的单链表实现中传递引用(指针)的概念,javascript,algorithm,linked-list,Javascript,Algorithm,Linked List,我试图用JavaScript实现一个单链表,我很难想象传递引用的概念(以及指向何处的内容)。我的代码在下面,我已经跟进了一些问题或我的理解,请你检查一下,让我知道我的思路是否正确 class Node{ constructor(val){ this.val = val; this.next = null; } } class LinkedLists { constructor(){ this.head = null;

我试图用JavaScript实现一个单链表,我很难想象传递引用的概念(以及指向何处的内容)。我的代码在下面,我已经跟进了一些问题或我的理解,请你检查一下,让我知道我的思路是否正确

class Node{
    constructor(val){
        this.val = val;
        this.next = null;
    }
}

class LinkedLists {
    constructor(){
        this.head = null;
        this.length = 0;
        this.tail = null;
    }

    // methods
    addNodeStart(val){
        let node = new Node(val);
        if (this.length === 0){
            this.length++;
            this.head = node;
            this.tail = node;

        } else {
            node.next = this.head;
            this.head = node;
            this.length++;
        }
        return this;
    }

    addNodeEnd(val){
        let node = new Node(val);

        if (this.length === 0){
            this.length++;
            this.head = node;
            this.tail = node;

        } else {
            this.length++;
            this.tail.next = node;
            this.tail = node;
        }
    }
}
我的理解是:

  • 在将一个节点添加到开始(addNodeStart)时,我们首先使用给定的val创建一个新节点,并使用if语句检查长度是否为0,然后将尾部和头部都设置为新节点
  • 否则,如果长度>1,则首先指向
    节点。然后
    指向
    this.head
    (this.head指向设置为head的节点)。因此,
    this.head
    只是一个容器,其中包含对节点的引用??或者这是一个实际的节点?然后再次将this.head设置为node。因此,this.head只是一个变量名,例如,让x=5;这里x引用了5的内存位置

  • 那么,我是否认为this.head只是一个容器(变量),它保存一个节点的值?还是我错过了一些东西?

    我不知道你想要实现什么。但这可能有助于调试您的列表:

    您可以使用toArray方法,然后在控制台中检查列表

    LinkedLists.prototype.toArray = function() {
      let = element = this.head
      const array = []
      while(element) {
        array.push(element)
        element = element.next || null
      }
      return array
    }