//循环队列之队列的顺序表示和实现
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define MASIZE 100
typedef int Elemt;
typedef struct{
int * base;
int front;
int rear;
}CQueue;
//创建一个空的循环列表
Elemt creat_Queue(CQueue &Q){
Q.base=(int*)malloc(MASIZE*sizeof(int));
Q.front=Q.rear=0;
printf("creat successful!!!\n");
return OK;
}
//释放队列
Elemt free_Queue(CQueue &Q){
if(Q.base != NULL ){
free(Q.base);
printf("free successful!!!!\n");
return OK;
}
return ERROR;
}
// 判断该循环队列是否满了,若满,则返回1,否则返回0
Elemt determin_FULL(CQueue Q){
if((Q.rear+1)%MASIZE == Q.front ) return OK;
return ERROR;
}
//向循环队列中插入元素
Elemt in_Queue(CQueue &Q,int e){
if(determin_FULL(Q)) {
printf(" the Queue is full!!!!\n");
return ERROR;
}
Q.base[Q.rear]=e;
Q.rear=(Q.rear + 1 ) % MASIZE;
printf(" in successfull!!!\n");
return OK;//成功返回1
}
//判断该循环链表是否为空
Elemt determin_null(CQueue Q){
if(Q.front == Q.rear) return OK;
else return ERROR;
}
//遍历(也就是将之前插入的元素一一删除,并输出该元素)
Elemt bianli_Queue(CQueue &Q){
if(determin_null(Q)) {
printf("the QUEUE is null:\n");
return ERROR;
}
;
do{
printf("%d...........\n",Q.base[Q.front]);
Q.front=Q.front+1;
}while(Q.front% MASIZE != Q.rear) ;
return OK;
}
//主函数
int main(){
CQueue Q;
int e,n=5;
creat_Queue(Q);
while(n--){
printf("input your e:");
scanf("%d",&e);
in_Queue(Q,e);
}
bianli_Queue(Q);
free_Queue(Q);
return OK;
}