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'));
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?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.
In the paragraph that I gave at the top, it says that it provides 2 functions,
push and pop
, that automatically meansarray
andarray methods
right? Would these just be considered as extra features that are in addition to other means to define a stack?