DEV Community

Cover image for Stacks
Kenny
Kenny

Posted on

Stacks

Hi everyone! This is the end of week 05 of the boot camp. We've brushing up on mySql and also learning about MongoDB. Things are slowly clicking together for me at this point. Before I was just lost, on where to start, but now I feel a bit more comfortable with full-stack development. For this week We're gonna go over with Queues and stacks. This week were given a problem a break down on stakes and queues.

In stacks we can think of it like a stack of plates in a sink. Typically if we were to wash plates in the sink, normally we'd clean the plate that was stacked on top of the stack instead of the first plate on the bottom of stack. That is an example of last in, first out; for short (LIFO). Unless you prefer to wash the first plate at the bottom of the stack, then you're my kind of person! Stacks has two principal operations that are essentially array methods

Push: that adds elements at the end of the collection.
Pop: removes the last element of the item at the end of the collection.

Alt Text

In this problem we have to create a stack with pseudo-classical instantiation with the push pop methods, and avoid using native array methods/properties.

With doing so We can start by simply creating a Stack function, and within the function we can initialize storage to be an empty object and a size variable that indicate the size of the stack that'll soon to be used.

const Stack = function() {
 this.storage = {};
 this.size = 0;

Now that we have our stack storage we can give it the functions of push and pop.
Since our storage is an object, we can use bracket notation to manipulate the object.

Stack.prototype.push = function(value) {
 this.storage[this.size] = value;
 this.size++;

Since we're going to add elements into the object we're going to increase the size of the stack storage for a later function

Next we're going to make a pop function for the stack, in which we're going to test the size of the storage, and if there nothing in the stack, we can just return, otherwise we'll start by initialize a popped variable to store the storage element, and delete the actual storage element. Since we're popping an element out of the storage we'd have to decrease the size of our size counter and simply return the popped variable.

Stack.prototype.pop = function() {
  if (this._size === 0) {
    return 'This stack is Empty';
  }
  let popped = this._storage[this._size - 1];
  delete this._storage[this._size - 1];
  this._size--;
  return popped;
};

Lastly we can also add size function for the stack aswell to tell us how many elements that are in the stack.

Stack.prototype.size = function() {
  return this.size;

All we had to do is just return the size counter that we've implementing in our stack constructor!

In conclusion stacks are relatively simple when it comes to adding and removing data, a little take away is that stacks are Last in, First Out (LIFO)
Thank you for taking the time to skim over my little post

Top comments (0)