链表习题4

(1)          将统一提供的contacts.dat文件复制到计算机D盘根目录中,该文件以二进制形式存放了数量未知个通讯录数据,每条通讯录数据的结构定义如下:

struct Contactor

{

         charname[20];                  //姓名,在数据文件中不存在重复的姓名

         int age;                                //年龄

         int sex;                                 //性别,0代表女性,1代表男性

char phone[20];                 //电话号码

         charpostcode[10];           //邮政编码

         charaddress[100];           //通讯地址

};

(2)          编写一个函数。将D盘根目录中contacts.dat文件中的各个通讯录数据依次读出,构成一个单链表;

(1)      编写一个函数。显示每个人通讯录信息,显示格式要求如下。其中,性别的显示格式要求为:男性显示’M’,女性显示’F’。

上述示意内容中斜体和线条部分仅用于说明题意的说明信息,不需要实际显示出来

 

(2)      编写一个函数。在链表表头插入一个新的结点,新结点的数据和信息需要在主函数中由键盘输入,输入时自己注意不能输入重复名字。并随后在主函数中显示插入操作结束后的链表内容。

(3)      编写一个函数。删除在第4步所生成链表中年龄大于等于25岁的通讯录结点。并随后在主函数中显示删除操作结束后的链表内容。

(4)      编写一个函数。计算出在第5步所生成链表中女性的平均年龄和最大年龄,计算结果的显示必须在主函数中完成。

(5)      编写一个函数。从键盘输入一个姓名,查询链表。如果姓名存在,则修改该条通讯录的详细信息,新的信息也由键盘输入;如果姓名不存在,则不进行修改操作,但需要在主函数中显示“姓名不存在”的提示信息。

(6)      将经过上述第7步处理后的链表中全部通讯录信息保存在D:\res.txt中,格式要求与第3步相同。



typedef struct Contactor

{
    char name[20];        //姓名,在数据文件中不存在重复的姓名
    int age;                //年龄
    int sex;                //性别,0代表女性,1代表男性
    char phone[20];        //电话号码
    char postcode[10];        //邮政编码
    char address[100];        //通讯地址
    struct Contactor *next;
}TagNode;

  TagNode* append_list(TagNode* head,TagNode* temp)
 {
     TagNode* p=head->next;
     if(p==NULL){
         head->next=temp;
     }
     else{
         while(p->next){
             p=p->next;
         }
         p->next=temp;
         temp->next=NULL;
     }
     return head;
 }

 TagNode* create_list(char* filename)
 {
     FILE* fp=fopen(filename,"rb");
     TagNode temp;
     TagNode* p,*head;
     if(fp==NULL){
         printf("file error!\n");
         exit(0);
     }
     head=(TagNode*)malloc(sizeof(TagNode));
     head->next=NULL;
     while(fread(&temp,sizeof(TagNode)-sizeof(TagNode*),1,fp)==1){
         p=(TagNode*)malloc(sizeof(TagNode));
         *p=temp;
         p->next=NULL;
         head=append_list(head,p);
     }
     fclose(fp);
     return head;
 }

 int check(char* name,TagNode* head)
 {
     TagNode* p=head->next;
     while(p){
         if(strcmp(p->name,name)==0) return 0;
         p=p->next;
     }
     return 1;
 }

void insert_list(TagNode* head,TagNode* p)
{
    TagNode* temp=head->next;
    if(head->next==NULL) head->next=p;
    else{
        head->next=p;
        p->next=temp;
    }
}

void delete_list(TagNode* head)
{
    TagNode *p,*q;
    p=head->next;
    q=head;
    while(p)
    {
        if(p->age>=25)
        {
            q->next=p->next;
            free(p);      //释放删除节点的内存
            p=q->next;
        }
        else
        {
            q=p;
            p=p->next;
        }
    }
    return;
}

void calculate_list(TagNode* head,double* avg,int* max)
{
    int sum=0;
    int count=0;
    TagNode* p=head->next;
    if(p==NULL) return;
    while(p){
        if(p->sex==1){
            if(p->age>*max) *max=p->age;
            sum+=p->age;
            count++;
        }
        p=p->next;
    }
    *avg=(double)sum/count;
}

void revise_list(TagNode* head,char* name)
{
    TagNode* p=head->next;
    while(p){
        if(strcmp(p->name,name)==0){
            printf("年龄:"); scanf("%d",&p->age);
            printf("性别:"); scanf("%d",&p->sex);
            printf("电话:"); scanf("%s",&p->phone);
            printf("邮编:"); scanf("%s",&p->postcode);
            printf("通讯地址:"); scanf("%s",&p->address);
        }
        p=p->next;    
    }
}


void write_list(char* filename,TagNode* head)
{
    FILE* fp=fopen(filename,"w");
    if(fp==NULL){
        printf("file error!\n");
        exit(0);
    }
    TagNode* p=head->next;
    while(p){
        fprintf(fp,"%-10s%-3d%-2c%-12s%8s%-20s\n",p->name,p->age,p->sex,p->phone,p->postcode,p->address);
        p=p->next;
    }
    fclose(fp);
}

void show_list(TagNode* head)
{
    TagNode* p=head->next;
    while(p){
        char temp;
        if(p->sex==1) temp='M';
        else temp='F';
        printf("%-10s%-3d%-2c%-12s%8s%-20s\n",p->name,p->age,temp,p->phone,p->postcode,p->address);
        p=p->next;
    }
}

void release_list(TagNode* head)
{
    TagNode* p,*q;
    p=q=head;
    while(p){
        q=p->next;
        free(p);
        p=q;
    }
}

 void main()
 {

     TagNode* head,*p;
     int max=0;
     double avg=0;
     char name[20];
     //===创建链表并显示链表=======
     puts("文件读取后的记录链表为:");
     head=create_list("D:\\contact.dat");
     show_list(head);
     //=====插入新结点===============
     p=(TagNode *)malloc(sizeof(TagNode));     //申请内存
     p->next=NULL;
     printf("请输入你要插入节点的姓名:");
     scanf("%s",&p->name);
     while(check(p->name,head)==0)
     {
         printf("名字已存在,请重新输入:");
         scanf("%s",&p->name);
     }
     printf("年龄:"); scanf("%d",&p->age);
     printf("性别:"); scanf("%d",&p->sex);
     printf("电话:"); scanf("%s",&p->phone);
     printf("邮编:"); scanf("%s",&p->postcode);
     printf("通讯地址:"); scanf("%s",&p->address);
     insert_list(head,p);
     show_list(head);
     //=======删除年龄大于等于25岁的通讯录结点=====
     puts("删除年龄大于等于25岁的通讯录结点:");
     delete_list(head);
     show_list(head);
     //=====计算女性的平均年龄和最大年龄====
     calculate_list(head,&avg,&max);
     puts("计算女性的平均年龄和最大年龄:");
     printf("%lf     %d\n",avg,max);
     //=====修改该条通讯录======
     printf("input the name you want to modify:");
     scanf("%s",name);
     if(check(name,head)==1) printf("the name is invalid!\n");
     else revise_list(head,name);
     show_list(head);
     //=====保存通讯录===========
     write_list("D:\\res.txt",head);
     release_list(head);
     return ;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值