LinkedList-创建函数,该函数接受一个值,创建一个节点,并将其添加到列表末尾

时间:2018-11-28 08:29:49

标签: javascript linked-list

这是我们从以下位置构建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;
   }
  }
 }

错误:push(value){<-----此大括号引发错误。意外的标记。

2 个答案:

答案 0 :(得分:2)

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

如果您没有头节点,那么您将无法设置最后一个节点,因为实际上没有人。

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; }

答案 1 :(得分:1)

因为我们在做。 这是一个可以在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);