#include<stdlib.h>
#include<stdio.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!=NULL){
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队列一空!操作失败!\n");
exit(0);
}
else
{
return &(q->data[q->head++]);
}
}
DATA *PeekSQType(SQType*q){
if(SQTypeIsEmpty(q)){
printf("\n空队列\n");
return NULL;
}
else{
return &(q->data[q->head]);
}
}
int SQTypeLen(SQType *q){
int temp;
temp=q->tail-q->head;
return (temp);
}
void main(){
SQType*stack;
DATA data;
DATA *data1;
stack=SQTypeInit();
printf("入队列操作:\n");
printf("输入姓名 年龄 进行入队列操作:");
do{
scanf("%s%d",data.name,&data.age);
if(strcmp(data.name,"0")==0)
{
break;
}
else
{
InSQType(stack,data);
}
}while(1);
do
{
printf("出队列操作;按任意键进行出栈操作:\n");
getchar();
data1=OutSQType(stack);
printf("出队列的数据是(%s,%d)\n",data1->name,data1->age);
}while(1);
SQTypeFree(stack);
}
队列(Queue)
最新推荐文章于 2024-10-11 11:31:13 发布