Array stack method and queue method characteristics

  • 2020-03-30 01:25:16
  • OfStack

The stack method: Last in first outside

Queue method: First in first outside.

The specific application is as follows:


<!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> The stack method </title>
    <script type="text/javascript">
        //A stack is a LIFO(last in first outside) last in first out data structure
       function basicPushOrPop(){
         var colors=["red","green","blue"];
         var count=colors.push("pink");//The push() method can take any number of arguments, add them to the end of the data one by one, and return the length of the modified array
         alert(count);

         var temp=colors.pop();//The pop() method removes the last item from the end of the array, reduces the length of the array, and returns the removed item
         alert(temp);
       }

       //The access rule for the queue data structure is FIFO(first in first outside)
       function basicShift(){
          var colors=new Array();
          var count=colors.push("red","blue");//Into two
          alert(count);

          var temp=colors.shift();//Takes the data of the first item in the queue and removes it
          alert(" Now the array length is: "+colors.length+"-- The removed item is: "+temp);

          var newcount=colors.unshift("green","black");//The unshift method represents adding arbitrary values of any type to the front of the queue and returning the new array length
          alert(" Now the array length is: "+newcount);//The ie unshift method always returns undefined
       }
    </script>
</head>
<body>
  <input type="button" value=" The stack method " onclick="basicPushOrPop();" />
  <input type="button" value=" Queue method " onclick="basicShift();" />
</body>
</html>


Related articles: