【c语言】写了一个简单的通讯录程序
刚学习完单链表,赶紧写了一个通讯录小程序练练手!光看不练是很难提升的,自己真正动起手来才会发现许多问题。这次练习的过程中忽略了刚进入循环时的条件和逻辑符号的短路求值特性。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1024
void addPerson(struct Person **head);
void getInput(struct Person *person);
void printfPerson(struct Person *person);
void findPerson(struct Person *head);
void changePerson(struct Person **head);
void delPerson(struct Person **head);
void displayContent(struct Person *head);
void releasePool(void);
struct Person *pool = NULL;
int count;
struct Person
{
char name[40];
char phone[44];
struct Person *next;
};
void getInput(struct Person *person)
{
printf("请输入联系人姓名:");
scanf("%s" , &person->name);
printf("请输入联系人电话:");
scanf("%s" , &person->phone);
}
void printfPerson(struct Person *person)
{
printf("联系人姓名:%s\n" , person->name);
printf("联系人电话:%s\n" , person->phone);
}
void addPerson(struct Person **head)
{
struct Person *_new;
static struct Person *tail;
if (pool != NULL)
{
_new = pool;
pool = pool->next;
count--;
}
else
{
_new = (struct Person *)malloc(sizeof(struct Person));
if(_new == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
}
getInput(_new);
if(*head != NULL)
{
tail->next = _new;
_new->next = NULL;
}
else
{
*head = _new;
_new->next = NULL;
}
tail = _new;
printf("录入成功!\n\n");
}
void findPerson(struct Person *head)
{
struct Person *person;
person = head;
char name[40];
printf("请输入联系人姓名:");
scanf("%s" , &name);
while( person != NULL && strcmp(person->name , name) )
{
person = person->next;
}
if(person == NULL)
{
printf("找不到该联系人!\n\n");
}
else
{
printfPerson(person);
putchar('\n');
}
}
void changePerson(struct Person **head)
{
char name[40];
struct Person *person;
printf("请输入要查找的联系人姓名:");
scanf("%s" , &name);
person = *head;
while( person != NULL && strcmp(person->name , name) )
{
person = person->next;
}
if(person == NULL)
{
printf("找不到该联系人!\n\n");
return;
}
else
{
printf("请输入新的联系电话:" );
scanf("%s" , &person->phone);
printf("修改成功!\n\n");
}
}
void delPerson(struct Person **head)
{
struct Person *previous;
struct Person *temp;
struct Person *person;
char name[40] ;
char judge;
previous = NULL;
person = *head;
printf("请输入要删除的联系人姓名:");
scanf("%s" , &name);
while( person != NULL && strcmp(person->name , name) )
{
previous = person;
person = person->next;
}
if(person == NULL)
{
printf("该联系人不存在!\n\n");
return;
}
else
{
getchar();
printf("确认删除该联系人吗?(Y/N):");
scanf("%c" , &judge);
if(judge == 'Y')
{
if(previous == NULL)
{
*head = person->next;
}
else
{
previous->next = person->next;
}
printf("删除成功!\n\n");
if (count < MAX)
{
if (pool != NULL)
{
temp = pool;
pool = person;
person->next = temp;
}
else
{
pool = person;
person->next = NULL;
}
count++;
}
else
{
free(person);
}
}
}
}
void displayContent(struct Person *head)
{
struct Person *person;
person = head;
if(person == NULL)
{
printf("通讯录为空!\n");
}
else
{
while(person != NULL)
{
printfPerson(person);
person = person->next;
}
putchar('\n');
}
}
void releaseContacts(struct Person **contacts)
{
struct Person *temp;
while(*contacts != NULL)
{
temp = *contacts;
*contacts = (*contacts)->next;
free(temp);
}
}
void releasePool(void)
{
struct Person *temp;
while(pool != NULL)
{
temp = pool;
pool = pool->next;
free(temp);
}
}
int main()
{
printf("============通讯录程序2.0===========\n\
|---------1.插入新的联系人---------|\n\
|---------2.查找已有联系人---------|\n\
|---------3.更改已有联系人---------|\n\
|---------4.删除已有联系人---------|\n\
|---------5.显示当前通讯录---------|\n\
|---------6.退出通讯录程序---------|\n\
|--------Powered by PencilX--------|\n\n");
struct Person *head = NULL;
while(1)
{
int order;
printf("请输入指令:");
scanf("%d" , &order);
switch(order)
{
case 1 : addPerson(&head);break;
case 2 : findPerson(head);break;
case 3 : changePerson(&head);break;
case 4 : delPerson(&head);break;
case 5 : displayContent(head);break;
case 6 : goto END;
default:{printf("请输入正确的指令!\n");putchar('\n');break;}
}
}
END:
releaseContacts(&head);
releasePool();
return 0;
}
实现效果如下:
|