C言語で循環キューの初期化&進隊&出隊&読取りチームヘッド要素&判定空-2を実現します.
3761 ワード
/* ( flag )*/
#include
#include
#define Queue_Size 50 //
#define OK 1
#define ERROR 0
typedef struct
{
int elem[Queue_Size]; //
int front; //
int rear; //
int flag; //flag,
}SeqQueue;
/********************** *********************/
void initQueue(SeqQueue *Q); //
int enterQueue(SeqQueue *Q,int n); //
void deleteQueue(SeqQueue *Q); //
int isEmpty(SeqQueue *Q); //
int getHead(SeqQueue *Q,int *x); // , x
int main(){
SeqQueue Q;
int choice;
while(true) //
{
printf("*****************Please enter your choice*****************
");
printf(" choice 1:Queue initialization
");
printf(" choice 2:Into the queue
");
printf(" choice 3:Out of the queue
");
printf(" choice 4:Determine whether the queue is empty
");
printf(" choice 5:Get queue head
");
printf(" choice 0:exit
");
scanf("%d",&choice);
switch(choice)
{
case 1:
initQueue(&Q);
break;
case 2:
int n;
printf("Please enter the number into the queue elements:");
scanf("%d",&n); //
//
(enterQueue(&Q,n)==1)?printf("%d
",n):printf("%d
",n);
break;
case 3:
deleteQueue(&Q);
break;
case 4: //
(isEmpty(&Q)==1)?printf("An empty Queue
"):printf("Not an empty Queue
");
break;
case 5: //
int x;
(getHead(&Q,&x)==0)?printf("An empty Queue error!!!!
"):printf("Get head successful, :%d
",x);
break;
case 0:
exit(0);
break;
default:
printf("ERROR!!
");
exit(0);
break;
}
}
return 0;
}
/********************** *********************/
void initQueue(SeqQueue *Q)
{ // *Q
Q->front=Q->rear=0;
Q->flag=0;
}
int enterQueue(SeqQueue *Q,int n) //
{ // n
int n1,n2;
if((Q->front==Q->rear)&&(Q->flag==1)) return ERROR; //
printf("Please enter into the queue elements in turn:
");
for(n1=0;n1elem[Q->rear]=n2;
Q->rear=(Q->rear+1)%Queue_Size; //
if(Q->front==Q->rear) // :1. , 2. ,
{
Q->flag=1; //flag=1:
return ERROR;
}
}
return OK;
}
void deleteQueue(SeqQueue *Q)
{ //
int a;
if((Q->front==Q->rear)&&(Q->flag==0)) // ,
{
printf("An empty Queue error!!!!
");
}
else
{
a=Q->elem[Q->front]; //
Q->front=(Q->front+1)%Queue_Size; //
printf(" %d .
",a); //
if(Q->front==Q->rear) Q->flag=0; //flag=0:
}
}
int isEmpty(SeqQueue *Q)
{
if((Q->front==Q->rear)&&(Q->flag==0))
{ // , OK
return OK;
}
return ERROR; // ERROR
}
int getHead(SeqQueue *Q,int *x)
{
if((Q->front==Q->rear)&&(Q->flag==0))
{ // ,
return ERROR;
}
else
{
*x=Q->elem[Q->front]; //
return OK;
}
}