循环队列CircularQueue with flag

/*

C Language

*/

#define MAXSIZE 10

#include<stdio.h>
//DEFINE STRUCTURE
typedef struct{
int *base;
int front;
int rear;
int flag;//sign whether queue is full
}CQueue;
//
//FUNCTION
//INIT QUEUE
CQueue *InitQueue(){
CQueue *p;
if((p=(CQueue *)malloc(sizeof(CQueue)))&&(p->base=(int *)malloc(MAXSIZE*sizeof(int)))){
p->front=p->rear=0;
p->flag=0;
return p;
}else{
printf("There is ERROR in malloc !\n");
return NULL;
}
}
//EN QUEUE
void EnQueue(CQueue *p){
int m=0;
if(p->flag==MAXSIZE){
printf("This Queue has been full !\n");
printf("You can not insert any elements !\n");
}else{
printf("Please input the element you want to insert :   ");
scanf("%d",&m);
p->base[p->rear]=m;
p->rear=(p->rear+1)%MAXSIZE;
p->flag++;
printf("\n");
}
}
//DE QUEUE
void DeQueue(CQueue *p){
int e=0;
if(p->flag==0){
printf("This Queue has been empty !\n");
printf("You can not delete any elements !\n");
}else{
e=p->base[p->front];
p->front=(p->front+1)%MAXSIZE;
p->flag--;
printf("The %d has been deleted !\n",e);
}
}
//SHOW QUEUE
void Show(CQueue *p){
int i=0;
if(p->flag==0){
printf("This Queue has been empty !\n");
}else{
printf("*****************************\n");
printf("This Queue is :  \n");
for(i=0;i<(p->flag);i++){
printf("%d\n",p->base[(p->front+i)%MAXSIZE]);
}
printf("*****************************\n");
}
}
//
//MAIN
void main(void){
CQueue *p;
int i=0;
//Test InitQueue
p=InitQueue();
//Test EnQueue
printf("En Queue starts !\n");
for(i=0;i<MAXSIZE;i++){
EnQueue(p);
}
Show(p);
//Test DeQueue
DeQueue(p);
DeQueue(p);
Show(p);
//Test EnQueue the second time
EnQueue(p);
EnQueue(p);
Show(p);
//Test EnQueue the third time
EnQueue(p);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值