Stack

Result

To Visualize: Copy code and test Click Here

Code


        let data = [];
        let currentSize = data.length;
        const max = 5;
  
        function push(newVal) {
          if (currentSize >= max) {
            console.log("Stack is full, you cannot add " + newVal);
            return;
          }
          data[currentSize] = newVal;
          currentSize += 1;
        }
  
        function pop() {
          if (currentSize > 0) {
            currentSize -= 1;
            data.length = currentSize;
          } else {
            console.log("Stack is already empty");
          }
        }

        push(1)
        push(2)
        push(3)
        pop()
        push(4)
        pop()
        pop()
        pop()
        pop()