写的通讯录
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MAX 1024
struct Contact
{
char name;
char phone;
struct Contact *next;
};
int count = 0;
struct Contact *pool = NULL;
void addPerson(struct Contact **pointer);
void findPerson(struct Contact *pointer);
void changePerson(struct Contact *pointer);
void delPerson(struct Contact **pointer);
void displayContact(struct Contact *pointer);
void release(struct Contact *forward);
void shutdown(struct Contact *pointer);
void addPerson(struct Contact **pointer)
{
struct Contact *p;
if(pool == NULL)
{
p = (struct Contact *)malloc(sizeof(struct Contact));
if (p==NULL)
{
printf("分配新空间失败,请重试。\n");
exit(1);
}
}
else
{
p = pool;
pool = pool -> next;
count--;
}
printf("添加联系人:请输入姓名\n");
scanf("%s",p->name);
printf("添加联系人:请输入电话号码\n");
scanf("%s",p->phone);
if (*pointer == NULL)
{
*pointer = p;
p->next = NULL;
}
else
{
struct Contact *temp = *pointer;
*pointer = p;
p->next = temp;
}
}
void findPerson(struct Contact *pointer)
{
char input;
int i=0;
printf("请输入要查询的姓名\n");
scanf("%s",input);
while(pointer != NULL)
{
if (!strcmp(input, pointer->name))
{
printf("%s的电话号码是%s\n",input, pointer->phone );
i++;
}
pointer = pointer -> next;
}
if(i==0)
{
printf("未找到联系人%s\n",input );
}
}
void changePerson(struct Contact *pointer)
{
char input;
int i=0;
printf("请输入要更改联系方式的联系人姓名\n");
scanf("%s",input);
while(pointer != NULL)
{
if (!strcmp(input, pointer->name))
{
printf("更改%s的电话号码为\n",input );
scanf("%s",pointer->phone);
i++;
}
pointer = pointer -> next;
}
if(i==0)
{
printf("未找到联系人%s\n",input );
}
}
void delPerson(struct Contact **pointer)
{
char input;
int i=0;
printf("请输入要删除的联系人姓名\n");
scanf("%s",input);
struct Contact *back = NULL;
struct Contact *forward = NULL;
if (!strcmp(input, (*pointer)->name))
{
forward = *pointer;
*pointer = (*pointer)->next;
release(forward);
i++;
}
else
{
back = *pointer;
forward = (*pointer)->next;
while(forward != NULL)
{
if (!strcmp(input, (forward)->name))
{
back->next = forward->next;
release(forward);
i++;
}
back = forward;
forward = forward->next;
}
}
if(i==0)
{
printf("未找到联系人%s\n",input );
}
}
void release(struct Contact *forward)
{
if (pool == NULL)
{
pool = forward;
forward->next = NULL;
}
else if(count < MAX)
{
struct Contact *tempool = pool;
pool = forward;
forward->next = tempool->next;
count++;
}
else
{
free(forward);
}
}
void displayContact(struct Contact *pointer)
{
struct Contact *p = pointer;
while(p != NULL)
{
printf("%s : %s \n", p->name, p->phone );
p = p->next;
}
}
void shutdown(struct Contact *pointer)
{
if(pointer != NULL)
{
struct Contact *temp = pointer;
free(pointer);
pointer = temp->next;
}
if(pool != NULL)
{
struct Contact *temp = pool;
free(pool);
pool = temp->next;
}
}
int main(void)
{
struct Contact *pointer = NULL;
printf("===请输入命令代码==========\n");
printf("===1: 增加一个联系人======\n");
printf("===2: 删除一个联系人======\n");
printf("===3: 查看指定联系人======\n");
printf("===4: 显示所有联系人======\n");
printf("===5: 更改联系人联系方式===\n");
printf("===6: 退出===============\n");
while(1)
{
int i = 1;
scanf("%d",&i);
switch(i)
{
case 1:
addPerson(&pointer);
break;
case 2:
delPerson(&pointer);
break;
case 3:
findPerson(pointer);
break;
case 4:
displayContact(pointer);
break;
case 5:
changePerson(pointer);
break;
case 6:
goto SHUTDOWM;
break;
default:
printf("请输入正确的指令代码");
}
}
SHUTDOWM: shutdown(pointer);
return 0;
}
测试:
===请输入命令代码==========
===1: 增加一个联系人======
===2: 删除一个联系人======
===3: 查看指定联系人======
===4: 显示所有联系人======
===5: 更改联系人联系方式===
===6: 退出===============
1
添加联系人:请输入姓名
爱新觉罗努尔哈赤
添加联系人:请输入电话号码
13567852435
1
添加联系人:请输入姓名
嬴政
添加联系人:请输入电话号码
1898723645
1
添加联系人:请输入姓名
朱元璋
添加联系人:请输入电话号码
18876245603
1
添加联系人:请输入姓名
赵匡胤
添加联系人:请输入电话号码
17856730987
4
赵匡胤 : 17856730987
朱元璋 : 18876245603
嬴政 : 1898723645
爱新觉罗努尔哈赤 : 13567852435
5
请输入要更改联系方式的联系人姓名
嬴政
更改嬴政的电话号码为
01078698763
5
请输入要更改联系方式的联系人姓名
赵匡胤
更改赵匡胤的电话号码为
02178657943
4
赵匡胤 : 02178657943
朱元璋 : 18876245603
嬴政 : 01078698763
爱新觉罗努尔哈赤 : 13567852435
2
请输入要删除的联系人姓名
嬴政
4
赵匡胤 : 02178657943
朱元璋 : 18876245603
爱新觉罗努尔哈赤 : 13567852435
5
请输入要更改联系方式的联系人姓名
李世民
未找到联系人李世民
6
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[进程已完成]
页:
[1]