我想用javascript编写一个栈程序.
我在<html>中加入<input type="text" name=text1>和<input type="text" name=text2>连个文本框和入栈,出栈清空三个按钮.
我想请问我如何实现在text1文本里面输入的数字入栈,而出栈得数字在text2里输出呢.
我是一个初学者,还望大家多多帮忙啊~~~
我在<html>中加入<input type="text" name=text1>和<input type="text" name=text2>连个文本框和入栈,出栈清空三个按钮.
我想请问我如何实现在text1文本里面输入的数字入栈,而出栈得数字在text2里输出呢.
我是一个初学者,还望大家多多帮忙啊~~~
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>hi,csdn</title>
<script>
function stack(){
this.data = new Array();
this.start = -1;
this.push = function(i){
this.data[this.data.length] = i;
}
this.pop = function(){
this.start++;
if(this.start==this.data.length){
this.start--;
return "out of stack";
}
return this.data[this.start];
}
this.clear = function(){
this.data = new Array();
this.start = -1;
}
}
var newstack=new stack();
</script>
</head><body>
<input id="a" type="text">
<input id="b" type="text">
<input type="button" value="in" onclick="newstack.push(a.value);a.value=''">
<input type="button" value="out" onclick="b.value=newstack.pop()">
<input type="button" value="clear" onclick="newstack.clear()">
</body></html>