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

前端技术 2023/09/03 JavaScript

栈方法:后进先出(last in first outside)

队列方法:先进先出(first in first outside)

具体应用如下:

复制代码 代码如下:

<!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=\"栈方法\" onclick=\"basicPushOrPop();\" />
  <input type=\"button\" value=\"队列方法\" onclick=\"basicShift();\" />
</body>
</html>

本文地址:https://www.stayed.cn/item/7435

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。