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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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. ...

  4. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  5. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. 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 ...

  7. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!

    UVa11995  I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...

  9. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

随机推荐

  1. js各种继承方式和优缺点的介绍

    js各种继承方式和优缺点的介绍 作者: default 参考网址2 写在前面 本文讲解JavaScript各种继承方式和优缺点. 注意: 跟<JavaScript深入之创建对象>一样,更像 ...

  2. GridView的RowCommand事件中获取每行控件的值

    //获取当前行                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent; //获取 ...

  3. 你应该知道的.net平台下socket异步通讯(代码实例)

    1,首先添加两个windows窗体项目,一个作为服务端server,一个作为客户端Client 2,然后添加服务端代码,添加命名空间,界面上添加TextBox控件 using System.Net; ...

  4. Install Oracle 11G Release 2 (11.2) on Oracle Linux 7 (OEL7)

    Install Oracle 11G Release 2 (11.2) on Oracle Linux 7 (OEL7) This article presents how to install Or ...

  5. 快速排序-php代码实现

    <?php function quickSort(array &$a) { $n = count($a); quickSortInternally($a, 0, $n-1); } fun ...

  6. linux下 export只能设定临时变量

    今天在调用ABBYY API的时候,需要传递APPID和APPPASSWD给系统环境才能够执行相应的python调用代码. 设置之后,因为写代码自己关掉了terminal,后面直接运行报错,访问权限不 ...

  7. 2.启动ABP ASP.NET ZERO

    1.使用VS2017打开项目,等待自动还原程序包结束 2.生成项目,确保项目全部生成成功 3.生成数据库 (1).将项目“MyCompanyName.AbpZeroTemplate.EntityFra ...

  8. Android中动态改变控件的大小的一种方法

    在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...

  9. HDU——1020Encoding(水题,string过)

    Encoding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. 免安装版MySql安装与配置

    1:在MySql官网下载免安装版 http://downloads.mysql.com/archives/community/ 下载链接 http://downloads.mysql.com/arch ...