typedef struct record
{文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
int number;//编号文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
char name[12];//姓名文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
char sex;//性别文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
char phone[12];//下面号码文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
struct record *next;//指向了下一条记录文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
}RECORD, *PRECORD;文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
PRECORD phead = NULL;//头指针文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
//添加记录文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
void AddRecord(int num, char *name, char sex, char *phone)文章源自微观生活(93wg.com)微观生活-https://93wg.com/22108.html
{
PRECORD pr = (PRECORD)malloc(sizeof(RECORD)), ptmp;
pr->number = num;
//pr->name = name;过错的数组赋值方式
strcpy(pr->name, name);//把传递进来的name指针指向的字符串拷贝到pr指向的结形成员变量中
strcpy(pr->phone, phone);
pr->sex = sex;
pr->next = NULL;
if (!phead)
phead = pr;
else
{
ptmp = phead;
while (ptmp->next)
ptmp = ptmp->next;
ptmp->next = pr;
}
}
//NULL == 0
//获取当前链表中记录的数量
unsigned NumOfRecords()
{
unsigned count = 0;
PRECORD ptmp = phead;
while (ptmp)
{
++count;
ptmp = ptmp->next;
}
return count;
}
//显示所有的记录
void ShowRecords()
{
PRECORD pr = phead;
//打印表头
printf(\"----------------------------------------------------\\n\");
printf(\"%9s%15s%10s%18s\\n\", \"编号\", \"姓名\", \"性别\", \"电话号码\");
printf(\"----------------------------------------------------\\n\");
//循环打印出所有记录
while (pr)
{
printf(\"%9d%15s%10s%18s\\n\", pr->number,
pr->name, pr->sex == \'m\' ? \"男\" : \"女\", pr->phone);
pr = pr->next;
}
//打印表尾
printf(\"----------------------------------------------------\\n\");
}
//删除了一条记录
void DelRecord(int num)
{
PRECORD pr = phead, ptmp;
if (!phead)//如果没有记录
{
printf(\"过错!通信录为空!\\n\");
return;
}
if (phead->number == num)//如果要删除了的是第一条记录
{
ptmp = phead;
phead = phead->next;
free(ptmp);
printf(\"删除了记录胜利!\\n\");
return;
}
//要删除了的记录不是第一条记录
while (pr->next)
{
if (pr->next->number == num)//下一条记录的编号等于咱们要删除了的编号
{
ptmp = pr->next;//把下一条记录的地址保留到ptmp中
pr->next = ptmp->next;//将当前指针的next指针赋为下一条的下一条记录
free(ptmp);//删除了以前咱们保留在ptmp之中的那条记录
printf(\"删除了记录胜利!\\n\");
return;
}
pr = pr->next;//让pr不断指向下一条记录
}
printf(\"没有该编号的记录!\\n\");
}
int main()
{
//自己手工输入几条记录
AddRecord({{10001:0}}, \"卢俊义\", \'m\', \"13808888888\");
AddRecord({{10002:0}}, \"潘金莲\", \'l\', \"13808899978\");
AddRecord({{10003:0}}, \"武松\", \'m\', \"13013902578\");
AddRecord({{10004:0}}, \"扈三娘\", \'l\', \"13752563288\");
AddRecord({{10005:0}}, \"西门庆\", \'m\', \"13801234567\");
//显示所有的记录
ShowRecords();
printf(\"**************共有%d条记录****************\\n\", NumOfRecords());
//删除了潘金莲的那条记录
DelRecord({{10002:0}});
//显示所有的记录
ShowRecords();
printf(\"**************共有%d条记录****************\\n\", NumOfRecords());
getchar();
return 0;
}
以上就是微观生活(93wg.com)关于“C语言——电话簿实现原理”的详细内容,希望对大家有所帮助!
评论