循环队列,如题,就是对队列实现循环存储,队列的头尾指针相连,而循环队列在C语言中不能通过实现动态分配一维数组实现存储,可定义循环队列如下
typedef struct{
datatype *data;
int front;
int rear;
}sqQueue;//define a sqQueue
对循环队列的操作如下
void InitsqQueue(sqQueue &q){
q.data=(datatype *)malloc(SIZE_QUEUE*sizeof(datatype));
if(!q.data)
exit(0);
q.front=q.rear=0;
}//initialize a sqQueue
int sqQueueLength(sqQueue &q){
return (q.rear-q.front+SIZE_QUEUE)%SIZE_QUEUE;
}//return the length of the sqQueue
void InsertsqQueue(sqQueue &q,datatype e){
if((q.rear+1)%SIZE_QUEUE==q.front)
exit(0);
q.data[q.rear]=e;
q.rear=(q.rear+1)%SIZE_QUEUE;
}//insert a element in sqQueue ,when the Queue is full,exit
datatype deleteElementOfsqQueue(sqQueue &q){
if(q.front==q.rear)
exit(0);
e=q.data[q.rear];
q.front=(q.front+1)%SIZE_QUEUE;
}//delete an element of the sqQueue
基本操作类似,都是初始化,插入,删除,多了一个返回队列长度的函数
OK,今天到此结束,我去看看STL容器类了,都忘得差不多了,该看看了...