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.2 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.1 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.1.0 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.6 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.5 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.4 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.3 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.2 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.1 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.2.0.0 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.55 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.54 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.53 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.52 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.51 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.50 tracks/javascript/exercises/simple-linked-list/example.js
trackler-2.1.0.49 tracks/javascript/exercises/simple-linked-list/example.js