xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

数据结构-栈 All In One

数据结构-栈 All In One

堆栈(Stack)又称为栈或堆叠,是计算机科学中的一种抽象资料类型,只允许在有序的线性资料集合的一端(称为堆栈顶端,英语:top)进行加入数据(英语:push)和移除数据(英语:pop)的运算。

因而按照后进先出(LIFO, Last In First Out)的原理运作,堆栈常用一维数组链表来实现。
常与另一种有序的线性资料集合队列相提并论。

入栈/出栈
先进后出

function Stack (size) {
  this.size = size;
  this.arr = [];
  this.push = function(value) {
    if(this.arr.length < this.size) {
       this.arr.push(value)
    } else {
      console.log(`❌ stack overflow!`)
    }
  }
  this.pop = function() {
    if(this.arr.length > 0) {
       this.arr.pop()
    }
  }
}


const stack = new Stack(3);

stack.push(1);
stack.push(2);
stack.push(3);

stack.push(4);
// ❌ stack overflow!

图解算法数据结构 All In One

https://www.cnblogs.com/xgqfrms/p/16366896.html

refs

https://zh.wikipedia.org/wiki/堆栈

https://leetcode.cn/leetbook/detail/queue-stack/



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-06-20 23:09  xgqfrms  阅读(13)  评论(1编辑  收藏  举报