关于C语言通讯录的代码,大家来看看

pepperhcj 2014-08-02 06:42:34
#include <stdio.h>
//#include <malloc.h> //得到指向大小为Size的内存区域的首字节的指针//
#include <string.h>
#include <stdlib.h> //标准库函数//
#define NULL 0
#define LEN sizeof(struct address_list) //计算字节//
int n;
struct address_list
{
char name[30]; //名字
char work[30]; //职业
char handset[30]; //手机
char email[30]; //电子邮件
char address[30]; //通讯地址
struct address_list *next;
};
struct address_list *shifang(struct address_list *head); // 释放内存函数声明
//创建函数,不带头结点的链表
struct address_list *creat(void)
{
struct address_list *head,*p1,*p2;
char name[20];
n=0;
p1=(struct address_list *)malloc(LEN);
p2=p1; //强制内存转换
printf("请输入通讯录的内容!\n姓名输入为0时表示创建完毕!\n");
printf("请输入姓名:");
gets(name);
if(strcmp(name,"0")!=0)
{
strcpy(p1->name,name);
printf("请输入职业:"); gets(p1->work);
printf("请输入手机:"); gets(p1->handset);
printf("请输入电子邮件:"); gets(p1->email);
printf("请输入通讯地址:"); gets(p1->address);
head=NULL;
while(1)
{
n=n+1; //记录通讯录人数个数
if(n==1) ///????n==0
head=p1;
else
p2->next=p1;
p2=p1;
printf("请输入姓名:");
gets(name);
if(strcmp(name,"0")==0)
{
break;
}
else
{
p1=(struct address_list *)malloc(LEN);
strcpy(p1->name,name);
printf("请输入职业:"); gets(p1->work);
printf("请输入手机:"); gets(p1->handset);
printf("请输入电子邮件:"); gets(p1->email);
printf("请输入通讯地址:"); gets(p1->address);
}
}
p2->next=NULL;
return head;
}
else
return 0;
}
//输出函数
void print(struct address_list *head)
{
struct address_list *p;
if(head!=NULL)
{
p=head;
printf("本通讯录现在共有%d人:\n",n);
printf("---姓名-------职业--------手机-------Email-------通讯地址\n");
printf("==================================\n");
do
{
printf("== %s",p->name); printf(" ");
printf("%s",p->work); printf(" ");
printf("%s",p->handset); printf(" ");
printf("%s",p->email); printf(" ");
printf("%s",p->address); printf(" \n");
p=p->next;
}while(p!=NULL);
printf("==================================\n");
}
else
printf("通讯录为空,无法输出!\n");
}
//增加函数
struct address_list *insert(struct address_list *head)
{
struct address_list *p0,*p1,*p2;
char name[20];
p1=head;
printf("请输入增加的内容:\n");
printf("请输入姓名:");
gets(name);
if(strcmp(name,"0")==0)
{
printf("姓名不能为0,增加失败!\n");
return(head);
}
else
{
p0=(struct address_list *)malloc(LEN);
strcpy(p0->name,name);
printf("请输入职业:"); gets(p0->work);
printf("请输入手机:"); gets(p0->handset);
printf("请输入电子邮件:"); gets(p0->email);
printf("请输入通讯地址:"); gets(p0->address);
n=n+1;
if(head==NULL)
{
head=p0;
p0->next=NULL;
return head;
}
else
{
while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return head;
}
}
}
struct address_list* delete_txl(struct address_list *head)
{
struct address_list *p,*q;
char name[30];
if(head==NULL)
{
printf("通讯录为空,无法显示!\n");
return head;
}
p=head;
printf("请输入需要删除的人的姓名:");
gets(name);
if(strcmp(head->name,name)==0)
{
head=head->next;
free(p);
printf("删除操作成功!\n");
return head;
}
else
{
q=head,p=head->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
q->next=p->next;
free(p);
printf("删除操作成功!\n");
return head;
}
p=p->next;
q=q->next;
}
}
}
//显示函数
struct address_list *display(struct address_list *head)
{
struct address_list *p1,*p2;
char name[30];
int m;
if(head==NULL)
{
printf("通讯录为空,无法显示!\n");
return head;
}
p1=head;
m=0;
printf("请输入需要显示人的姓名:");
gets(name);
while(p1!=NULL)
{
while((strcmp(p1->name,name))!=0 && p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf("%s的通讯内容如下:\n",name);
printf("---姓名--------职业--------手机-------Email------通讯地址\n");
printf("==================================\n");
printf("== %s",p1->name);printf(" ");
printf("%s",p1->work);printf(" ");
printf("%s",p1->handset);printf(" ");
printf("%s",p1->email);printf(" ");
printf("%s",p1->address); printf(" \n");
printf("==================================\n");
}
p1=p1->next;
}
if(m==0)
{
printf("此人未在本通讯录中!\n");
}
return(head);
}

