我已经使用javascript实现了一个单独的链表。请找到以下代码:
class Node {
constructor(data) {
this.data = data;
this.nextElement = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
isEmpty() {
return this.head === null;
}
insertAtHead(data) {
const tempNode = new Node(data);
tempNode.nextElement = this.head;
this.head = tempNode;
}
traverse() {
let current = this.head;
while (current.nextElement != null) {
console.log("node data", current.data);
current = current.nextElement;
}
}
insertAtTail(data) {
const tempNode = new Node(data);
if (this.head === null) {
this.head = tempNode;
return;
}
let currentNode = this.head;
while (currentNode.nextElement != null) {
currentNode = currentNode.nextElement;
}
currentNode.nextElement = tempNode;
}
}
const linkedList = new LinkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);
linkedList.traverse();
但是遍历方法从不打印最后一个元素。我在这里错过了什么?insertAtTail方法看起来是正确的。有人能告诉我吗。
感谢
traverse
在current.nextElement
为null
时停止循环——但此时current
仍然是具有data
的节点,只是它之后没有下一个节点。
相反,继续进行,直到节点本身是null
:
traverse() {
let current = this.head;
while (current) { // *** Only change is on this line
console.log("node data", current.data);
current = current.nextElement;
}
}
(
while (current) {
可能是
while (current !== null) {
如果您愿意。(
实例:
class Node {
constructor(data) {
this.data = data;
this.nextElement = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
isEmpty() {
return this.head === null;
}
insertAtHead(data) {
const tempNode = new Node(data);
tempNode.nextElement = this.head;
this.head = tempNode;
}
traverse() {
let current = this.head;
while (current) {
console.log("node data", current.data);
current = current.nextElement;
}
}
insertAtTail(data) {
const tempNode = new Node(data);
if (this.head === null) {
this.head = tempNode;
return;
}
let currentNode = this.head;
while (currentNode.nextElement != null) {
currentNode = currentNode.nextElement;
}
currentNode.nextElement = tempNode;
}
}
const linkedList = new LinkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);
linkedList.traverse();
在遍历中,您需要检查所有节点,直到下一个节点为空。
所以我刚从遍历中删除了.nextElement,它运行得很好
class Node {
constructor(data) {
this.data = data;
this.nextElement = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
isEmpty() {
return this.head === null;
}
insertAtHead(data) {
const tempNode = new Node(data);
tempNode.nextElement = this.head;
this.head = tempNode;
}
traverse() {
let current = this.head;
while (current) { // Here
console.log("node data", current.data);
current = current.nextElement;
}
}
insertAtTail(data) {
const tempNode = new Node(data);
if (this.head === null) {
this.head = tempNode;
return;
}
let currentNode = this.head;
while (currentNode.nextElement != null) {
currentNode = currentNode.nextElement;
}
currentNode.nextElement = tempNode;
}
}
const linkedList = new LinkedList();
linkedList.insertAtTail(12);
linkedList.insertAtTail(23);
linkedList.insertAtTail(25);
linkedList.traverse();