JS achieve a one-way linked list, doubly linked list, circular list

https://cloud.tencent.com/developer/article/1114246

 

List ordered set of storage elements, and arrays but the difference is that the list of elements stored in memory is not continuous. Each list element contains itself a node storage element and a reference point to the next element. It looks like this:

  Compared to traditional arrays, linked lists of additions and deletions when a benefit is no need to move other elements, as long as the change pointer to it. But the downside is that if you want to access the elements of the list, you need to start from scratch loop iteration to the element you want.

function LinkedList() {

    // Node辅助类,表示要加入列表的项,element是即将添加到列表的值,next是指向列表中下一个节点项的指针 let Node = function (element) { this.element = element this.next = null } let length = 0 let head = null // 向链表尾部追加元素 this.append = function (element) { let node = new Node(element) let current if (head === null) { // 列表中第一个节点 head = node } else { current = head while (current.next) { current = current.next // 找到最后一项,是null } current.next = node // 给最后一项赋值 } length++ // 更新列表的长度 } // 从链表中移除指定位置元素 this.removeAt = function (position) { if (position > -1 && position < length) { // 值没有越界 let current = head let previous, index = 0 if (position === 0) { // 移除第一项 head = current.next } else { while (index++ < position) { previous = current current = current.next } previous.next = current.next // 将previous与current的下一项连接起来,跳过current,从而移除 } length-- // 更新列表的长度 return current.element } else { return null } } // 在链表任意位置插入一个元素 this.insert = function (position, element) { if (position >= 0 && position <= length) { // 检查越界值 let node = new Node(element), current = head, previous, index = 0 if (position === 0) { // 在第一个位置添加 node.next = current head = node } else { while (index++ < position) { previous = current current = current.next } node.next = current // 在previous与current的下一项之间插入node previous.next = node } length++ return true } else { return false } } // 把链表内的值转换成一个字符串 this.toString = function () { let current = head, string = '' while (current) { string += current.element + ' ' current = current.next } return string } // 在链表中查找元素并返回索引值 this.indexOf = function (element) { let current = head, index = 0 while (current) { if (element === current.element) { return index } index++ current = current.next } return -1 } // 从链表中移除指定元素 this.remove = function (element) { let index = this.indexOf(element) return this.removeAt(index) } this.isEmpty = function () { return length === 0 } this.size = function () { return length } this.getHead = function () { return head } } let list = new LinkedList() list.append(1) list.append(2) console.log(list.toString()) // 1 2 list.insert(0, 'hello') list.insert(1, 'world') console.log(list.toString()) // hello world 1 2 list.remove(1) list.remove(2) console.log(list.toString()) // hello world 
There is a single chain variant - circular linked list, and finally a pointer to the next element of an element, not a reference null, but a pointer to the first element, only need to modify the next point of the last head to.
 
Doubly linked list with a single difference is that, in a single linked list, only a chain of the next node linked nodes in a doubly linked list, the link is bi-directional: the next element in a chain, the chain forward by one element to another .
  Doubly linked list provides a list of two iterative methods: from start to finish, or the vice versa. In a single linked list, if we miss looking element in the iteration list, you need to back to the beginning of the list, re-iterates this is the advantage of two-way list.
  Similarly bidirectional linked list way linked list, need to control the next, prev two pointers, while the need to increase FooterQuote tail.
function DoubleLinkedList() {

        // Node辅助类,表示要加入列表的项,element是即将添加到列表的值,next是指向列表中下一个节点项的指针 let Node = function (element) { this.element = element this.prev = null // 新增一个向前的指针 this.next = null } let length = 0 let head = null let tail = null // 新增一个尾引用 // 向链表尾部追加元素 this.append = function (element) { let node = new Node(element) let current if (head === null) { // 列表中第一个节点 head = node // head与tail是同一个元素 tail = node } else { current = head while (current.next) { current = current.next // 找到最后一项,是null } current.next = node // 给最后一项赋值 node.prev = current tail = node // 修改尾引用 } length++ // 更新列表的长度 } // 从链表中移除指定位置元素 this.removeAt = function (position) { if (position > -1 && position < length) { // 值没有越界 let current = head let previous, index = 0 if (position === 0) { // 移除第一项 head = current.next if (length === 1) { // 只有一项 tail = null } else { head.prev = null } } else if (position === length - 1) { // 移除最后一项 current = tail tail = current.prev tail.next = null } else { while (index++ < position) { previous = current current = current.next } previous.next = current.next // 将previous与current的下一项连接起来,跳过current,从而移除 current.next.prev = previous } length-- // 更新列表的长度 return current.element } else { return null } } // 在链表任意位置插入一个元素 this.insert = function (position, element) { if (position >= 0 && position <= length) { // 检查越界值 let node = new Node(element), current = head, previous, index = 0 if (position === 0) { // 在第一个位置添加 if (!head) { head = node tail = node }else { node.next = current current.prev = node head = node } node.next = current head = node } else if (position === length) { current = tail current.next = node node.prev = current tail = node } else { while (index++ < position) { previous = current current = current.next } node.next = current // 在previous与current的下一项之间插入node previous.next = node current.prev = node node.prev = previous } length++ return true } else { return false } } // 把链表内的值转换成一个字符串 this.toString = function () { let current = head, string = '' while (current) { string += current.element + ' ' current = current.next } return string } // 在链表中查找元素并返回索引值 this.indexOf = function (element) { let current = head, index = 0 while (current) { if (element === current.element) { return index } index++ current = current.next } return -1 } // 从链表中移除指定元素 this.remove = function (element) { let index = this.indexOf(element) return this.removeAt(index) } this.isEmpty = function () { return length === 0 } this.size = function () { return length } this.getHead = function () { return head } } let list = new DoubleLinkedList() list.append(1) list.append(2) console.log(list.toString()) // 1 2 list.insert(0, 'hello') list.insert(1, 'world') console.log(list.toString()) // hello world 1 2 list.remove(1) list.remove(2) console.log(list.toString()) // hello world


 

Guess you like

Origin www.cnblogs.com/leftJS/p/11074346.html