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.159 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.158 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.157 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.156 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.155 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.154 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.153 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.152 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.151 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.150 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.149 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.148 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.147 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.146 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.145 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.144 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.143 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.142 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.141 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.140 tracks/javascript/exercises/linked-list/example.js