当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript中利用数组实现的循环队列代码

Javascript
ExtJs 3.1 XmlTreeLoader Example Error
JQuery 获得绝对,相对位置的坐标方法
JQUERY操作JSON实例代码
基于Jquery的简单&简陋Tabs插件代码
jQuery插件 tabBox实现代码
JavaScript Event学习第十章 一些可替换的事件对
JavaScript Event学习第十一章 按键的检测
一段实现页面上的图片延时加载的js代码
我遇到的参数传递中 双引号单引号嵌套问题
Extjs学习过程中新手容易碰到的低级错误积累
JavaScript 输入框内容格式验证代码
JavaScript Event学习补遗 addEventSimple
jquery实现的提示浮层跟随鼠标移动
jQuery 添加/移除CSS类实现代码
jQuery 改变CSS样式基础代码
改善你的jQuery的25个步骤 千倍级效率提升
jquery 问答知识整理
Jquery iframe内部出滚动条
不同浏览器对回车提交表单的处理办法
Javascript 浏览器事件小结

Javascript 中的 javascript中利用数组实现的循环队列代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 50 ::
收藏到网摘: n/a

javascript中利用数组实现的循环队列代码,需要的朋友可以参考下。 //循环队列
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;
}
}
}