zoukankan      html  css  js  c++  java
  • javascript数据结构-栈

    github博客地址

    栈(stack)又名堆栈,它是一种运算受限的线性表。遵循后进先出原则,像垃圾桶似的。
    功能实现依然按照增删改查来进行,内部数据存储可以借用语言原生支持的数组。

    栈类

    function Stack(){
    	this.data = [];
    }
    

      

    添加数据

    数据添加到末尾

    push: function (element){
    	this.data.push(element);
    }
    

      

    删除数据

    从末尾删除

    pop: function (){
    	this.data.pop();
    }
    

      

    获取数据

    返回最后一个添加的

    peek: function (){
    	return this.data[this.size() - 1];
    }
    

      

    是否为空

    isEmpty: function (){
    	return this.data.length == 0;
    }
    

      

    清空数据

    clear: function (){
    	this.data= [];
    }
    

      

    数据长度

    size: function (){
    	return this.data.length;
    }
    

      

    辅助函数,打印数据

    print: function (){
    	console.log(this.data.toString());
    }
    

      

    完整代码

     1 function Stack(){
     2     this.data = [];
     3 }
     4 Stack.prototype = {
     5     push: function (element){
     6         this.data.push(element);
     7     },
     8     pop: function (){
     9         this.data.pop();
    10     },
    11     peek: function (){
    12         return this.data[this.data.length - 1];
    13     },
    14     isEmpty: function (){
    15         return this.data.length == 0;
    16     },
    17     clear: function (){
    18         this.data= [];
    19     },
    20     size: function (){
    21         return this.data.length;
    22     },
    23     print: function (){
    24         console.log(this.data.toString());
    25     }
    26 }
    View Code
  • 相关阅读:
    Uva 10779 collector's problem
    poj 2728 最优比率树(最小生成树问题)
    LA 3126 二分图匹配 最小路径覆盖
    poj 1149 最大流构图
    Step By Step(Java XML篇)
    Step By Step(Java 输入输出篇)
    Step By Step(Java 集合篇)
    Step By Step(Java 线程篇)
    Step By Step(Java 反射篇)
    Step By Step(Java 国际化篇)
  • 原文地址:https://www.cnblogs.com/donglegend/p/6043330.html
Copyright © 2011-2022 走看看