//排序函数
struct address_list *paixu(struct address_list *head)
{
struct address_list *p1,*p2;
int i,j;
struct address_list1
{
char name[30];
char work[30];
char handset[30];
char email[30];
char address[30];
};
struct address_list1 px[200];
struct address_list1 temp;
if(head==NULL)
{
printf("通讯录为空,无法排序!\n");
return(head);
}
p1=head;
for(i=0;i<n,p1!=NULL;i++)
{
strcpy(px[i].name,p1->name);
strcpy(px[i].work,p1->work);
strcpy(px[i].handset,p1->handset);
strcpy(px[i].email,p1->email);
strcpy(px[i].address,p1->address);
p2=p1;
p1=p1->next;
}
head=shifang(head);
for(j=0;j<n-1;j++)
{
for(i=j+1;i<n;i++)
{
if(strcmp(px[i].name,px[j].name)<0)
{
temp=px[i];
px[i]=px[j];
px[j]=temp;
}
}
}
p1=(struct address_list *)malloc(LEN);
p2=p1;
strcpy(p1->name,px[0].name);
strcpy(p1->work,px[0].work);
strcpy(p1->handset,px[0].handset);
strcpy(p1->email,px[0].email);
strcpy(p1->address,px[0].address);

head=p1;
for(i=1;i<n;i++)
{
p1=(struct address_list *)malloc(LEN);
strcpy(p1->name,px[i].name);
strcpy(p1->work,px[i].work);
strcpy(p1->handset,px[i].handset);
strcpy(p1->email,px[i].email);
strcpy(p1->address,px[i].address);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
printf("按姓名排序后为:\n");
print(head);
return(head);
}
//姓名查找函数
struct address_list *search(struct address_list *head)
{
struct address_list *p1,*p2;
int m;
char name[30];
if(head==NULL)
{
printf("通讯录为空,无法分类查找!\n");
return(head);
}
p1=head;
printf("********************\n");
printf("** 请输入需要查找的姓名 **\n");
printf("********************\n");
m=0;
gets(name);
while(p1!=NULL)
{
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
m++;
printf("你查找的内容是:\n");
printf("+++++++++++++++++++++++++++++++++++\n");
printf("++ %s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
printf("+++++++++++++++++++++++++++++++++++\n");
}
p1=p1->next;

if(m==0)
{
printf("此人未在本通讯录中!\n");
}
break;
}

return(head);
}

//释放内存函数
struct address_list *shifang(struct address_list *head)
{
struct address_list *p1;
while(head!=NULL)
{
p1=head;
head=head->next;
free(p1);
}
return(head);
}

//文件写入函数
void save(struct address_list *head)
{
FILE *fp;
struct address_list *p1;
char tong[30];
if(head==NULL)
{
printf("通讯录为空,无法存储!\n");
return;
}
printf("请输入保存后的文件名:");
gets(tong);
fp=fopen("tong.txt","w");
if(fp==NULL)
{
printf("cannot open file\n");
return;
}
p1=head;
fprintf(fp,"姓名 职业 手机 Email 通讯地址\n");
for(;p1!=NULL;)
{
fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->work,p1->handset,p1->email,p1->address);
p1=p1->next;
}
printf("保存完毕!\n");
fclose(fp);
}


这个是C语言通讯录的代码我想知道那个p2有什么作用。。创建函数那一块,总是把值赋给p2,,但是p2是怎么起作用的呢。
...全文
963 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyd54454 2014-08-03
  • 打赏
  • 举报
回复
引用 7 楼 u012632288 的回复:
当n=1的时候,,输出的是p1,,当n大于1的时候,输出的是什么。。 if(n==1) // head=p1; else p2->next=p1; p2=p1; 这一段是怎么起作用的。。可以讲讲吗,我链表这一块学得不好,基础有点差
如果n等于1(这里判断n==1,是因为上面,n = n + 1的缘故),head指向p1所指向的内存,即让head指向链表的头,只执行那么一次。否则,因为p2实则是指向链表最后一个节点的,那么让p2->next = p1,就是让最后一个节点的next指针指向p1(p1是新malloc出来的内存)。然后让p2再指向p1,保证了p2永远是链表最后一个节点,方便你一直尾插。你可以把p2名字改成tail,可能会清晰一点。
pepperhcj 2014-08-03
  • 打赏
  • 举报
回复
引用 5 楼 cyd54454 的回复:
malloc出来的内存都是无名内存,也就是说你不能通过变量名知道它的地址,所以你必须手动用指针指向该地址,一旦没有指针指向该内存,你就找不到它了,所以才有p2=p1,不然p1指向别的地方,前面的那块内存就没那么方便的被找到(你要是不用p2,你可以从head开始遍历..脑洞大开的人才这么做)。有名内存:比如int a ;你就能通过a这变量名,访问该内存地址。
当n=1的时候,,输出的是p1,,当n大于1的时候,输出的是什么。。 if(n==1) // head=p1; else p2->next=p1; p2=p1; 这一段是怎么起作用的。。可以讲讲吗,我链表这一块学得不好,基础有点差
我看你有戏 2014-08-03
  • 打赏
  • 举报
回复
p2保存了一个节点内存块的首地址啊
cyd54454 2014-08-03
  • 打赏
  • 举报
回复
malloc出来的内存都是无名内存,也就是说你不能通过变量名知道它的地址,所以你必须手动用指针指向该地址,一旦没有指针指向该内存,你就找不到它了,所以才有p2=p1,不然p1指向别的地方,前面的那块内存就没那么方便的被找到(你要是不用p2,你可以从head开始遍历..脑洞大开的人才这么做)。有名内存:比如int a ;你就能通过a这变量名,访问该内存地址。
cyd54454 2014-08-03
  • 打赏
  • 举报
回复
引用 2 楼 zxh707wk 的回复:
引用 1 楼 lisong694767315 的回复:
p2用来记录p1的上一个节点。。。
+1
+2,蹭一个
Zidane_2014 2014-08-03
  • 打赏
  • 举报
回复
记录上一个节点。 不然后边无法继续向后读取。
707wk 2014-08-02
  • 打赏
  • 举报
回复
引用 1 楼 lisong694767315 的回复:
p2用来记录p1的上一个节点。。。
+1
神奕 2014-08-02
  • 打赏
  • 举报
回复
p2用来记录p1的上一个节点。。。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