线性表是具有相同数据类型的n个数据元素的有限序列。其特点是:
线性表主要分为两类:
// 顺序表(数组)
const sequentialList = [1, 2, 3, 4, 5];
// 链表
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
// 添加元素
add(data) {
const node = new Node(data);
if (!this.head) {
this.head = node;
} else {
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this.size++;
}
// 其他方法...
}
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 许可协议,转载请注明出处。