编辑
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.
(即可以定义为对象?)
我正在阅读课程中的一段,需要澄清这一点:
堆栈数据结构实现了一个 LIFO 优先级集合。它提供了这两个功能:push 和 pop。push 将一个元素添加到堆栈的顶部,并且 pop 删除最顶部的元素。
我被给了这个例子given a string, reverse it using a stack
代码
我举了一个例子:
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'));
当您定义堆栈或使用堆栈时,
array, linked lists, and as a class
您可以定义堆栈的唯一方法是什么?在工作环境中考虑和使用这一点的频率是多少?当有人正在构建应用程序并考虑使用它时,是否有一个简单的例子。
在我在顶部给出的段落中,它说它提供了 2 个功能,
push and pop
这自动意味着array
和array methods
对吗?除了定义堆栈的其他方法之外,这些是否会被视为额外功能?