棧方法:後進先出(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>