From 0137c2205cea6175a668917c1400825b638e5252 Mon Sep 17 00:00:00 2001 From: pomerama Date: Mon, 23 Oct 2023 16:51:59 +0200 Subject: [PATCH] done --- index.html | 134 ++++++++++++------------------ src/queue/queue-data-structure.js | 16 +++- src/queue/queue-dom.js | 30 +++++++ src/stack/stack-data-structure.js | 16 +++- 4 files changed, 114 insertions(+), 82 deletions(-) diff --git a/index.html b/index.html index 3fab1ac7..227e62cc 100644 --- a/index.html +++ b/index.html @@ -1,92 +1,66 @@ - - - DataStructures | Stacks & Queues - - - - -

Data Structures

+ + + DataStructures | Stacks & Queues + + + -
-
-
-

Stack

-
- - -
- - -
-
-
    -
    -
    + +

    Data Structures

    + +
    +
    +
    +

    Stack

    +
    + + +
    + + +
    +
    +
      +
      -
      +
      +
      -
      -

      Queue

      -
      - - -
      - - +
      +

      Queue

      +
      + + +
      + + -
      -
      -
        -
        -
        +
        +
        +
          +
          +
          + + + - - + + + - - - - + \ No newline at end of file diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..8151a45f 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -6,23 +6,37 @@ class Queue { canEnqueue() { // ... your code goes here + return this.queueControl.length < this.MAX_SIZE } isEmpty() { // ... your code goes here + return this.queueControl.length == 0; } enqueue(item) { // ... your code goes here + if (this.canEnqueue()) { + this.queueControl.push(item); + return this.queueControl; + } else { + throw new Error('QUEUE_OVERFLOW'); + } } dequeue() { // ... your code goes here + if (!this.isEmpty()) { + return this.queueControl.shift() + } else { + throw new Error('QUEUE_UNDERFLOW') + } } display() { // ... your code goes here - } + return this.queueControl; + } } // This is required to enable the automated tests, please ignore it. diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..c175a0ff 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -11,10 +11,28 @@ const queue = new Queue(); const clearQueueInput = () => { // ... your code goes here + queueInput.value = ''; }; const generateListQueue = () => { // ... your code goes here + warningTopQueue.style.display = 'none'; + warningBottomQueue.style.display = 'none'; + queueUL.innerHTML = ''; + let length = queue.display().length; + let size = queue.MAX_SIZE - length; + queue.display().forEach(item => { + let li = document.createElement('li'); + li.className = 'active'; + li.innerText = item; + queueUL.appendChild(li) + }) + for (let i = 0; i < size; i++) { + let li = document.createElement('li'); + li.className = 'inactive'; + li.innerText = ' '; + queueUL.appendChild(li) + } }; generateListQueue(); @@ -22,24 +40,36 @@ generateListQueue(); const generateWarningQueue = (type) => { if (type === 'underflow') { // ... your code goes here + warningBottomQueue.style.display = 'block'; + warningBottomQueue.innerText = type } else if (type === 'overflow') { // ... your code goes here + warningTopQueue.style.display = 'block'; + warningTopQueue.innerText = type; } }; const addToQueue = () => { try { + queue.enqueue(queueInput.value); + clearQueueInput(); + generateListQueue(); // ... your code goes here } catch (error) { // there was an overflow error, handle it + generateWarningQueue('overflow') } }; const removeFromQueue = () => { try { // ... your code goes here + queue.dequeue(); + clearQueueInput(); + generateListQueue(); } catch (error) { // there was an underflow error, handle it + generateWarningQueue('underflow') } }; diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..b760bfc1 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -6,23 +6,37 @@ class Stack { canPush() { // ... your code goes here + return this.stackControl.length < this.MAX_SIZE } isEmpty() { // ... your code goes here + return this.stackControl.length == 0; } push(item) { // ... your code goes here + if (this.canPush()) { + this.stackControl.push(item); + return this.stackControl; + } else { + throw new Error('STACK_OVERFLOW'); + } } pop() { // ... your code goes here + if (!this.isEmpty()) { + return this.stackControl.pop(); + } else { + throw new Error('STACK_UNDERFLOW'); + } } display() { // ... your code goes here - } + return this.stackControl + } } // This is required to enable the automated tests, please ignore it.