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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 14:11:40
字體:
來源:轉載
供稿:網友
//循環隊列
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;
}
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 淮北市| 平凉市| 桦南县| 三明市| 阜阳市| 扎兰屯市| 弥勒县| 长沙市| 原平市| 临朐县| 浠水县| 英山县| 内江市| 胶州市| 宜都市| 富裕县| 新绛县| 堆龙德庆县| 韶山市| 南宁市| 辽中县| 开平市| 湛江市| 新乡市| 开江县| 清河县| 乌拉特后旗| 海原县| 汾阳市| 张家港市| 凤凰县| 南阳市| 阳高县| 江门市| 疏附县| 周宁县| 林州市| 濉溪县| 贵定县| 贵定县| 方城县|