数据结构之 JavaScript实现栈
2017-03-18 13:24
351 查看
栈是一种先入先出的数据结构,主要操作包括出栈、入栈,出栈、入栈操作都是在对尾实现的,如下图:
JavaScript代码实现
function Stack(array) {
if (array instanceof Array) {
this.dataArray = array;
}else{
throw "'"+array+"' is not an Array";
}
};
Stack.prototype = function(){
Push:function(element){//进栈
this.dataArray.push(element);
},
Pop:function(){//出栈
return this.dataArray.pop();
},
IsEmpty:function(){//是否为空
return this.dataArray.length ==0;
},
Peek:function(){
},
Length:function(){//判断栈内元素个数
return this.dataArray.length;
},
Clear:function(){//清空栈
this.dataArray = new Array();
}
}
JavaScript代码实现
function Stack(array) {
if (array instanceof Array) {
this.dataArray = array;
}else{
throw "'"+array+"' is not an Array";
}
};
Stack.prototype = function(){
Push:function(element){//进栈
this.dataArray.push(element);
},
Pop:function(){//出栈
return this.dataArray.pop();
},
IsEmpty:function(){//是否为空
return this.dataArray.length ==0;
},
Peek:function(){
},
Length:function(){//判断栈内元素个数
return this.dataArray.length;
},
Clear:function(){//清空栈
this.dataArray = new Array();
}
}
相关文章推荐
- javaScript实现的数据结构
- Javascript数据结构与算法--队列(顺序队列、优先队列、循环队列)的实现与用法
- [数据结构]javascript实现二叉查找树
- javascript实现字典数据结构
- 散列表的实现 -- 数据结构与算法的javascript描述 第八章
- 在SQL Server中巧妙存储树形结构数据及ASP.Net javaScript无刷新树形GridView的实现
- Javascript数据结构与算法--栈的实现与用法
- [置顶] 【javascript 数据结构与算法】javascript实现冒泡排序
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- 数据结构中关键路径算法的实现与应用
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- castor + xml schema 实现webservice中复杂结构数据传输
- 利用javascript实现可视化数据备份[原创]
- JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
- 利用JavaScript实现类似Delphi中数据敏感控件DBTextBox!
- castor + xml schema 实现webservice中复杂结构数据传输