查看: 4318|回复: 6

c语言,结构体配合链表做的一个通讯录管理系统。待改进。

[复制链接]
发表于 2016-10-25 13:29:35 | 显示全部楼层 |阅读模式


#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct stucontact
{

        char  stuname[20];    //姓名
        char  stunum[20];     //电话号码
        char  stubirth[20];       //生日
        char  stuhome[20];           //籍贯
        struct stucontact *next;
};
#define LEN sizeof(struct stucontact)
#define n 3   
struct stucontact *creat(void)                       //初始化通讯录
{
        int i = 0;
        struct stucontact *p1 = NULL, *p2 = NULL, *head = NULL;
        char   a[20], b[20], c[20], d[20];
        printf("请输入%d组学生信息(用空格分开)(姓名,手机号,生日,籍贯): \n",n);
        while (i<n)                              
        {        scanf("%s%s%s%s", a,b,c,d);
                i++;
                p1 = (struct stucontact *)malloc(LEN);              //分配新结点
                strcpy(p1->stuname, a);                              //字符串的赋值
                strcpy(p1->stunum, b);
                strcpy(p1->stubirth , c);
                strcpy(p1->stuhome , d);
                if (head == NULL)head = p1;
                else
                        p2->next = p1;         
                p2 = p1;
               
        }
        p2->next = NULL;
        return (head);
}
void  list(struct stucontact *head){                             //输出
        struct stucontact *p;
        if (head == NULL) printf("通讯录为空!");
        else
        {
                printf("通讯录为:\n");
                p = head;
                while (p != NULL)
                {
                        printf("%s  %s  %s  %s\n", p->stuname, p->stunum, p->stubirth, p->stuhome);
                        p = p->next;                     
                }
        }
}
struct stucontact *add(struct stucontact *head)
{
        char ch='y',h;
        while(ch=='y'||ch=='Y'){
                struct stucontact  *p0, *p1, *p2 = NULL;
        p0 = (struct stucontact *)malloc(LEN);
        p0->next = NULL;        
        printf("请输入要填加的学生信息(用空格分开)(姓名,手机号,生日,籍贯): \n");
        scanf("%s%s%s%s", p0->stuname,p0->stunum,p0->stubirth, p0->stuhome);
        getchar();         //空读
        if (head == NULL) head = p0;
        else
        {

                p1 = head;
                while ((strcmp(p0->stuname , p1->stuname)>0) && (p1->next != NULL))  //字符串的比较
                {
                        p2 = p1;
                        p1 = p1->next;

                }
                if (strcmp(p0->stuname , p1->stuname)<=0)
                {
                        if (head == p1){ head = p0;
                        printf("已添加");
                        printf("是否继续添加?确认请输入 y/Y    ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;        }
                        else {p2->next = p0;
                        p0->next = p1;
                        printf("已添加");
                        printf("是否继续添加?确认请输入 y/Y    ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;        }
                }
                else {        p1->next = p0;
        printf("已添加删除");
                        printf("是否继续删除添加?确认请输入 y/Y   ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;}       
        }
        }return (head);

}
struct stucontact *del(struct stucontact *head)
{
        char ch='y',h;
        while(ch=='y'||ch=='Y'){
        char a[20];                    //a就已经是一个首地址
        char b[20];
        struct stucontact *p1 = NULL, *p2 = NULL;
        if (head == NULL)  printf("通讯录为空,无法进行删除操作");
        else
        {
                printf("请输入要删除的学生名字及电话号码(用空格分开):  \n");
                scanf("%s%s", a, b);
                p1 = head;
                while ((strcmp(a, p1->stuname) != 0) && (strcmp(b, p1->stunum) != 0) && (p1->next != NULL))   //查找
                {
                        p2 = p1;
                        p1 = p1->next;
                }
                if ((strcmp(a, p1->stuname) == 0) && (strcmp(b, p1->stunum) == 0))           //姓名和电话号码必须都满足
                {
                        if (head == p1) { head=head->next;}            //  删除不了头结点。。。。望各位大神指导
                        else p2->next = p1->next;                    //链表问题最好画图,多画几次就能基本了解
                        printf("已删除");
                        printf("是否继续删除?确认请输入 y/Y    ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;       
                }
                else {printf("不存在");
                printf("是否继续删除?确认请输入 y/Y    ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;        }

        }
        }
        return (head);
}

struct stucontact *search(struct stucontact *head)
{char ch='y',h;
        while(ch=='y'||ch=='Y'){
        if (head == NULL)  printf("通讯录为空,无法进行查询操作");
        else
        {
                char a[20];
                printf("请输入要查询的学生名字:     ");
                scanf("%s", a);
                struct stucontact *p1 = NULL, *p2 = NULL;
                p1 = head;
                while ((strcmp(a, p1->stuname) != 0) && (p1->next != NULL))
                {
   
                        p2 = p1;
                        p1=p1->next;
                }
                if ((strcmp(a, p1->stuname) == 0))
                {
                        printf("该学生信息为:  姓名:%s 电话:%s  生日:%s  籍贯:%s \n", p1->stuname, p1->stunum, p1->stubirth, p1->stuhome);
            printf("是否继续查询?确认请输入 y/Y     ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;        }
                else  printf("通讯录中不存在您要查找的信息。\n");
        }
        }
        return (head);
}

struct stucontact   *change(struct stucontact  *head)
{
        char ch='y',h;
        while(ch=='y'||ch=='Y'){
        if (head == NULL)  printf("通讯录为空,无法进行查询操作");
        else
        {
                char a[20];
                printf("请输入要修改的学生名字:     ");
                scanf("%s", a);
                struct stucontact  *p1 = NULL, *p2 = NULL;
                p1 = head;
                while ((strcmp(a , p1->stuname) !=0) && (p1->next != NULL))
                {
         
                        p2 = p1;                       //存放
                        p1=p1->next;                      //p1指向下一个
                }
                if ((strcmp(a, p1->stuname)==0))
                {
                printf("请输入修改的信息:  \n");
                scanf("%s%s%s%s", p1->stuname, p1->stunum, p1->stubirth, p1->stuhome);              //执行完上边的循环之后,p2是要删除的结点的上一个,要删除的结点是p1,p1->next是要删除的结点的后一个结点,所以这样赋值就可以直接跳过要删的结点。
             printf("已修改,是否继续修改?确认请输入 y/Y    ");
                scanf("%s",&h);             //单个字符需要加&
                getchar();
                if(h=='y'||h=='Y')
                ch=h;
                else break;}
                else  printf("通讯录中不存在您要查找的信息。\n");
        }
        }
        return(head);
}


void main()
{
        struct stucontact *head;
        head = creat();
        list(head);
        int select;
        while (1)
        {
                printf("\n");
                printf("\n");
                printf("\n");
                printf("\n");
                printf("\t\t\t------------------------\n");
                printf("\t\t\t                       \n");
                printf("\t\t\t    学生通讯录管理系统  \n");
                printf("\t\t\t                        \n");
                printf("\t\t\t------------------------\n");
                printf("\n");
                printf("\t\t\t         1.增加         \n");
                printf("\t\t\t         2.删除         \n");
                printf("\t\t\t         3.搜索         \n");
                printf("\t\t\t         4.修改         \n");
                printf("\t\t\t         5.输出         \n");
                printf("\t\t\t         0.退出         \n");
                printf("请输入您接下来的操作:   ");
                scanf("%d", &select);
                switch (select)
                {
                case 1:add(head); break;
                case 2:del(head); break;
                case 3:search(head); break;
                case 4:change(head); break;
                case 5:list(head); break;
                case 0:printf("谢谢使用,再见!"); exit(1);
                default:printf("\n   按键错误,请重新选择  \n"); break;
                }
        }


}




回复

使用道具 举报

发表于 2016-10-26 16:35:52 | 显示全部楼层
先说一下问题,head指针是在create函数里定义的,只能当前函数里用,在外面是无效的,所以你在别的函数里删头结点当然删不掉,可以用全局变量解决。你注释写的很全,这个我要赞一个,这是个好习惯,一定要保留,开发什么都是
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-10-26 22:39:20 | 显示全部楼层
嗯嗯,明白。谢谢。
回复 支持 反对

使用道具 举报

发表于 2017-1-21 17:17:59 | 显示全部楼层
自己写的???
回复

使用道具 举报

发表于 2017-1-21 17:18:45 | 显示全部楼层
本人c小白0
回复 支持 反对

使用道具 举报

发表于 2017-2-3 18:09:39 | 显示全部楼层
学习学习....
回复

使用道具 举报

发表于 2017-2-25 13:30:30 | 显示全部楼层
不懂.......
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

指导单位

江苏省公安厅

江苏省通信管理局

浙江省台州刑侦支队

DEFCON GROUP 86025

旗下站点

邮箱系统

应急响应中心

红盟安全

联系我们

官方QQ群:112851260

官方邮箱:security#ihonker.org(#改成@)

官方核心成员

Archiver|手机版|小黑屋| ( 沪ICP备2021026908号 )

GMT+8, 2025-5-1 02:17 , Processed in 0.241698 second(s), 20 queries , Gzip On.

Powered by ihonker.com

Copyright © 2015-现在.

  • 返回顶部