Skip to content

Instantly share code, notes, and snippets.

@ivanzusko
Last active October 25, 2020 14:00
Show Gist options
  • Save ivanzusko/aa3e78ed0bb2155d03eedf115311d19b to your computer and use it in GitHub Desktop.
Save ivanzusko/aa3e78ed0bb2155d03eedf115311d19b to your computer and use it in GitHub Desktop.

Revisions

  1. ivanzusko revised this gist Oct 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Queue.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * insertion - O(1)
    * removal - O(1)
    * searching - O(n)
    * access - O(n)
    * access - O(n)
    */

    function Queue() {
  2. ivanzusko revised this gist Oct 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Queue.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * insertion - O(1)
    * removal - O(1)
    * searching - O(n)
    * acess - O(n)
    * access - O(n)
    */

    function Queue() {
  3. ivanzusko created this gist Oct 25, 2020.
    86 changes: 86 additions & 0 deletions Queue.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    /* Queue */
    /**
    * insertion - O(1)
    * removal - O(1)
    * searching - O(n)
    * acess - O(n)
    */

    function Queue() {
    let size = 0;
    let first = null;
    let last = null;

    const Node = function(element){
    this.element = element;
    this.next = null;
    };

    this.enqueue = function (element) {
    const node = new Node(element);

    if (first === null) {
    first = node;
    last = node;
    } else {
    last.next = node;
    last = node;
    }

    size++;
    return this;
    }

    this.dequeue = function () {
    if (first === null) {
    return undefined;
    }

    const currentNode = first;
    first = currentNode.next;
    size--;

    if (size === 0) {
    first = null;
    last = null;
    }

    return currentNode.element;
    }

    this.isEmpty = function () {
    return size === 0;
    };

    this.toArray = function () {
    let array = [];
    let currentNode = first;

    while (currentNode !== null) {
    array.push(currentNode.element);
    currentNode = currentNode.next;
    }

    return array;
    }

    this.size = function (){
    return size;
    };

    this.first = function (){
    return first.element;
    };

    this.last = function () {
    return last.element;
    };

    this.firstNode = function (){
    return first;
    };

    this.lastNode = function () {
    return last;
    };
    }