c语言,结构体配合链表做的一个通讯录管理系统。待改进。
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct stucontact
{
charstuname; //姓名
charstunum; //电话号码
charstubirth; //生日
charstuhome; //籍贯
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, b, c, d;
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);
}
voidlist(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; //a就已经是一个首地址
char b;
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;
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; }
elseprintf("通讯录中不存在您要查找的信息。\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;
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;}
elseprintf("通讯录中不存在您要查找的信息。\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;
}
}
}
先说一下问题,head指针是在create函数里定义的,只能当前函数里用,在外面是无效的,所以你在别的函数里删头结点当然删不掉,可以用全局变量解决。你注释写的很全,这个我要赞一个,这是个好习惯,一定要保留,开发什么都是
嗯嗯,明白。谢谢。:D 自己写的??? 本人c小白0 学习学习.... 不懂.......
页:
[1]