【问题标题】:Circular linked list in javascriptjavascript中的循环链表
【发布时间】:2014-09-08 04:45:22
【问题描述】:

我正在尝试在 javascript 中实现循环链​​表。 我只想知道在javascript中实现这个的正确方法吗? 是否存在内存泄漏或无限对象创建?

function LinkedList() {
    this.Node = null;
    this.count = 0;
    this.head = null;
};

LinkedList.prototype.append = function (value) {
    // create new node
    var node = this.createNode(value);
    console.log(value);
    if (this.head == null) {
        this.Node = node;
        this.Node.next = null;
        this.head = this.Node;
    } else {
        var ptr = this.Node;
        while (ptr.next != null) {
            ptr = ptr.next;
        }
        ptr.next = node;
    }
    this.count++;
};

LinkedList.prototype.getSize = function () {
    console.log(this);
};

LinkedList.prototype.close = function () {
    var ptr = this.head;
    while (ptr.next != null) {
        ptr = ptr.next;
    }
    ptr.next = this.head;
};

LinkedList.prototype.createNode = function (value) {
    var node = {};
    node.value = value;
    node.next = null;
    return node;
};

var li = new LinkedList();
li.append(1);
li.append(2);
li.append(3);
li.append(4);
li.close();
li.getSize();

当我检查控制台时,它显示为 head 包含一个节点,并且该节点包含另一个节点等。 它是引用还是它们存储的实际对象?

【问题讨论】:

  • 如果您自己没有发现任何错误,您应该询问 codereview。
  • 是唯一的特性节点添加吗?
  • 我想使用循环链接列表。我不想要 remove , display 等功能。我只想知道对象的数量以及在javascript中实现循环链​​表的正确方法。
  • 将链表数据和方法保存在自己的节点中是否符合 javascript 标准?我是新手,但在 c++ 国际象棋编程中,我通常创建树创建器、树遍历器和节点类.每个节点都有独立的盒子创建填充数据并由树创建者添加到位。遍历由遍历器类管理,该类负责向前向后和分支移动。

标签: javascript linked-list circular-list


【解决方案1】:

您执行附加功能的方式对我来说似乎有点缺陷......因为在它执行之后,您的列表处于不一致的状态。您需要调用 close() 才能正确设置所有内容。我建议您可以更改 append() 函数以根据动态更新头部和尾部;因此你不需要调用 close()。

下面是我将如何实现 append() 方法(基本上只是稍微修改您的代码):

LinkedList.prototype.append = function(value) {
  var node = {
        value: value,
        next: this.head
    };//cricular node
  if (this.count === 0) {
    this.head = {};
    this.head.value = value;
    this.head.next = this.head;
    this.tail = this.head;
  } else {
    this.tail.next = node;
    this.tail = node;
  }
  this.count++;
};

【讨论】:

  • 我同意你的观点,但我不希望这个功能在我的代码中实现,因为我必须在最后构造链表。我不想在每次插入后更改链接。
【解决方案2】:
getCircular(start){
        let i=0,j=0;
        let secondHead = this.head;
        let pointer = this.head;
        let pointer2 = secondHead;
        while(i<start){
            let temp1 = pointer.next;
            pointer = temp1;
            i++;
        }
        this.head = pointer;
        this.tail.next = pointer2;
        while(j<start-1){
            let temp2 = pointer2.next;
            pointer2 = temp2;
            j++;
        }
        this.tail = pointer2;
        this.tail.next = null;
        return this;
    }

假设已经有一个列表: Kohli -> Dhoni -> Yuvi -> Sharma -> Dhawan , 你传递一个 2 的索引,那么结果将是这样的: Yuvi -> Sharma -> Dhawan -> Kohli -> Dhoni

那么你的调用应该是这样的:ll.getCircular(2); // 例如 ll 是一个对象

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多