循环队列的实现

循环队列,如题,就是对队列实现循环存储,队列的头尾指针相连,而循环队列在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容器类了,都忘得差不多了,该看看了...

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值