Implementation of a linked list. One-way linked list
Node class function node (element) {this.element = element; This.next = null;} LinkedList class function Llist () {this.head = new Node (' head '); This.find = find; This.insert = insert; this.findprevious = findprevious; This.remove = remove; This.display = display;} Find function Find (item) {Let Currnode = This.head; while (currnode.element!== item) {currnode = Currnode.next; } return Currnode;} Insert function Insert (newelement, item) {Let NewNode = new Node (newelement); Let current = This.find (item); Newnode.next = Current.next; Current.next = NewNode;} Show function display () {Let currnode = This.head; while (Currnode.next!== null) {currnode = Currnode.next; Console.log (currnode.element); }}//Check the next node function findprevious (item) {Let Currnode = This.head; while (Currnode.next!== null && currNode.next.element!== Item) {currnode = Currnode.next; } return Currnode;} Delete functionRemove (item) {Let PrevNode = this.findprevious (item); if (Prevnode.next!== null) {prevnode.next = PrevNode.next.next; }}
Two. Doubly linked list
function Node (element) {this.element = element; This.next = null; this.previous = null;} function Dlist () {this.head = new Node (' head '); This.find = find; This.insert = insert; This.display = display; This.remove = remove; This.findlast = FindLast; This.dispreverse = Dispreverse;} function Dispreverse () {Let currnode = This.head; Currnode = This.findlast (); while (Currnode!== null && currnode.element!== ' head ') {console.log (currnode.element); Currnode = currnode.previous; }}function FindLast () {Let currnode = This.head; while (Currnode.next!== null) {currnode = Currnode.next; } return Currnode;} function Remove (item) {Let Currnode = This.find (item); if (Currnode.next!== null) {currNode.previous.next = Currnode.next; currNode.next.previous = currnode.previous; Currnode.next = null; currnode.previous = null; }}function display () {Let Currnode = This.head while (Currnode.next!== null) {Console.log (currNode.next.element); Currnode = Currnode.next; }}function Find (item) {Let Currnode = This.head; while (currnode.element!== item) {currnode = Currnode.next; } return Currnode;} function Insert (newelement, item) {Let NewNode = new Node (newelement); Let Currnode = This.find (item); Newnode.next = Currnode.next; newnode.previous = Currnode; Currnode.next = NewNode;}
Three. Circular link list
// Node类function Node (element) { this.element = element; this.next = null;}// LinkedList类function CList () { this.head = new Node('head'); // 修改 this.head.next = this.head; this.find = find; this.insert = insert; this.findPrevious = findPrevious; this.remove = remove; this.display = display;}// 其他// ...
Practice one. Implement the Advance (n) method to move the current node forward by N nodes.
// 向前移动一位DList.prototype.goPrevious = function (thisNode) { let node1, node2, node3, node4; if (thisNode.previous.element !== 'head') { node1 = thisNode.previous.previous; node2 = thisNode.previous; node3 = thisNode; node4 = thisNode.next; // 位置调整 node1.next = node3; node3.previous = node1; node3.next = node2; node2.previous = node3; node2.next = node4; node4.previous = node2; }};DList.prototype.advance = function (n, item) { let currNode = this.find(item); while (n--) { this.goPrevious(currNode); }};// 示例let names = new DList();names.insert('Mazey', 'head');names.insert('Cherrie', 'Mazey');names.insert('John', 'Cherrie');names.insert('Luna', 'John');names.insert('Ada', 'Luna');names.display();console.log('---');names.advance(2, 'Luna');names.display(); // Mazey, Luna, Cherrie, John, Ada
Two. Implement the Back (n) method to move the current node backward by n nodes.
// 向前移动一位DList.prototype.goNext = function (thisNode) { let node1, node2, node3, node4; if (thisNode.next.element !== null) { node1 = thisNode.previous; node2 = thisNode; node3 = thisNode.next; node4 = thisNode.next.next; // 位置调整 node1.next = node3; node3.previous = node1; node3.next = node2; node2.previous = node3; node2.next = node4; node4.previous = node2; }};DList.prototype.back = function (n, item) { let currNode = this.find(item); while (n--) { this.goNext(currNode); }};// 示例let names = new DList();names.insert('Mazey', 'head');names.insert('Cherrie', 'Mazey');names.insert('John', 'Cherrie');names.insert('Luna', 'John');names.insert('Ada', 'Luna');names.display();console.log('---');names.back(2, 'Cherrie');names.display(); // Mazey, John, Luna, Cherrie, Ada
JavaScript data structures and algorithms-chain list Exercises