Sha256: 8641f3447094dc87de30da979599696f5db6cfe85da8b6a37022115a94bf2a88

Contents?: true

Size: 1.83 KB

Versions: 149

Compression:

Stored size: 1.83 KB

Contents

export class ElementValueRequiredException extends Error {}
export class ElementNextNotInstanceException extends Error {}

export class Element {
  constructor(value, next) {
    if (value === undefined) {
      throw new ElementValueRequiredException();
    }

    if (next !== undefined && !(next instanceof Element)) {
      throw new ElementNextNotInstanceException();
    }

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

export class List {
  static fromArray(array) {
    const list = new List();
    array.forEach(item => list.push(new Element(item)));

    return list;
  }

  push(value) {
    const newEl = (value instanceof Element)
      ? value
      : new Element(value);

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

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

    lastEl.next = newEl;
  }

  unshift(value) {
    const newEl = (value instanceof Element)
      ? value
      : new Element(value);

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

  shift() {
    if (!this.head) {
      return;
    }

    this.head = this.head.next;
  }

  pop() {
    if (!this.head) {
      return;
    }

    let penultEl;
    let lastEl = this.head;

    while (lastEl.next) {
      penultEl = lastEl;
      lastEl = lastEl.next;
    }

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

  reverse() {
    if (!this.head) {
      return;
    }

    let current;
    let previous;

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

    this.head = previous;
  }

  toArray() {
    const array = [];
    let current = this.head;

    while (current) {
      array.push(current.value);
      current = current.next;
    }

    return array;
  }
}

Version data entries

149 entries across 149 versions & 1 rubygems

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