Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js doubly linked list

// Doubly linked list

function Node(val = null) {
  this.val = val;
  this.after = null;
  this.before = null;
}

function LinkedList() {
  this.head = new Node();
}

LinkedList.prototype.push = function (val) {
  if (this.head.val == null) return (this.head.val = val);
  const node = new Node(val);

  let temp = this.head;
  while (temp.after) {
    temp = temp.after;
  }
  temp.after = node;
  node.before = temp;
};

LinkedList.prototype.unshift = function (val) {
  if (this.head.val == null) return (this.head.val = val);

  const node = new Node();
  node.val = this.head.val;
  this.head.val = val;
  node.before = this.head;
  if (this.head.after) node.after = this.head.after;
  this.head.after = node;
};

LinkedList.prototype.print = function () {
  if (!this.head.after)
    return console.log(this.head.val != null ? this.head.val : "Empty");

  let temp = this.head;
  while (temp) {
    console.log(temp.val);
    temp = temp.after;
  }
};

LinkedList.prototype.pop = function () {
  if (this.head.val == null) return;
  if (!this.head.after) return (this.head.val = null);

  let temp = this.head;
  while (temp.after.after) {
    temp = temp.after;
  }
  temp.after = null;
};

LinkedList.prototype.shift = function () {
  if (this.head.val == null) return;
  if (!this.head.after) return (this.head.val = null);

  this.head = this.head.after;
  this.head.before = null;
};

const list = new LinkedList();

list.push(0);
list.unshift(1);
list.unshift(2);
list.shift();

list.print();
Comment

PREVIOUS NEXT
Code Example
Javascript :: loop in react depending on number 
Javascript :: lodash convert object to array 
Javascript :: simple reactjs login form 
Javascript :: javascript cookie expire in 5 minutes 
Javascript :: redux devtools chrome 
Javascript :: how to parse using express without body parser 
Javascript :: how to auto refresh a div js 
Javascript :: scrollbar automatically scroll down as new divs are added reactjs 
Javascript :: radio button onclick jquery 
Javascript :: fileupload progress bar in axios 
Javascript :: css and js on flask 
Javascript :: $.post jquery beforesend loader 
Javascript :: how to print in jsp 
Javascript :: how to use media queries in emotion 
Javascript :: js read from json 
Javascript :: get first element by class name jquery 
Javascript :: javascript remove certain element from array 
Javascript :: copy to clipboard using javascript 
Javascript :: Html2Canvas screenshot and download 
Javascript :: how to handle all error of all router in express 
Javascript :: touppercase 
Javascript :: javascript remove object property 
Javascript :: font ligature vs code 
Javascript :: killall node windows 
Javascript :: alphabet javascript 
Javascript :: como ler um arquivo json com javascript 
Javascript :: contains whitespace js function 
Javascript :: mongodb update many 
Javascript :: Prevent Double Submit with JavaScript 
Javascript :: NodeJS get rootpath of our project 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =