我想用javascript编写一个栈程序.
我在<html>中加入<input type="text" name=text1>和<input type="text" name=text2>连个文本框和入栈,出栈清空三个按钮.
我想请问我如何实现在text1文本里面输入的数字入栈,而出栈得数字在text2里输出呢.
我是一个初学者,还望大家多多帮忙啊~~~

解决方案 »

  1.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <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>
      

  2.   

    请问如果不用this指针的话可以实现么?
      

  3.   

    不用this也行.但是复杂一点.更难写.