#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{char num[20];
int month;
int day;
struct node *next;
}Node; //声明结构类型 定义结构类型
void insert(Node *head,char *sno,int m,int d);
void display(Node *head);
int main(void)
{Node*head;
head=(Node*)malloc(sizeof(Node));
head->next= NULL; //创建带头节点的空链
int n;
char sno[20];
int month,day;
scanf("%d",&n);
while(n--)
{scanf("%s %d %d",sno,&month,&day);
insert(head,sno,month,day);
}
display(head);
system("PAUSE");
return 0;
}
void insert(Node *head,char *sno,int m,int d)
{Node*p=head->next;
Node*prep=head;
Node *s=(Node*)malloc(sizeof(Node));
strcpy(s->num,sno);
s->month=m;
s->day=d;
while(p!=NULL)
{if(p->month<s->month||p->month==s->month&&p->day<=s->day)
{prep=p;
p=p->next;
}
else break;
}
s->next=p;
prep->next=s;
} //创建有序的链表
void display (Node*head)
{Node*p=head->next;
while(p!=NULL)
{Node*q=p->next;
if (q&&p->month==q->month&&p->day==q->day)
{printf("%d %d %s %s ",p->month,p->day,p->num,q->num);
q=q->next;
while(q&&p->month==q->month&&p->day==q->day)
{
printf("%s ",q->num);
q=q->next;
}
printf("\n");
p=q;
}
else
p=p->next;
}
} //遍历链表
生日相同 输出生日 日期 以及学号 创建、遍历链表
最新推荐文章于 2022-06-30 10:51:04 发布