DEV Community

Steven Frasica
Steven Frasica

Posted on

Algorithms : Understanding Stacks

Stacks are a data type I was excited to learn about, given that I heard the term mentioned many times in my studies, but had not yet devoted much time to understanding. Once I did a couple of problems involving stacks, their implementation became much clearer.

Stack

Stacks are containers that hold individual pieces of data. There are comparisons to make between stacks and queues. One important difference is that in a stack, the first item that goes in, will be the last item that goes out of the stack. An example to understand stacks is a stack of plates. As mentioned, the first item in a stack is the last item out of a stack. When putting dishes away in a cabinet, the first dish is on the bottom and any subsequent dishes go on top of it, in a stack. Once there are a few plates on top of the first dish, it becomes increasingly difficult to remove the bottom or first dish. It is much easier to remove the top or last dish you placed on the stack. We will use an array and array methods to implement a stack and functionality.

Creating a Stack class

We are asked to create a stack data structure with functionality to push or add a record to the stack, pop or remove a record from the stack, and peek to "view" a record in the stack without removing that record.

class Stack {
  constructor() {
    this.data = [];
  }
/* using constructor, we can initialize an array when we create an instance of a stack. The array is a stack property called data.
*/
}

Now we implement the push method to add a record to the stack.

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

  push() {
  //use array method push and pass in an argument record to push or add into the stack.
  }
}
class Stack {
  constructor() {
    this.data = [];
  }

  push(record) {
   this.data.push(record);
  }
}

Next method is the pop method to remove the last or most recently added record to the stack.

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

  push(record) {
   this.data.push(record);
  }

  pop() {
   return this.data.pop();
  }
  // Include return keyword so we get the removed record from the stack.
}

Lastly, we will implement the peek method which allows us to check if there is a record left in the stack.

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

  push(record) {
   this.data.push(record);
  }

  pop() {
   return this.data.pop();
  }

  peek() {
   // return the last record in the stack without removing it
  }
}

Since we are using an array to build out our stack class, we can use bracket notation to read the last item in the array, which actually allows us to read the last record in the stack.

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

  push(record) {
   this.data.push(record);
  }

  pop() {
   return this.data.pop();
  }

  peek() {
   return this.data[this.data.length - 1];
  }
}

Resources

Stephen Grider's Algorithms and Data Structures Udemy Course

Interview Cake

Top comments (0)