此程序在DEV-CPP测试通过*/
#include<stdio.h>
#include<string.h>
#define LEN sizeof(patient)
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=headp2=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
}
struct BR_ //病人结构体;{
int id//病人ID;
int lv//病人等级;
}
int main()
{
int n,i,j,yisheng,binglv,brnum = 0
BR_ b2y[3][100] = { }//存放每个医生对应的病人旅槐;
int bingnum[3] = {}//存放每个医生对应的病人个数;
char order[5]
//这里可以把所有输入的字串,存起来,再一一解决; 但是你原来的代码,是输入一个处理一个;
//所以 我也输入一个 处理一个了;
//但是题意是 : 输入完成后,再依次输出;
//所以,只能把要输出的东西,存在一个数组里面(下面的数组);等输入完成后再统一依次输颂镇绝出
char print_[300][8] = {}//用来存放 最后print的字符串; 每一个out 都要处理一下;
int str_num = 0//字符串的条数;
scanf("%d",&n)
for(j = 0 j <n++j)
{
scanf("%s",order)
if(strcmp(order,"IN") == 0 ) //字符串比较野姿,如果输入的IN,
{
brnum += 1
scanf("%d",&yisheng)
scanf("%d",&binglv)
BR_ temp
temp.id = brnum
temp.lv = binglv
b2y[ yisheng ][ bingnum[yisheng] ] = temp
bingnum[yisheng]++
//排序; 把等级高的 放在前面; 注意使用 稳定排序 的方式;比如下面的选择排序;
int pos = 0
while(pos != n - 1)
{
for(i = pos + 1i<n++i)
{
if(b2y[yisheng][pos].lv <b2y[yisheng][i].lv)
{
BR_ temp1 = b2y[yisheng][pos]
b2y[yisheng][pos] = b2y[yisheng][i]
b2y[yisheng][i] = temp1
}
}
pos++
}
}
else if( strcmp(order,"OUT") == 0) //
{
scanf("%d",&yisheng)
if(bingnum[yisheng] <= 0) //如果这个医生当前病人为0 输出empty;
strcpy(print_[str_num++],"EMPTY")
else //不为0;输出排在最前面(也就是等级最高的病人);
{
char temp
itoa(b2y[yisheng][0].id,print_[str_num++],10)
//最前面的病人 已经处理了,所以把数组后面的元素依次往前移动;
for(i = 0i <bingnum[yisheng] - 1++i)
{
b2y[yisheng][i] = b2y[yisheng][i + 1]
}
bingnum[yisheng]--//病人数量递减;
}
}
}
for(i = 0 i <str_num++i)
printf("%s\n",print_[i])
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)