通过队来模拟病人就诊,但有问题
#include <stdio.h>#include <stdlib.h>
typedef struct p{
char num[20];
struct p *next;
}patient;
void initque(patient *&head,patient *&rail)
{
patient *p;
p=(patient *)malloc(sizeof(patient));
p->next=NULL;
head=rail=p;
}
void enque(patient *&head,patient *&rail)
{
patient *q1;
q1=(patient *)malloc(sizeof(patient));
scanf("%s",q1->num);
rail->next=q1;
rail=q1;
}
char * outque(patient *&head,patient *&rail)
{
patient *q1;
if(head->next==rail)
{
return rail->num;
free(rail);
rail=head;
}
else
{
q1=head->next;
head->next=q1->next;
return q1->num;
free(q1);
}
}
int main()
{
patient *head,*rail;
initque(head,rail);
char lin;
scanf("%c",&lin);
while(1)
{
if (lin=='a'||lin=='A')
{
enque(head,rail);
}
else if(lin=='n'||lin=='N')
{
if(head==rail)
{
printf("无病人就诊\n");
}
else
printf("病历号为%s的病人就诊",outque(head,rail));
}
else if(lin=='s'||lin=='S')
{
break;
}
else
printf("输入命令不合法!\n");
scanf("%c",&lin);
}
printf("今天不再接收病人排队,下列排队的病人依次就诊:");
while(head!=rail)
{
printf("%s ",outque(head,rail));
}
return 0;
}
通过队来模拟病人就诊,但运行后就有死循环,我找不出为什么会这样