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

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


在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值