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.139 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.138 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.137 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.136 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.135 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.134 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.133 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.132 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.131 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.130 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.129 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.128 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.127 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.126 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.125 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.124 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.123 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.122 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.121 tracks/javascript/exercises/linked-list/example.js
trackler-2.2.1.120 tracks/javascript/exercises/linked-list/example.js