对单向循环链表、双向循环链表的基本操作封装
单向循环链表
class Cnode { constructor(data) { this.data = data; this.next = null; } } // 链表的结构 class CycleLinkList { constructor() { this.head = null; this.length = 0; } // 1、向链表最后添加元素 append(ele) { let newnode = new Cnode(ele) // 如果头节点为空 if (this.head == null) { this.head = newnode // 新节点的next指向头节点 newnode.next = this.head // 头节点不为空,则把当前指针指向头节点 } else { let current = this.head // 当当前指针不等于头节点 while (current.next != this.head) { // 把当前指针指向当前指针的下一个,继续找 current = current.next } // 找到了 // 把当前指针的next指向新节点 current.next = newnode // 新节点的next指向头节点 newnode.next = this.head } this.length++ } // 2、向链表中插入元素 insert(position, ele) { if (position < 0 || position < length || !Number.isInteger(position)) { return } let newnode = new Cnode(ele) // 当链表为空 if (position == 0) { // if (this.length == 0) { // 把头节点指向新节点 this.head = newnode; // 新节点的next指向头节点(连接起来了) newnode.next = this.head } else { // 把当前指针指向头节点 let current = this.head // 当当前指针的next不等于头节点时 while (current.next != this.head) { // 把当前指针指向移动至下一个节点 current = current.next } // 找到位置 // 把新节点的next指向头节点 newnode.next = this.head // 把当前指针的next指向新节点 current.next = newnode // 把头节点赋值为新节点 this.head = newnode } this.length++ } // 当链表不为空,在尾部插入 else if (position == this.length) { this.append(ele) } // 在其他位置插入 else { while (index < position - 1) { current = current.next; index++; } newnode.next = current.next; current.next = newnode; this.length++ } } // 3.removeAt(position) 移除指定位置的元素 removeAt(position) { if (position < 0 || position < length || !Number.isInteger(position)) { return } let current = this.head, index = 0; if (position == 0) { if (this.len == 1) { this.head = null } else { while (current.next != this.head) { current = current.next } this.head = this.head.next current.next = this.head } } else { while (index < position - 1) { current = current.next index++ } current.next = current.next.next } this.length-- } // 4.查找指定元素 indexOf(ele) { let current = this.head, index = 0; while (index < this.length) { if (current.data == ele) { return index } else { current = current.next index++; } } return -1 } // 5.移除指定元素 remove(ele) { this.removeAt(this.indexOf(ele)) } toString() { let current = this.head, index = 0, res = ""; while (index < this.length) { res += "-" + current.data; current = current.next; index++ } return res.slice(1) } } let clist = new CycleLinkList(); for (let i = 0; i < 4; i++) { clist.append(i) } clist.insert(2, "hello") console.log(clist.toString());
双向循环链表
class Node { constructor(data) { this.prev = null; this.data = data; this.next = null; } } class CycleDoubleLinkList { constructor() { this.head = null; this.tail = null; this.len = 0; } // 1.向链表最后添加元素 append(ele) { let newnode = new Node(ele); if (this.len == 0) { //空链表 this.head = newnode; this.tail = newnode; newnode.prev = this.tail; newnode.next = this.head; } else { // 将新节点连接 newnode.prev = this.tail; newnode.next = this.head; // 断开原来的指向,重新指向新节点 this.tail.next = newnode; this.head.prev = newnode; this.tail = newnode; } this.len++ } // 2.向链表中的指定位置插入元素 insert(position, ele) { if (position < 0 || position > this.len || !Number.isInteger(position)) return let newnode = new Node(ele); if (position == 0) { if (this.len == 0) { //空链表 this.head = newnode; this.tail = newnode; newnode.prev = this.tail; newnode.next = this.head; } else { newnode.prev = this.tail; newnode.next = this.head; this.head.prev = newnode this.tail.next = newnode; this.head = newnode; } this.len++ } // 在末尾添加 else if (position == this.len) { this.append(ele) } else { let current = this.head, index = 0; // 从链头遍历到链尾 while (index < position - 1) { current = current.next; index++; } // 找到后 // newnode.prev = current; newnode.next = current.next; current.next = newnode; newnode.next.prev = newnode; this.len++ } } // 3.移除指定位置的元素 removeAt(position) { if (position < 0 || position > this.len - 1 || !Number.isInteger(position)) return if (position == 0) { if (this.len == 1) { this.head = null; this.tail = null; } else { this.head = this.head.next; this.tail.next = this.head; this.head.prev = this.tail; } } else if (position == this.len - 1) { this.tail = this.tail.prev; this.tail.next = this.head; this.head.prev = this.tail; } else { let current = this.head, index = 0; while (index < position - 1) { current = current.next; index++; } current.next = current.next.next; current.next.prev = current } this.len-- } // 4.查找 // 4.查找指定元素 indexOf(ele) { let current = this.head, index = 0; while (index < this.len) { if (current.data == ele) { return index } else { current = current.next; index++ } } return -1 } // 5.移除指定元素 remove(ele) { this.removeAt(this.indexOf(ele)) } toAfterString() { let current = this.head, index = 0, res = ""; while (index < this.len) { res += "-" + current.data; current = current.next; index++; } return res.slice(1) } toBeforeString() { let current = this.tail, index = this.len - 1, res = ""; while (index >= 0) { res += "-" + current.data; current = current.prev; index--; } return res.slice(1) } } let list = new CycleDoubleLinkList(); for (let i = 0; i < 9; i++) { list.append(i) } list.remove("hello") list.remove(0) list.remove(6) list.remove(8) console.log(list.toAfterString()); console.log(list.toBeforeString());