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)文章
Layui Table js 模擬選中checkbox的例子
今天小編就為大家分享一篇Layui Table js 模擬選中checkbox的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
JavaScript數(shù)學(xué)對象(Math)方法舉例詳解
這篇文章主要給大家介紹了關(guān)于JavaScript數(shù)學(xué)對象(Math)方法的相關(guān)資料,Math(數(shù)學(xué))對象的作用是執(zhí)行普通的算數(shù)任務(wù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
js簡單倒計(jì)時(shí)實(shí)現(xiàn)代碼
這篇文章主要介紹了js簡單倒計(jì)時(shí)實(shí)現(xiàn)代碼,涉及JavaScript時(shí)間與日期的相關(guān)運(yùn)算技巧,需要的朋友可以參考下2016-04-04
js實(shí)現(xiàn)簡單的左右兩邊固定廣告效果實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)簡單的左右兩邊固定廣告效果,實(shí)例分析了javascript實(shí)現(xiàn)固定廣告的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
JavaScript定時(shí)器實(shí)現(xiàn)限時(shí)秒殺功能
這篇文章主要為大家詳細(xì)介紹了JavaScript定時(shí)器實(shí)現(xiàn)限時(shí)秒殺功能,適合用于電商節(jié)日活動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
js判斷一個(gè)元素是否為另一個(gè)元素的子元素的代碼
用js判斷一個(gè)元素是否為另一個(gè)元素的子元素,再做一些效果的時(shí)候經(jīng)常用到,特別是和鼠標(biāo)事件相關(guān)的應(yīng)用中,比如一個(gè)浮層,在鼠標(biāo)操作浮層內(nèi)元素的時(shí)候浮層顯示,當(dāng)點(diǎn)擊浮層外的元素的時(shí)候隱藏浮層2012-03-03

