Creating 3 Stacks With 1 Array in JavaScript

I’ve included diagrams to better visualize the process.

First we need to grab the offset of the stack within the values array.

To do this, we’ll multiply the stack number we want by the capacity of each stack.

For example, let’s find the index of the top item in stack 2 given that the _stackCapacity for each stack is 5.

The stacks contain the following elements:Stack 0: [1, 12]Stack 1: [18, 8, 2]Stack 2: [5, 9, 66, 15]Here is a visual representation of what the values array looks like:Step 1: Calculate the offset; find the index of the bottom item in stack twoAssuming our stacks start at zero (i.

e.

stack 0, stack 1, stack 2), we can find where the bottom of stack two starts in the values array by multiplying the stack we’re looking for, two, by the stack capacity, which is the value passed in at instantiation.

If our stack capacity is five, we know that the bottom element of stack two starts at index 10 in the values array.

index of bottom element in stack 2 = stack we’re looking for * capacity of each stack.

index of bottom element in stack 2 = 2 * 5 (found from _stackCapacity)index of bottom element in stack 2 = 10.

Step 2: Calculate the total number of values currently in stack twoWe already know how many values are in stack 2; they’re being kept in the sizes array.

So by grabbing the value of sizes[2] we know how many elements are in stack 2: 4Step 3: Add the offset with the total number of values in the stack, minus oneWe have to subtract one from the number of items in the stack, since our array starts at index zero.

When we add it all up we get:index of top element in stack 2 = offset + number of values in stack two— 1index of top element in stack 2 = 10 + 4 — 1index of top element in stack 2 = 13The code for this is as follows:PushThe push method pushes a value onto the top of the respective stack.

It takes in two arguments:The stack to push the value ontoThe valueThe first thing we have to do is check whether the stack is full.

If it is full, let’s console.

log the message Stack number ${stackNumber} is full.

If the stack isn’t full, increase the number of items in the stack, which is found in the sizes array.

3.

Then add the new value to the top of the stack.

We’ll use the indexOfTop method we just explained above to grab the top of the stack and add a value on top of it.

4.

If it’s successfully added, let’s console.

log a friendly message.

PopThis method pops the top item off of the respective stack number.

It takes in one argument:The stack to pop the value off ofWe must first check if the stack is empty using the isEmpty method.

If it is, we’ll return a console.

log a message.

If the stack isn’t empty, we’ll grab the index of the top element on the stack using the indexOfTop method and save it to a variable called topIndex.

Now let’s grab the value of that element.

We can do this with this.

values[topIndex] .

We’ll return this element, which is why we need to save it to a variable.

We also need to tell the values array that the value at this index no longer exists.

We’ll set this explicitly to zero (this could pose issues if your stack can take zero as a value, but for our sake we’ll assume the stack only accepts positive integers).

We must also decrement the size of the stack in the sizes array.

We can do this with this.

sizes[stackNumber]–.

Finally, let’s return the value we just popped off.

PeekThis method returns the top item off of the respective stack number.

It doesn’t alter the stack, it simply lets you view the element on the top.

It takes in one argument:The stack whose top value we want to peek atWe first have to check if the stack is empty.

We can use the isEmpty method to do so.

If it is empty, let’s console.

log a friendly message.

If the stack isn’t empty, we need to find the index for the element on top of the stack.

We can use the indexOfTop method to do so.

Finally, we can return the value found at that index with this.

values[topIndex] .

Putting It All TogetherThe final class looks like this:You’ve now created an array that represents three fixed-size stacks!.You can view the CodePen for this class here.

.

. More details

Leave a Reply