[Algorithom] Stack Data Structure in JavaScript
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
[Algorithom] Stack Data Structure in JavaScript的更多相关文章
- [Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...
- [Algorithm] Linked List Data Structure in JavaScript
A linked list is a collection of items where each item points to the next one in the list. Because o ...
- [Algorithm] JavaScript Graph Data Structure
A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- UVa 11995:I Can Guess the Data Structure!(数据结构练习)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
随机推荐
- canvas 动画库 CreateJs 之 EaselJS(下篇)
本文来自网易云社区 作者:田亚楠 继承 对应原文:Inheritance 我们可以继承已有的「显示对象」,创建新的自定义类.实现方法有很多种,下面介绍其中之一. 举例:实现一个继承于 Containe ...
- 大数据学习——scala函数与方法
package com /** * Created by Administrator on 2019/4/8. */ object TestMap { def ttt(f: Int => Int ...
- Sonar - 部署常见问题及解决方法
1. sonarqube启动报错,查看es.log如下: 问题原因:sonarqube不能使用root用户启动 解决方法: (1)更改sonarqube所属用户权限 chown -R gold:gol ...
- Jmeter(四)_逻辑控制器详解
循环控制器: 指定其子节点运行的次数,可以使用具体的数值,也可以设置为变量 1:勾选永远:表示一直循环下去 2:如果同时设置了线程组的循环次数和循环控制器的循环次数,那循环控制器的子节点运行的次数为两 ...
- 【POJ 2585】Window Pains 拓扑排序
Description . . . and so on . . . Unfortunately, Boudreaux's computer is very unreliable and crashes ...
- Matplotlib中文乱码解决办法
Matplotlib中文乱码 解决方法如下: 首先设置源码文件编码方式为UTF-8 #-*- coding: utf-8 -*- 接着设置字体属性字典 font = {'family': 'SimHe ...
- BZOJ 4818 [Sdoi2017]序列计数 ——矩阵乘法
发现转移矩阵是一个循环矩阵. 然后循环矩阵乘以循环矩阵还是循环矩阵. 据说还有FFT并且更优的做法. 之后再看吧 #include <map> #include <cmath> ...
- 【NOIP2016】换教室(DP,期望)
题意: 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课程 ...
- 转 C++ 面向对象程序设计的基本特点
传送门 Miss it C++ 面向对象程序设计的基本特点 First: 抽象 面向对象方法中的抽象,是指对具体问题(对象)进行概括,抽出一类对象公共性质并加以描述的过程. 抽象的过程,也是对问题 ...
- linux sleep函数
应用程序: #include usleep(n) //n微秒 Sleep(n)//n毫秒 sleep(n)//n秒 驱动程序: #include mdelay(n) //milliseconds 其实 ...