Sha256: 1fba75359867612e56692fc846729805bd8b49c7905b78b1a94b71bc3b6328cd

Contents?: true

Size: 1.84 KB

Versions: 185

Compression:

Stored size: 1.84 KB

Contents

function Element(value) {
  return { 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

185 entries across 185 versions & 1 rubygems

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