[Algorithom] Stack Data Structure in JavaScript
JavaScript in Data Stack Structure
2023-09-14 08:59:16 时间
A stack is a collection of items that obeys the principle of "last in, first out". Like a stack of plates, we can only access the topmost plate at any time. We must remove that plate before we get down to other plates. This is useful for function calls, hence why it's called a "call stack" in JavaScript.
/** * Stack * * Last in First Out > LIFO */ function createStack() { const array = []; return { push(item) { array.push(item) }, pop() { return array.pop() }, peek() { return array[array.length - 1]; }, get length () { return array.length; }, isEmpty() { return array.length === 0; } } } const s = createStack(); s.push('one'); s.push('two'); s.push('three'); s.pop(); console.log(s.peek()) // two
相关文章
- Javascript之旅——第七站:说说js的调试
- JavaScript操作DOM元素
- [Javascript] Create Your First Iterator in JavaScript
- [Javascript] Keyword 'in' to check prop exists on Object
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [Javascript] Multiply Two Arrays over a Function in JavaScript
- [Algorithms] Refactor a Loop in JavaScript to Use Recursion
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain
- [Javascript] Compose multiple functions for new behavior in JavaScript
- [Javascript] Proper use of console.assert in JavaScript
- [Javascript] Hoisting in JavaScript
- [Javascript] Understanding the difference between .prototype and .__proto__ in JavaScript
- [Javascript] Run asynchronous functions in sequence using reduce
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [Javascript] Await a JavaScript Promise in an async Function with the await Operator
- [Algorithm] Linked List Data Structure in JavaScript
- [Javascript] Iterate Over Items with JavaScript's for-of Loop
- [Javascript] Javascript 'in' opreator
- 关于Fiori应用sandbox JavaScript的两个疑问
- 如何使用原生的 JavaScript 代码,触发 SAP UI5 按钮控件的点击事件处理函数
- JavaScript 引擎和 Just-in-Time 编译概念,Hot Function 的简单介绍