棧方法:后進(jìn)先出(last in first outside)
隊列方法:先進(jìn)先出(first in first outside)
具體應(yīng)用如下:
復(fù)制代碼 代碼如下:
<!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)后進(jìn)先出的數(shù)據(jù)結(jié)構(gòu)
function basicPushOrPop(){
var colors=["red","green","blue"];
var count=colors.push("pink");//push()方法可以接收任意數(shù)量的參數(shù),并把它們逐個添加到數(shù)據(jù)的末尾,并返回修改后數(shù)組的長度
alert(count);
var temp=colors.pop();//pop()方法則從數(shù)組末尾移除最后一項,減少數(shù)組的length值,然后返回移除的項
alert(temp);
}
//隊列數(shù)據(jù)結(jié)構(gòu)的訪問規(guī)則是FIFO(first in first outside)
function basicShift(){
var colors=new Array();
var count=colors.push("red","blue");//推入兩項
alert(count);
var temp=colors.shift();//取的隊列中第一項的數(shù)據(jù),并移除
alert("現(xiàn)在數(shù)組長度為:"+colors.length+"--移除的項為:"+temp);
var newcount=colors.unshift("green","black");//unshift方法表示在隊列前端添加任意個任意類型的值,并返回新的數(shù)組長度
alert("現(xiàn)在數(shù)組長度為:"+newcount);//ie unshift方法總是返回undefined
}
</script>
</head>
<body>
<input type="button" value="棧方法" />
<input type="button" value="隊列方法" />
</body>
</html>
新聞熱點
疑難解答
圖片精選