/*
实现循环队列的基本操作(初始化、判断队空、判断队满、入队、出队)
*/
//在javascript中,可以使用数组来实现一个队列
function stack(){
this.datastore = new Array(); //初始化
this.isEmpty = isEmpty; //判断队空
this.isFull = isFull; //判断队满
this.add = add; //入队
this.remove = remove; //出队
this.count = 0;
function isEmpty(){
if(this.count == 0) return true;
return false;
}
function isFull(){
if(!isEmpty()) return true;
return false;
}
function add(value){
alert(this.count);
this.datastore[this.count ++] = value;
}
function remove(){
if(this.count <= 0) {
this.count = 0;
alert('当前队列为空,无法删除!');
return;
}
delete this.datastore[-- this.count ];
}
}
template
class SqQueue
{
Telem*elem;
int front,rear;
int len;
public:
SqQueue(int maxsz=100):len(maxsz)
{
elem=new Telem[len];
front=rear=0;
};
~SqQueue(){ delete[]elem;};
void clear(){front=rear=0;};
int leng(){return(rear-front+len)%len;};
bool empt(){return(front==rear);};
bool full(){return((rear+1)%len==front);};
bool enque(Telem&el);
Telem dlque();
Telem getf();
};
template
{
if((rear+1)%len==front)return(false);
else{
elem[rear]=el;
rear=(rear+1)%len;
return(true);
}
};
template
{
Telem el;
if(rear==front)return NULL;
else{
el=elem[front];
front=(front+1)%len;
return(el);
}
};
template
{
if(rear==front)return NULL;
else return(elem[front]);
};