Array栈方法和队列方法的特点说明


<!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>
    <title>栈方法</title>
    <script type="text/javascript">
        //栈是一种LIFO(last in first outside)后进先出的数据结构
       function basicPushOrPop(){
         var colors=["red","green","blue"];
         var count=colors.push("pink");//push()方法可以接收任意数量的参数,并把它们逐个添加到数据的末尾,并返回修改后数组的长度
         alert(count);

         var temp=colors.pop();//pop()方法则从数组末尾移除最后一项,减少数组的length值,然后返回移除的项
         alert(temp);
       }

       //队列数据结构的访问规则是FIFO(first in first outside)
       function basicShift(){
          var colors=new Array();
          var count=colors.push("red","blue");//推入两项
          alert(count);

          var temp=colors.shift();//取的队列中第一项的数据,并移除
          alert("现在数组长度为:"+colors.length+"--移除的项为:"+temp);

          var newcount=colors.unshift("green","black");//unshift方法表示在队列前端添加任意个任意类型的值,并返回新的数组长度
          alert("现在数组长度为:"+newcount);//ie unshift方法总是返回undefined
       }
    </script>
</head>
<body>
  <input type="button" value="栈方法" />
  <input type="button" value="队列方法" />
</body>
</html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdyssw.html