Sha256: d2222d20d09614db45d5990a38d7d3b3338e679b2863a74cf3eab3fc2506fe50

Contents?: true

Size: 1.85 KB

Versions: 211

Compression:

Stored size: 1.85 KB

Contents

function Element(value) {
  return {value: value, next: null, prev: null}
}

export default class Deque {
  constructor() {
    this.size = 0;
    this.head = null;
    this.tail = null;
  }

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

    const value = this.head.value;
    if (this.head.next) {
      this.head = this.head.next;
      this.head.prev = null;
    } else {
      this.head = this.tail = null;
    }

    return value;
  }

  push(value) {
    if (this.head) {
      const newHead = new Element(value);
      newHead.next = this.head;
      this.head.prev = newHead;
      this.head = newHead;
    } else {
      this.head = new Element(value);
      this.tail = this.head;
    }
  }

  shift() {
    if (!this.tail) {
      return undefined;
    }

    const value = this.tail.value;
    if (this.tail.prev) {
      this.tail = this.tail.prev;
      this.tail.next = null;
    } else {
      this.head = this.tail = null;
    }

    return value;
  }

  unshift(value) {
    if (this.tail) {
      const newTail = new Element(value);
      newTail.prev = this.tail;
      this.tail.next = newTail;
      this.tail = newTail;
    } else {
      this.tail = new Element(value);
      this.head = this.tail;
    }
  }

  count() {
    let count = 0,
        element = this.head;

    while (this.head && element) {
      count++;
      element = element.next;
    }
    return count;
  }

  delete(value) {
    let element = this.head;
    while (element) {
      if (element.value === value) {
        if (element.next) {
          element.next.prev = element.prev;
        } else {
          this.tail = this.tail.prev;
        }
        if (element.prev) {
          element.prev.next = element.next;
        } else {
          this.head = this.head.next;
        }
        element = null;
      } else {
        element = element.next;
      }
    }
  }
}

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.0.8.51 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.50 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.49 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.48 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.47 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.46 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.45 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.44 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.43 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.42 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.41 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.40 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.39 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.38 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.37 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.36 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.35 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.34 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.33 tracks/ecmascript/exercises/linked-list/example.js
trackler-2.0.8.32 tracks/ecmascript/exercises/linked-list/example.js