【问题标题】:LinkedList - Creating function that takes a value, creates a node, and add it to end of listLinkedList - 创建函数,该函数接受一个值,创建一个节点,并将其添加到列表末尾
【发布时间】:2019-04-30 02:31:55
【问题描述】:

这是我们用来构建 LL 的伪代码:

FUNCTION push(element)
CREATE node
SET node.value TO element
SET node.next TO null

IF the head node does not exist
THEN SET head to node
ELSE
SET current to head
SET current.next to node
END IF
END FUNCTION

伪代码本身也有错误。

以下是我尝试遵循的,但现在它正在指向 到推函数中的 { 右侧。

let head = null,
last,
node,
current,
value = element;

const linkedList = () => {
 let node = new Node(value);

 push(value) {
   if(head === null) {
       head = last = node;
   } else {
       last.next = node;
       last = node;
   }
  }
 }

Error : push(value) {

【问题讨论】:

  • 这是作业吗?
  • “伪代码本身也有错误”——但你不会告诉我们它是什么?
  • 什么是"`它指向{" mean?? Are you getting a compiler error? If so, tell us what it says. How can we help you otherwise? And which {`?有几个
  • 这是我正在做的练习
  • 添加了最后一行以阐明错误的位置

标签: javascript linked-list


【解决方案1】:

除了语法错误之外,您还有一个逻辑错误。您需要将头节点与任何其他节点区别对待,如果未设置,则将头属性设置为节点,但如果设置,则将下一个节点分配给最后一个节点。

如果你没有头节点,你可以不设置最后一个节点,因为实际上没有。

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

class LinkedList {
    constructor() {
        this.head = null;
        this.last = null;
    }
    push(value) {
        var node = new Node(value);
        if (!this.head) {
            this.head = node;
        } else {
            this.last.next = node;
        }
        this.last = node;
    }
}

var ll = new LinkedList;

ll.push(10);
console.log(ll);

ll.push(11);
console.log(ll);

ll.push(12);
console.log(ll);
.as-console-wrapper { max-height: 100% !important; top: 0; }

检查已插入的值。

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

class LinkedList {
    constructor() {
        this.head = null;
        this.last = null;
    }
    push(value) {
        var node = new Node(value),
            temp = this.head;
        
        while (temp) {
            if (temp.value === value) return;
            temp = temp.next;
        }

        if (!this.head) {
            this.head = node;
        } else {
            this.last.next = node;
        }
        this.last = node;
    }
}

var ll = new LinkedList;

ll.push(10);
console.log(ll);

ll.push(11);
console.log(ll);

ll.push(12);
console.log(ll);

ll.push(11);
console.log(ll);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 你有时间聊聊链表吗?
  • 我不知道我是否有能力创建一个聊天室或它是如何工作的
  • 这里问一下,如果cmet变多了,就会出现聊天机会。
  • 我需要创建一个未排序的单链表并从链表中删除所有重复项。示例输入:a -> c -> d -> d -> a 输出:a -> c -> d
  • 那么你需要将head节点迭代到最后(nextnull),如果没有找到则插入新节点。
【解决方案2】:

既然我们已经做到了。 这是一个可以在 ES6 中插入和删除节点的版本。没有最后一个节点,因为那会破坏它的美丽。 :)

class Node {
    constructor(value) {
        this.prev = null;
        this.next = null;
        this.value = value === undefined? null : value;
        this.list = null;
    }
    remove() {
        let prev = this.prev;
        let next = this.next;
        if (prev) {
            prev.next = next;
        }
        if (next) {
            next.prev = prev;
        }
        return this;
    }
    insert(node) {
        let prev = this.prev;
        if (prev) {
            prev.next = node;
            node.prev = prev;
        }
        this.prev = node;
        node.next = this;
    }
    append(node) {
        let next = this.next;
        if (next) {
            next.prev = node;
            node.next = next;
        }
        this.next = node;
        node.prev = this;
    }
}
class LinkedList {
    constructor() {
        this.head = null;
    }
    get last() {
        return this.list.length > 0 ? this.list[this.list.length] : null;
    }
    get values() {
        let node = this.head;
        let values = [];
        while(node) {
            values.push(node.value);
            node = node.next;
        };
        return values;
    }

    push(node) {
        node.prev = null;
        node.next = null;
        if (this.head) {
            this.head.prev = node;
            node.next = this.head;
        }
        this.head = node;
    }
    find(v) {
        let node = this.head;
        let fn = v;
        if (!(v instanceof Function)) fn = (el) => el.value === v;
        while(node && !fn(node)) {node = node.next;};
        return node;
    }
    forEach(fn) {
        let node = this.head;
        while(node) {fn(node); node = node.next;};
    }
}

可以这样使用:

let ll = new LinkedList();
let n1= new Node(1);
let n2= new Node(2);
let n3= new Node(3);
ll.push(n1);
ll.push(n2);
ll.find(1).append(n3);
console.log(ll.values);
ll.find(3).remove();
console.log(ll.values);
ll.find(2).append(n3);
console.log(ll.values);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多