循环队列——数据结构
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define INIT_SIZE 10
struct circleQueue{
int *base;
int front;
int rear;
};
void initQueue(circleQueue &Q){
Q.base=new int[INIT_SIZE];
Q.rear=Q.front=0;
}
bool queueEmpty(circleQueue Q){
return Q.front==Q.rear?1:0;
}
bool queueFull(circleQueue Q){
return (Q.rear+1)%(INIT_SIZE+1)==Q.front?1:0; //判断是否队满
}
void enQueue(circleQueue &Q,int e){
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%(INIT_SIZE+1);
}
int deQueue(circleQueue &Q){
int e;
e=Q.base[Q.front];
Q.front = (Q.front + 1) %(INIT_SIZE+1);
return e;
}
int getFront(circleQueue Q){
return Q.base[Q.front];
}
int main(){
circleQueue Q;
initQueue(Q);
int i;
for(i=1;i<=10;i++){
enQueue(Q,i);
}
for(i=1;i<=5;i++){
if(!queueEmpty(Q))
cout<<deQueue(Q)<<" ";
}
for(i=11;i<=15;i++){
enQueue(Q,i);
}
while(!queueEmpty(Q))
{
cout<<deQueue(Q)<<" ";
}
return 0;
}
浙公网安备 33010602011771号