Sha256: 8ed6bb26c675efb85dda9ed663b05b4c360b6db0bc208c6d614c0a4e79b01d74

Contents?: true

Size: 1.53 KB

Versions: 63

Compression:

Stored size: 1.53 KB

Contents

'use strict';

function Node(value, next, prev) {
  this.value = value;
  this.next = next || this;
  this.prev = prev || this;
}

function LinkedList() {
  this._front = null;
}

LinkedList.prototype.push = function (value) {
  if (this._front === null) {
    this._front = new Node(value);
  } else {
    var back = this._front.prev;
    var n = new Node(value, this._front, back);
    back.next = n;
    this._front.prev = n;
  }
};

LinkedList.prototype.unshift = function (value) {
  this.push(value);
  this._front = this._front.prev;
};

LinkedList.prototype.pop = function () {
  if (this._front === null) {return null;}
  this._front = this._front.prev;
  return this.shift();
};

LinkedList.prototype.shift = function () {
  var value = this._front.value;
  var front = this._front.next;
  var back = this._front.prev;
  if (front === this._front) {
    this._front = null;
  } else {
    front.prev = back;
    back.next = front;
    this._front = front;
  }
  return value;
};

LinkedList.prototype.count = function () {
  if (this._front === null) {
    return 0;
  } else if (this._front.next === this._front) {
    return 1;
  }
  this._front.next = this._front.next.next;
  return this.count() + 1;
};

LinkedList.prototype.delete = function (match) {
  if (this._front.next === this._front && this._front.value === match) {
    this._front = null;
  } else if (this._front.next.value === match) {
    this._front.next = this._front.next.next;
  } else {
    this._front = this._front.next;
    this.delete(match);
  }
};

module.exports = LinkedList;

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.179 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.178 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.177 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.176 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.175 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.174 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.173 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.172 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.171 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.170 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.169 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.167 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.166 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.165 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.164 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.163 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.162 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.161 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.160 tracks/javascript/exercises/linked-list/example.js