数据结构 队列 c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>#define QUEUELEN 15
typedef struct
{
char name[10];
int age;
}DATA;typedef struct
{
DATA data[QUEUELEN];
int head;
int tail;
}SQType;SQType *SQTypeInit()
{
SQType *q;
if(q=(SQType *)malloc(sizeof(SQType)))
{
q->head=0;
q->tail=0;
return q;
}
else
{
return NULL;
}
}int SQTypeIsEmpty(SQType *q)
{
int temp;
temp=q->head==q->tail;
return temp;
}int SQTypeIsFull(SQType *q)
{
int temp;
temp=q->tail==QUEUELEN;
return temp;
}void SQTypeClear(SQType *q)
{
q->head=0;
q->tail=0;
}void SQTypeFree(SQType *q)
{
if(q)
{
free(q);
}
}int InSQType(SQType *q,DATA data)
{
if(q->tail==QUEUELEN)
{
printf("队列已满!操作失败!\n");
return 0;
}
else
{
q->data[q->tail++]=data;
return 1;
}
}DATA *OutSQType(SQType *q)
{
if(q->head==q->tail)
{
printf("队列已空!操作失败!\n");
exit(0);
}
else
{
return &(q->data[q->head++]);
}
}DATA *PeekSQType(SQType *q)
{
if(SQTypeIsEmpty(q))
{
printf("队列空!\n");
return NULL;
}
else
{
return &(q->data[q->head]);
}
}int SQTypeLen(SQType *q)
{
int temp;
temp=q->tail-q->head;
return temp;
}int main()
{
SQType *queue;
DATA data;
DATA *data1;queue=SQTypeInit();
printf("入队列操作:\n");
printf("输入 姓名 年龄 进行入队列操作:\n");
do
{
scanf("%s%d",data.name,&data.age);
if(strcmp(data.name,"0")==0)
{
break;
}
else
{
InSQType(queue,data);
}
}while(1);do
{
printf("出队列操作:按任意键进行出队列操作:\n");
getchar();
data1=OutSQType(queue);
printf("出队列的数据是(%s,%d)\n",data1->name,data1->age);
}while(1);SQTypeFree(queue);
}