新手求解!关于排队编程问题,在C-FREE中检查找不到问题。求解
/*按照要求和提示我写了patient *Creat()、patient *Delete()、int InLine(patient *head)、patient *OutLine(patient *head)、int Search(patient *head)函数,因为不清楚你对程序控制的要求,我简单地写了个控制菜单int menu()函数,用来简单控制程序。你可以根据需要修改int menu()和int main(),在需要的地方声明链表头指针patient *head此程序在DEV-CPP测试通过*/
#include<stdio.h>
#include<string.h>
#define LEN sizeof(patient)
#include<malloc.h>
int IsExist;
struct date
{
char name[20];
char sex[7];
int age;
struct date *next;
};
typedef struct date patient;
patient *Creat() //创建链表
{
patient *head=(patient *)malloc(LEN);
if(head==NULL) return NULL; //创建失败,返回0
printf("Input name,sex(\"male\" or \"female\"),age:");
scanf("%s %s %d",&head->name,&head->sex,&head->age);
head->next=NULL;
IsExist=1; //标记链表存在
return(head);
}
patient *Delete()
{
return NULL;
}
int InLine(patient *head) //排队
{
patient *p1,*p2;
p1=head;
while(p1->next!=NULL) p1=p1->next; //找链尾
p2=(patient *)malloc(LEN);
if(p2==NULL) return 0; //新增元素失败,返回0
printf("Input name,sex(\"male\" or \"female\"),age:");
scanf("%s %s %d",&p2->name,&p2->sex,&p2->age);
p1->next=p2;
p2->next=NULL;
return 1;
}
patient *OutLine(patient *head) //就诊病人离开
{
if(!IsExist) return 0; //链表不存在,返回0
patient *p1,*p2;
p1=head;p2=head->next;
if(head->next==NULL) //链表只有一个元素,删除链表
{
IsExist=0;
return(Delete());
}
if(p2!=NULL) return(p2);
}
int Search(patient *head) //查询自己当前位置
{
patient *p;
p=head;
int i=0;
char sea_name[20];
if(!IsExist) return(i); //链表不存在,返回0
i++;
printf("Input your name:");
scanf("%s",sea_name); //输入姓名并查找
while(strcmp(p->name,sea_name))
{
i++;
p=p->next;
}
return(i); //返回当前位置
}
int menu() //菜单函数
{
int quit=0,temp; //quit变量用于控制退出程序
int select;
static patient *head;
printf("1.Line up\n2.Search my state\n3.Delete the first element\n4.Exit\nYou want to:");
scanf("%d",&select);
switch(select)
{
case 1:if(!(IsExist?InLine(head):(head=Creat()))) exit(0);return 0;
//排队,如果链表不存在则创建,存在则增加长度,操作失败则终止程序
case 2:if(temp=Search(head)) printf("There are %d persons.\n",temp);return 0;
case 3:head=OutLine(head);return 0;
case 4:return 1;
}
}
int main()
{
IsExist=0;
int quit;
do
{
quit=menu();
}
while(!quit);
return 0;
}