/*
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);
}