C言語データ構造の循環キュー(作成、入隊、入隊、ヘッドの要素を取り、巡回)
//*循環キューの作成*循環キューの初期化*一次元配列を作成*列の空対列の満杯判定*データドメイン構造体の作成*循環キューの構造体の作成*循環キューの長さを求める*
#include
#include
#include
#include
#define OK 1
#define ERROR 0
#define MAXSIZE 1024 //
#define NAMESIZE 255//
int count=0;//
typedef int IdType;//
typedef char*NameTyp;//
typedef int Statu;//
typedef struct elementtype
{
IdType id;//
NameTyp name;//
}ElementType;//
typedef struct seqqueue
{
ElementType *data;//
int rear;//
int front;//
}Seq_Queue;//
int length_seqqueue(Seq_Queue*q);//
Statu empty_seqqueue(Seq_Queue*q);//
Statu full_seqqueue(Seq_Queue*q);//
Statu create_seqqueue(Seq_Queue*q);//
void Init_seqqueue(Seq_Queue*q);//
Statu pop_seqqueue(Seq_Queue*q,ElementType *data);//
Statu push_seqqueue(Seq_Queue*q,ElementType *data);//
Statu getdata_seqqueue(Seq_Queue*q,ElementType *data);//
void Display_seqqueue(Seq_Queue*q);//
void main()
{
Seq_Queue *q;
ElementType data;//
ElementType data1;//
ElementType key;//
q=(Seq_Queue*)malloc(sizeof(Seq_Queue));
//
Init_seqqueue(q);
//
int result=create_seqqueue(q);
if(result==ERROR)
printf("
");
else
{
printf("
");
printf(" %d
",count);
}
if(full_seqqueue(q))
printf("
");
else
printf("
");
if(empty_seqqueue(q))
printf("
");
else
{
printf("
");
printf(" %d
",length_seqqueue(q));
}
data.name=(NameTyp)malloc(sizeof(char)*NAMESIZE);
int result1=pop_seqqueue(q,&data);
if(result1==ERROR)
printf("
");
else
{
printf("
");
printf(" [%d,%s]
",data.id,data.name);
}
key.name=(NameTyp)malloc(sizeof(char)*MAXSIZE);
int result3=getdata_seqqueue(q,&key);
if(result3==ERROR)
printf("
");
else
{
printf("
");
printf(" [%d,%s]
",key.id,key.name);
}
data1.name=(NameTyp)malloc(sizeof(char)*NAMESIZE);
data1.id=rand()%30;
printf("
");
scanf("%s",data1.name);
int result2=push_seqqueue(q,&data1);
if(result2==ERROR)
printf("
");
else
printf("
");
printf("
");
Display_seqqueue(q);
}
void Init_seqqueue(Seq_Queue*q)//
{
if(q==NULL)
printf("
");
else
{
q->data=(ElementType*)malloc(sizeof(ElementType)*MAXSIZE);//
q->front=0;
q->rear=0;
}
}
Statu create_seqqueue(Seq_Queue*q)//
{
char name[NAMESIZE];
if(q==NULL)
{
return ERROR;
}
printf("
");
while(scanf("%s",name)!=EOF)
{
if(strcmp(name,"^")==0)
break;
else
{
q->data[q->rear].name=(NameTyp)malloc(sizeof(char)*NAMESIZE);
strcpy(q->data[q->rear].name,name);
q->data[q->rear].id=rand()%50;//
q->rear=(q->rear+1)%MAXSIZE;
count++;
}
}
return OK;
}
Statu empty_seqqueue(Seq_Queue *q)//
{
if(q->rear==q->front)
return OK;
else
return ERROR;
}
Statu full_seqqueue(Seq_Queue*q)//
{
if((q->rear+1)%MAXSIZE==q->front)// rear
return OK;
else
return ERROR;
}
int length_seqqueue(Seq_Queue*q)//
{
//
if(empty_seqqueue(q))
return ERROR;
else
{
return ((q->rear-q->front+MAXSIZE)%MAXSIZE);
}
}
Statu pop_seqqueue(Seq_Queue*q,ElementType *data)//
{
if(empty_seqqueue(q))
return ERROR;
else
{
strcpy(data->name,q->data[q->front].name);
data->id=q->data[q->front].id;
q->front=(q->front+1)%MAXSIZE;// front
return OK;
}
}
Statu push_seqqueue(Seq_Queue*q,ElementType*data)//
{
//
if(full_seqqueue(q))
return ERROR;
else
{
q->data[q->rear].name=(NameTyp)malloc(sizeof(char)*MAXSIZE);
strcpy(q->data[q->rear].name,data->name);
q->data[q->rear].id=data->id;
q->rear=(q->rear+1)%MAXSIZE;
return OK;
}
}
Statu getdata_seqqueue(Seq_Queue*q,ElementType *data)//
{
if(empty_seqqueue(q))
return ERROR;
else
{
data->id=q->data[q->front].id;
strcpy(data->name,q->data[q->front].name);
return OK;
}
}
void Display_seqqueue(Seq_Queue*q)//
{
//
if(empty_seqqueue(q))
return ERROR;
else
{
while(empty_seqqueue(q)!=1)
{
ElementType data;
data.name=(NameTyp)malloc(sizeof(char)*MAXSIZE);
pop_seqqueue(q,&data);
printf("[%d,%s]\t",data.id,data.name);
}
}
}