js:数据结构笔记3--栈

栈是一种特殊的列表,数据结构为LIFO;

定义:

function Stack() {
   this.dataStore = [];
   this.top = 0;
   this.push = push;
   this.pop = pop;
   this.peek = peek; 
   this.length = length;
   this.clear =clear; 
}
function push(elem) {
   this.dataStore[this.top++] = elem;
}
function pop() {
	var data = this.dataStore[--this.top];
	this.dataStore.length = this.top;
   return data;
}
function peek() {
   return this.dataStore[this.top--];
}
function length() {
   return this.top;
}
function clear() {
   this.top = 0;
}

  

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。