美文网首页
算法学习--栈接口设计

算法学习--栈接口设计

作者: 清风徐云去 | 来源:发表于2019-09-25 14:18 被阅读0次
function stack(){
        this.arr = [];

        /**
         * [size description]元素数量
         * @return {[type]} [description]
         */
        stack.prototype.size = function(){
            return this.arr.length
        }
        /**
         * [isEmpty description]是否为空
         * @return {Boolean} [description]
         */
        stack.prototype.isEmpty = function(){
            let result = this.arr.length > 0 ? false : true;
            return result;
        }
        /**
         * [push description]入栈
         * @param  {[type]} e [description]
         * @return {[type]}   [description]
         */
        stack.prototype.push = function(e){
            this.arr.push(e);
            return this.arr;
        }
        /**
         * [pop description]出栈
         * @return {[type]} [description]
         */
        stack.prototype.pop = function(){
            this.arr.splice(this.arr.length - 1,1);
            return this.arr;
        }
        /**
         * [top description]获取栈顶元素
         * @return {[type]} [description]
         */
        stack.prototype.top = function(){
            return this.arr[this.arr.length - 1];
        }
    }
    
    let stackTest = new stack();
    console.log('isEmpty-- ',stackTest.isEmpty());
    stackTest.push(11);
    stackTest.push(22);
    stackTest.push(33);
    stackTest.push(44);
    console.log('push-- ',stackTest);
    console.log('size-- ',stackTest.size());
    console.log('top-- ',stackTest.top());
    console.log('pop-- ',stackTest.pop());
    console.log('top-- ',stackTest.top());
栈.png

相关文章

  • 算法学习--栈接口设计

  • TsingHuaDSA-栈和队列

    该文章为清华大学数据结构与算法设计MOOC课程读书笔记. 1. 栈 1.1 接口 LIFO后进先出 1.2 栈实现...

  • 栈-Stack

    栈-Stack 栈的接口设计 225. 用队列实现栈[https://leetcode-cn.com/proble...

  • 数据结构与算法之栈(四)

    一 目录 栈的介绍 栈的接口设计 栈的应用 - 浏览器前进后退 栈的使用 - 匹配有效括号 栈相关面试题 二 简介...

  • 算法学习--队列接口设计

  • 《全栈工程师修炼指南》学习笔记

    今天花时间学习了《全栈工程师修炼指南》的课程,全栈第一课就是将网络传输、接口设计的。 常规 HTTP 请求都是传统...

  • 算法设计 --- 最小栈

    算法描述 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) ...

  • 泛型

    泛型接口 实现栈需要能够返回最小值,且为O(1),也就意味着,栈中存储的元素必须是可以比较的,因此设计了这个接口,...

  • Lua C接口API 3

    Lua C 接口 API 接口比较多,主要都是围绕交互栈的操作,前面学习了如何简单操作交互栈,且相应的API比较...

  • 设计包含min函数的栈

    题目:设计包含min函数的栈 原创: 白话算法 要求:定义一个栈的数据结构,要求添加一个min函数,使他能够找到栈...

网友评论

      本文标题:算法学习--栈接口设计

      本文链接:https://www.haomeiwen.com/subject/wzxructx.html