js实现数据结构--链表(LinkedList)
原⽂:
概念:
链表存储有序的元素集合,但不同于数组,链表中的元素在内存中并不是连续放置的。每个元素由⼀个存储元素本⾝的节点和⼀个指向下⼀个元素的引⽤(也称指针或链接)组成。下图展⽰了链表的结构:
相对于传统的数组,链表的⼀个好处在于,添加或移除元素的时候不需要移动其他元素。然⽽,链表需要使⽤指针,因此实现链表时需要额外注意。数组的另⼀个细节是可以直接访问任何位置的任何元素,⽽要想访问链表中间的⼀个元素,需要从起点(表头)开始迭代列表直到到所需的元素。
普通链表:
// 链表节点
class Node {
constructor(element) {
this.element = element;
< = null;
}
}
// 链表
class LinkedList {
constructor() {
this.head = null;
this.length = 0; // length 同数组 length 与下标关系
}
// 追加元素
append(element) {
let node = new Node(element);
let current = null; // 指针?
if (this.head === null) {
this.head = node;
} else {
current = this.head;
while () {
current = ;
}
< = node;
}
this.length++;
}
// 任意位置插⼊元素
insert (position, element) {
if (position >= 0 && position <= this.length) {
let node = new Node(element);
let current = this.head;
let previous = null;
let index = 0;
if (position === 0) {
this.head = node;
} else {
while (index++ < position) {
previous = current;
current = ;
}
< = current;
< = node;
}
this.length++;
return true
}
return false
}
// 移除指定位置元素
removeAt(position) {
if (position > -1 && position < length) {
let current = this.head;
let previous = null;
let index = 0;
if (position === 0) {
this.head = ;
} else {
while(index++ < position) {
previous = current;
current = ;
}
< = ;
}
this.length--;
return current.element;
}
return null
}
// 寻元素下标
findIndex(element) {
while (current) {
if (element === current.element) {
return index + 1;
}
index++;
current = ;
}
return -1;
}
// 删除指定⽂档
remove(element) {
let index = this.findIndex(element);
return removeAt(index);
}
isEmpty() {
return !this.length;
}
size() {
return this.length;
}
// 输出字符串
toString() {
let current = this.head;
let string = '';
while (current) {
string += ` ${current.element}`;
current = ;
}
return string;
}
}
var ll = new LinkedList();
console.log(ll);
ll.append(2);
ll.append(6);
ll.append(24);
ll.append(152);
ll.insert(3, 18);
console.log(ll);
console.log(ll.findIndex(24));
双向链表:
class Node {
constructor(element) {
this.element = element;
this.prev = null;
< = null;
}
}
// 双向链表
class DoubleLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// 任意位置插⼊元素
insert(position, element) {
if (position >= 0 && position <= ehis.length) { let node = new Node(element);
let current = this.head;
let previous = null;
this.index = 0;
// ⾸位
if (position === 0) {
if (!head) {
this.head = node;
this.tail = node;
} else {
< = current;
this.head = node;
current.prev = node;
}
} else if (position === this.length) { // 末尾 current = this.tail;
< = node;
node.prev = current;
this.tail = node;
} else { // 中间
while(index++ < position) {
previous = current;
current = ;
}
< = current;
< = node;
current.prev = node;
node.prev = previous;
}
}
return false;
}
// 移除指定位置元素
removeAt(position) {
if (position > -1 && position < this.length) {
let current = this.head;
let previous = null;
let index = 0;
// ⾸位
if (position === 0) {
this.head =
this.head.prev = null
if (this.length === 1) {
this.tail = null
}
} else if (position === this.length - 1) { // 末位 this.tail = this.tail.prev
= null
} else { // 中位
while (index++ < position) {
previous = current
current =
}数组和链表
< =
}
this.length--;
return current.element;
} else {
return null;
}
}
// 其他⽅法
}
循环链表:
具体代码实现就不写了,下图是⽰意图
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论