1

EDIT

I would disagree that this is a duplicate, I am not asking for the best way to implement them, my questions below are asking for clarity on the how many ways are there to define them as well as asking for an example of when this would be thought of and used in a work setting. (i.e. can be defined as an object?)

I'm reading over a paragraph from a curriculum and need clarity on this:

The stack data structure implements a LIFO priority collection. It provides these two functions: push and pop. push adds an element to the top of the stack, and pop removes the top-most element.

I was given this example to given a string, reverse it using a stack

CODE

I gave this as an example:

let word = 'Bloc';

const reverseString = (str) => {

  let stack = [];

  for (let i of str) {

      stack.push(str[i]);

  }

  let reversed = '';

  for (let i of word) {

      reversed += stack.pop();

  }

  return reversed;

 }

console.log(reverseString('Bloc'));
  1. When you define a stack or use one, are an array, linked lists, and as a class the only ways you can define a stack?

  2. What is the frequency of when this is thought of and used in a work setting? and is there a simple example used when someone is building an application and thinks to use this.

  3. In the paragraph that I gave at the top, it says that it provides 2 functions, push and pop, that automatically means array and array methods right? Would these just be considered as extra features that are in addition to other means to define a stack?

mph85
  • 1,276
  • 4
  • 19
  • 39
  • Possible duplicate of [How do you implement a Stack and a Queue in JavaScript?](https://stackoverflow.com/questions/1590247/how-do-you-implement-a-stack-and-a-queue-in-javascript) – PM 77-1 Jun 05 '19 at 22:15
  • @PM77-1 please take a look at my edit, thanks – mph85 Jun 05 '19 at 22:29

2 Answers2

1

In a nutshell - stack is just a data structure that is utilized to solve specific problems in different languages, systems, libraries and etc.

  1. If we are talking specifically about javascript, then I would say that the ways you listed (array, linked lists, as a class) are all a good start to build a stack. However, they are all just data structures that you are trying to utilize to build another data structure. Any type of stackyou build, is built for a specific purpose and with different attributes. For example you can restrict how many elements are in your stack at any time (say - max amount of elements in your stack is 4, then when you try to add 5th element to your stack - it will reach stack overflow and not let you do it). You can also restrict what type of data goes in there. It all depends on what the purpose of that stack is. Arrays are a great way to visualize a stack and are a fantastic starting point to build one, since they already supply you methods of manipulating themselves like stack would (push and pop). But you don’t need to use arrays, you can use objects and find ways of working with those. The world is your oyster :)
  2. I personally don’t build stacks often at my workplace (I work as a web developer). However, it is an important data structure to know and understand. If you are going to be working in an environment that does a lot of data manipulation - you will be writing your own stack implementation once in a while as well. Some js libraries have their own stack implementations and if you are gonna be building those - well, then you should learn how it works! If you will be working with graphs and graph data - stack understanding is essential. So again - it’s all based on how heavy your workplace is on data aggregation and data processing.
  3. push and pop in js are for Arrays, that is correct. I would say that those 2 functions are an essential part of the stack, since they provide you with basic means of stack manipulation. However, those 2 methods are not all-defining and there can be much more that could be added.

Hope it helps. Good luck!

Artem K
  • 169
  • 1
  • 1
  • 9
1

There are a number of different ways that imitate a stack, but in the end they all come down to being a variation of a JavaScript object. JavaScript does not have structures like Stacks, Queues, or Linked Lists built in. Even an Array is a form of object, with some extra methods. Javascript's prototypal inheritance is what gives you the ability to imitate these structures and form object variations though. A simple example imitating a stack would be to use a constructor, which generates an object with its own methods:

function Stack() {
  return {
    items: [],
    push: function ( item ) {
      this.items[this.items.length] = item;
    }
    pop: function () {
      const topItem = this.items[this.items.length - 1];
      this.items.length = this.items.length - 1;
      return topItem;
    }
  }
}      

However, you would likely never actually imitate a stack like this in Javascript. Typically you'd just use the array methods like you did above. The idea of how the stack works is what's important.

For example, let's say you get a collection of data that includes a start time and an end time for when users were on your site, ordered by start time (e.g [[02:30, 03:00][02:45, 3:15][4:00, 5:30][5:00, 6:00]), and your job was display a chart of times when users were on your site, and not on your site. One solution to see the gaps and keep it in order would be to merge overlapping intervals (example becomes [[2:30, 3:15][4:00, 6:00]]).

You could do this do this with a "stack" by pushing the first item onto the stack, comparing that with the next interval in your collection. If it didn't overlap, you'd just push that item on to the stack but if it did overlap, you'd pop it off the stack, merge the two, and then push it back onto the stack to be compared when the next item.

So yes, in JavaScript, you would probably still use an array for this, but integrating the idea of a stack structure. If you start to work in other languages (typically static like Java or C++) those will have built-in Stacks, Queues, Linked Lists, etc. so it's important to understand how they work and the pros/cons/use cases for each of them.

marsheth
  • 822
  • 6
  • 9