Realization of single-link list and double-linked list structure _node.js in Node.js environment

Source: Internet
Author: User

JavaScript implementation of single linked list (LinkedList)
NPMJS Related library:
Complex-list, Smart-list, singly-linked-list
Programming Ideas:

    • The Add method is used to append the element to the end of the list, which is implemented by the Insert method;
    • Pay attention to the boundary condition processing of each function.

Own implementation:

Singlenode.js

(function () {
 "use strict";

 function Node (Element) {
  this.element = element;
  This.next = null;
 }

 Module.exports = Node;
}) ();

Linkedlist.js

(function () {"Use strict";

 var Node = require ("./lib/singlenode");
  function LinkedList () {this._head = new Node ("This are head node.");
 this._size = 0;
 } LinkedList.prototype.isEmpty = function () {return this._size = = 0;

 };
 LinkedList.prototype.size = function () {return this._size;

 };
 LinkedList.prototype.getHead = function () {return this._head;

 };
  LinkedList.prototype.display = function () {var currnode = This.gethead (). Next;
   while (Currnode) {console.log (currnode.element);
  Currnode = Currnode.next;

 }
 };
   LinkedList.prototype.remove = function (item) {if (item) {var Prenode = This.findpre (item);
   if (Prenode = null) return;
    if (Prenode.next!== null) {prenode.next = PreNode.next.next;
   this._size--;

 }
  }
 };
 LinkedList.prototype.add = function (item) {This.insert (item);

 };
  LinkedList.prototype.insert = function (newelement, item) {var newNode = new Node (newelement); var finder = Item?
 This.find (item): null; if (!finder) {var last = This.findlast ();
  Last.next = NewNode;
   } else{newnode.next = Finder.next;
  Finder.next = NewNode;
 } this._size++;

 }; /*********************** Utility functions ********************************/LinkedList.prototype.findLast =
  function () {var currnode = This.gethead ();
  while (currnode.next) {currnode = Currnode.next;
 return currnode;

 };
  LinkedList.prototype.findPre = function (item) {var currnode = This.gethead ();
  while (Currnode.next!== null && currNode.next.element!== Item) {currnode = Currnode.next;
 return currnode;

 };
  LinkedList.prototype.find = function (item) {if (item = NULL) return null;
  var currnode = This.gethead ();
  while (Currnode && currnode.element!== Item) {currnode = Currnode.next;
 return currnode;

 };
Module.exports = LinkedList;

 })();


JavaScript implementation of double-linked list (doublelinkedlist)
NPMJS Related library:
Complex-list, Smart-list
Programming Ideas:

    • The double linked list is a pointer to the forward direction, so the auxiliary function in single linked list is Findpre.
    • The reverse output method is added;
    • Attention to the treatment of boundary conditions.

Their own implementation
Doublenode.js

(function () {
 "use strict";

 function Node (Element) {
  this.element = element;
  This.next = null;
  this.previous = null;
 }

 Module.exports = Node;
}) ();

Doublelinkedlist.js

(function () {"Use strict";

 var Node = require ("./lib/doublenode");
  function Doublelinkedlist () {this._head = new Node ("This are head node.");
 this._size = 0;
 } DoubleLinkedList.prototype.getHead = function () {return this._head;

 };
 DoubleLinkedList.prototype.isEmpty = function () {return this._size = = 0;

 };
 DoubleLinkedList.prototype.size = function () {return this._size;

 };
  DoubleLinkedList.prototype.findLast = function () {var currnode = This.gethead ();
  while (currnode.next) {currnode = Currnode.next;
 return currnode;

 };
  DoubleLinkedList.prototype.add = function (item) {if (item = NULL) return null;
 This.insert (item);

 };
   DoubleLinkedList.prototype.remove = function (item) {if (item) {var node = This.find (item);
   if (node = null) return;
    if (Node.next = = null) {node.previous.next = null;
   node.previous = null;
    } else{node.previous.next = Node.next;
    node.next.previous = node.previous; Node.next = null;
   node.previous = null;
  } this._size--;

 }
 };
  DoubleLinkedList.prototype.find = function (item) {if (item = NULL) return null;
  var currnode = This.gethead ();
  while (Currnode && currnode.element!== Item) {currnode = Currnode.next;
 return currnode;

 };
  DoubleLinkedList.prototype.insert = function (newelement, item) {var newNode = new Node (newelement); var finder = Item?
  This.find (item): null;
   if (!finder) {var last = This.findlast ();
   Newnode.previous = Last;
  Last.next = NewNode;
   } else{newnode.next = Finder.next;
   newnode.previous = Finder;
   finder.next.previous = NewNode;
  Finder.next = NewNode;
 } this._size++;

 };
  DoubleLinkedList.prototype.dispReverse = function () {var currnode = This.findlast ();
   while (Currnode!= this.gethead ()) {Console.log (currnode.element);
  Currnode = currnode.previous;

 }
 };
  DoubleLinkedList.prototype.display = function () {var currnode = This.gethead (). Next; while (Currnode) {console.log (currnode.element);
  Currnode = Currnode.next;

 }
 };
Module.exports = doublelinkedlist;

 })();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.