国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript中利用數組實現的循環隊列代碼

2019-11-21 00:47:22
字體:
來源:轉載
供稿:網友
//循環隊列
function CircleQueue(size){
this.initQueue(size);
}
CircleQueue.prototype = {
//初始化隊列
initQueue : function(size){
this.size = size;
this.list = new Array();
this.capacity = size + 1;
this.head = 0;
this.tail = 0;
},
//壓入隊列
enterQueue : function(ele){
if(typeof ele == "undefined" || ele == ""){
return;
}
var pos = (this.tail + 1) % this.capacity;
if(pos == this.head){//判斷隊列是否已滿
return;
}else{
this.list[this.tail] = ele;
this.tail = pos;
}
},
//從隊列中取出頭部數據
delQueue : function(){
if(this.tail == this.head){ //判斷隊列是否為空
return;
}else{
var ele = this.list[this.head];
this.head = (this.head + 1) % this.capacity;
return ele;
}
},
//查詢隊列中是否存在此元素,存在返回下標,不存在返回-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;
},
//返回隊列中的元素個數
queueSize : function(){
return (this.tail - this.head + this.capacity) % this.capacity;
},
//清空隊列
clearQueue : function(){
this.head = 0;
this.tail = 0;
},
//判斷隊列是否為空
isEmpty : function(){
if(this.head == this.tail){
return true;
}else{
return false;
}
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仙居县| 安陆市| 威海市| 颍上县| 南阳市| 繁昌县| 曲周县| 师宗县| 广宗县| 扎赉特旗| 三明市| 台山市| 德格县| 南木林县| 大邑县| 渝北区| 长泰县| 霸州市| 永宁县| 沅江市| 满城县| 乐陵市| 敦煌市| 四平市| 马公市| 临邑县| 全南县| 韶山市| 米林县| 小金县| 江门市| 鄂托克前旗| 额尔古纳市| 济南市| 渝中区| 观塘区| 平武县| 东安县| 左权县| 阿瓦提县| 荥阳市|