生日相同 输出生日 日期以及学号创建、遍历链表

    科技2025-10-30  8

    #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; } } //遍历链表

    Processed: 0.013, SQL: 8