Sha256: 76e6f0e69213254212375db2e7bfb657119e50f12a0fa4b8ad896e5c62c526e7

Contents?: true

Size: 1.97 KB

Versions: 37

Compression:

Stored size: 1.97 KB

Contents

function Element (value, next) {
  if (!(this instanceof Element)) {
    throw new Error('Element is a constructor.');
  }

  if (value === undefined) {
    throw new Error('Value required.');
  }

  if (next !== undefined && !(next instanceof Element)) {
    throw new Error('A Element instance as next value is required.');
  }

  this.value = value;
  this.next = next;
};

function List () {};

List.prototype.push = function (value) {
  if (value === undefined) {
    throw new Error('Argument required.');
  }

  var newEl = (value instanceof Element)
    ? value
    : new Element(value);

  if (!this.head) {
    this.head = newEl;
    return;
  }

  var lastEl = this.head;
  while (lastEl.next) {
    lastEl = lastEl.next;
  }

  lastEl.next = newEl;
};

List.prototype.unshift = function (value) {
  if (value === undefined) {
    throw new Error('Argument required.');
  }

  var newEl = (value instanceof Element)
    ? value
    : new Element(value);

  newEl.next = this.head;
  this.head = newEl;
};

List.prototype.shift = function () {
  if (!this.head) {
    return;
  }

  this.head = this.head.next;
};

List.prototype.pop = function () {
  if (!this.head) {
    return;
  }

  var penultEl, lastEl = this.head;
  while (lastEl.next) {
    penultEl = lastEl;
    lastEl = lastEl.next;
  }

  if (!penultEl) {
    this.head = undefined;
  }
  else {
    penultEl.next = undefined;
  }
};

List.prototype.reverse = function () {
  if (!this.head) {
    return;
  }

  var current, previous;
  while (this.head) {
    current = this.head;
    this.shift();
    current.next = previous;
    previous = current;
  }

  this.head = previous;
};

List.prototype.toArray = function () {
  var array = [];

  while (this.head) {
    array.push(this.head.value);
    this.shift();
  }

  return array;
};

List.fromArray = function (array) {
  var list = new List();
  array.forEach(function (item) {
    list.push(new Element(item));
  });

  return list;
};

exports.List = List;
exports.Element = Element;

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
trackler-2.2.1.22 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.21 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.20 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.19 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.18 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.17 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.16 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.15 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.14 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.13 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.12 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.11 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.10 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.9 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.8 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.7 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.6 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.5 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.4 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.3 tracks/javascript/exercises/simple-linked-list/example.js