javascript中利用數(shù)組實(shí)現(xiàn)的循環(huán)隊(duì)列代碼
更新時(shí)間:2010年01月24日 17:31:26 作者:
javascript中利用數(shù)組實(shí)現(xiàn)的循環(huán)隊(duì)列代碼,需要的朋友可以參考下。
//循環(huán)隊(duì)列
function CircleQueue(size){
this.initQueue(size);
}
CircleQueue.prototype = {
//初始化隊(duì)列
initQueue : function(size){
this.size = size;
this.list = new Array();
this.capacity = size + 1;
this.head = 0;
this.tail = 0;
},
//壓入隊(duì)列
enterQueue : function(ele){
if(typeof ele == "undefined" || ele == ""){
return;
}
var pos = (this.tail + 1) % this.capacity;
if(pos == this.head){//判斷隊(duì)列是否已滿
return;
}else{
this.list[this.tail] = ele;
this.tail = pos;
}
},
//從隊(duì)列中取出頭部數(shù)據(jù)
delQueue : function(){
if(this.tail == this.head){ //判斷隊(duì)列是否為空
return;
}else{
var ele = this.list[this.head];
this.head = (this.head + 1) % this.capacity;
return ele;
}
},
//查詢隊(duì)列中是否存在此元素,存在返回下標(biāo),不存在返回-1
find : function(ele){
var pos = this.head;
while(pos != this.tail){
if(this.list[pos] == ele){
return pos;
}else{
pos = (pos + 1) % this.capacity;
}
}
return -1;
},
//返回隊(duì)列中的元素個(gè)數(shù)
queueSize : function(){
return (this.tail - this.head + this.capacity) % this.capacity;
},
//清空隊(duì)列
clearQueue : function(){
this.head = 0;
this.tail = 0;
},
//判斷隊(duì)列是否為空
isEmpty : function(){
if(this.head == this.tail){
return true;
}else{
return false;
}
}
}
function CircleQueue(size){
this.initQueue(size);
}
CircleQueue.prototype = {
//初始化隊(duì)列
initQueue : function(size){
this.size = size;
this.list = new Array();
this.capacity = size + 1;
this.head = 0;
this.tail = 0;
},
//壓入隊(duì)列
enterQueue : function(ele){
if(typeof ele == "undefined" || ele == ""){
return;
}
var pos = (this.tail + 1) % this.capacity;
if(pos == this.head){//判斷隊(duì)列是否已滿
return;
}else{
this.list[this.tail] = ele;
this.tail = pos;
}
},
//從隊(duì)列中取出頭部數(shù)據(jù)
delQueue : function(){
if(this.tail == this.head){ //判斷隊(duì)列是否為空
return;
}else{
var ele = this.list[this.head];
this.head = (this.head + 1) % this.capacity;
return ele;
}
},
//查詢隊(duì)列中是否存在此元素,存在返回下標(biāo),不存在返回-1
find : function(ele){
var pos = this.head;
while(pos != this.tail){
if(this.list[pos] == ele){
return pos;
}else{
pos = (pos + 1) % this.capacity;
}
}
return -1;
},
//返回隊(duì)列中的元素個(gè)數(shù)
queueSize : function(){
return (this.tail - this.head + this.capacity) % this.capacity;
},
//清空隊(duì)列
clearQueue : function(){
this.head = 0;
this.tail = 0;
},
//判斷隊(duì)列是否為空
isEmpty : function(){
if(this.head == this.tail){
return true;
}else{
return false;
}
}
}
相關(guān)文章
JS面向?qū)ο缶幊袒A(chǔ)篇(二) 封裝操作實(shí)例詳解
這篇文章主要介紹了JS面向?qū)ο缶幊谭庋b操作,結(jié)合實(shí)例形式詳細(xì)分析了JS面向?qū)ο蠓庋b操作的相關(guān)概念、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03基于RequireJS和JQuery的模塊化編程——常見(jiàn)問(wèn)題全面解析
下面小編就為大家?guī)?lái)一篇基于RequireJS和JQuery的模塊化編程——常見(jiàn)問(wèn)題全面解析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-04-04JS代碼屏蔽F12,右鍵,粘貼,復(fù)制,剪切,選中,操作實(shí)例
在本篇文章里小編給大家分享的是關(guān)于利用JS代碼屏蔽F12,右鍵,粘貼,復(fù)制,剪切,選中,操作,需要的朋友們學(xué)習(xí)下。2019-09-09JavaScript中兩個(gè)感嘆號(hào)的作用說(shuō)明
用兩個(gè)感嘆號(hào)的作用就在于,如果明確設(shè)置了o中flag的值(非null/undefined/0""/等值),自然test就會(huì)取跟o.flag一樣的值;如果沒(méi)有設(shè)置,test就會(huì)默認(rèn)為false,而不是null或undefined2011-12-12