LinkedList-创建函数,它获取一个值,创建一个节点,并将其添加到列表的末尾



这是我们必须从构建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({<-----此花括号正在引发错误。意外的令牌。

除了语法错误,还有一个逻辑错误。您需要将head节点与任何其他节点区别对待,并将head属性设置为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; }

既然我们已经做到了。这里有一个版本,您可以在ES6中插入append和remove节点。没有最后一个节点会破坏它的美丽。:(

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

最新更新