如何解决javascript - 简单的链表遍历问题
我已经使用 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();
但是 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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。