function Stack(){
this.dataStore=[];
this.top=0;
this.push=push;
this.pop=pop;
this.peek=peek;
this.length=length;
this.clear=clear;
}
function pop(){
return this.dataStore[--this.top];
}
function push(element){
this.dataStore[this.top++]=element;
}
function peek(){
return this.dataStore[this.top-1];
}
function length(){
return this.top;
}
function clear(){
return this.top=0;
}
/**********************************
-------实例:回文判断----------
-思路:---------------------
----1.将字符创入栈;
2.将栈中的元素逐个弹出构成一个新的字符串;
3,新的字符串与旧的字符串比较,,相等不相等
**********************************/
function isHuiWen(str){
var s=new Stack();
for(var i=0;i<str.length;i++){
s.push(str[i]);
}
var rStr="";
while(s.length()>0){
rStr+=s.pop();
}
console.log(rStr);
if(str==rStr) return true;
else return false;
}
var str="hello";
if(isHuiWen(str)) console.log(str+"是回文");
else console.log(str+"不是回文");
var str2="aabbaa";
if(isHuiWen(str2)) console.log(str2+"是回文");
else console.log(str2+"不是回文");
第四章(栈)
最新推荐文章于 2020-12-19 17:39:47 发布