标签:
需要滚动显示最多一定数量的信息,于弄了个这个
var LeesCircleQueue=function(size)
{
// 队列数组
var _queue=[];
// 队首索引
var _front=0;
// 队尾索引
var _rear=0;
// 元素个数
var _length=0;
// 队列的内存大小,但实际可用大小为_capacity-1
var _capacity = size;
// 添加元素
this.Push=function(item)
{
var nIndex = GetNextRearIndex();
_queue[nIndex] = item;
if (_length < _capacity)
_length++;
}
// 移除头部元素
this.Pop=function()
{
if (_length > 0)
{
_length--;
_front++;
if (_front == _capacity)
_front = 0;
}
if (_length == 0)
{
_front = _rear = 0;
}
}
// 获取全部内容
this.GetAllItem=function()
{
var tmp = [];
for (var i = 0; i < _length; i++)
{
tmp[i] = _queue[(_front + i) % _capacity];
}
return tmp;
}
// 获取下一个索引
function GetNextRearIndex()
{
if (_length == _capacity)//full
{
_rear = (_rear + 1) % _capacity;
_front = (_rear + 1) % _capacity;
}
else
{
if (_length > 0)
_rear = (_rear + 1) % _capacity;
else
{
_front = _rear = 0;
}
}
return _rear;
}
};
标签:
原文地址:http://www.cnblogs.com/xhzxlqt/p/4856545.html