logo
logo

Implement A Stack Using An Array

Lesson

Implement a stack using an array. You need to create a class called Stack, which should have the following functions:

  • Constructor: This function should initialize an empty array, which will be used to store the stack elements.

  • isEmpty(): This function should return true if the stack is empty, false otherwise.

  • push(element): This function should push the element into the stack.

  • pop(): This function should pop the topmost element from the stack and return it. If the stack is empty, it should return an underflow error message.

  • peek(): This function should return the topmost element from the stack without removing it. If the stack is empty, it should return an underflow error message.

  • printStack(): This function should print all the elements of the stack as a string.

Please implement the Stack class with the above-mentioned functions and test it by creating a new Stack object and calling its functions with sample inputs.

Sample Input:

let stack = new Stack();
console.log(stack.isEmpty()); // should return true
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.isEmpty()); // should return false
console.log(stack.printStack()); // should print "1 2 3"
console.log(stack.pop()); // should return 3
console.log(stack.peek()); // should return 2

Solution Code:

class Stack {
  constructor() {
    this.items = [];
  }

  isEmpty() {
    return this.items.length === 0;
  }

  push(element) {
    this.items.push(element);
  }

  pop() {
    if (this.isEmpty()) {
      return "underflow";
    }
    return this.items.pop();
  }

  peek() {
    if (this.isEmpty()) {
      return "underflow";
    }
    return this.items[this.items.length - 1];
  }

  printStack() {
    let str = "";
    for (let i = 0; i < this.items.length; i++) {
      str += this.items[i] + " ";
    }
    return str.trim();
  }
}

let stack = new Stack();
console.log(stack.isEmpty()); // should return true
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.isEmpty()); // should return false
console.log(stack.printStack()); // should print "1 2 3"
console.log(stack.pop()); // should return 3
console.log(stack.peek()); // should return 2

Solution Walkthrough

In this problem, we need to implement a stack using an array and write commonly asked functions for a stack such as is empty, push, pop, print stack, and peek.

To implement the stack, we start by building a class called "Stack" with an empty constructor that initializes an empty array called "items" as a property of the stack. We then implement the is empty function that returns true if the stack is empty and false otherwise by checking the length of the items array.

Next, we implement the push function that takes an element and adds it to the top of the stack by calling the push function on the items array property. We then test these functions by creating a new stack object and checking if it's empty and adding elements to the stack.

We then implement the print stack function that returns a string of all the elements in the stack with spaces in between them. We use a for loop to iterate through the items array property and append each element to a string with a space in between.

Next, we implement the pop function that removes and returns the topmost element in the stack by calling the pop function on the items array property. We handle the edge case of trying to pop from an empty stack by returning an "underflow" statement.

Finally, we implement the peek function that returns the topmost element in the stack without removing it. We do this by accessing the last element in the items array property.

Throughout the solution walkthrough, we also discuss the importance of accounting for edge cases and following best practices when writing code, such as using descriptive variable names and properly formatting code.

Overall, this solution demonstrates a basic implementation of a stack using an array and commonly asked stack functions.