#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define OK 1
#define ERR 0
struct telbook()
{
char name[10];
char sex[2];
char adress[20];
char phonenumber[20];
struct telbook * next;//链表中的指针域
}
typedef struct telbook Node;
typedef struct telbook * Link;
void screen(); //菜单页面
void add_informantaion(Link new_node); //添加信息
void create_new_link(Link *head) //创建带头指针的链表
void creat_new_node(Link *new_node); //创建结点
void judge_node(Link new_node); //判断结点是否创建成功
Link insert_node_tail(Link new_node);//尾插
Link search_information(Link head char *phonenumber);//查找信息
deleat_information(&head phonenumber);//删除功能
void printf_node(Link *head);//打印一个节点的信息
void list_information(Link *head) //列出所有信息
int main()
{
Link head;
int choice;
Link new_node;
char phonenumber[20];
create_new_link(&head);
while(1)
{
void screen();
printf(" 请从1到6中选择 \n");
scanf("%d",&choice);
switch(choice)
{
case 1:new_node = insert_node_tail(&head);//从尾部添加信息
add_informantaion(new_node);
getch();
break;
case 2:printf("请输入要删除的号码");
scanf("%s",phonenumber);
deleat_information(&head,phonenumber);
getch();
break;
case 3:printf("请输入要修改的号码");
scanf("%s",phonenumber);
new_node = search_information(head,phonenumber);
add_informantaion(new_node);
printf("添加信息成功");
getch;
break;
case 4:printf("请输入要寻找学生的电话号码");
scanf("%s",phonenumber);
new_node = search_information(head,phonenumber);
printf_node(new_node);
break;
case 6:list_information(&head);
break;
}
}
}
void screen()
{
printf("\n");
printf("\t\t*************************\n");
printf("\t\t* 通讯录菜单 *\n");
printf("\t\t* 1.添加信息 2.删除信息 *\n");
printf("\t\t* 3.修改信息 4.查找信息 *\n");
printf("\t\t* 5.保存 6.显示信息 *\n");
printf("\t\t* 退出前勿忘保存信息 *\n");
printf("\t\t*************************\n");
}
int judge_node(Link new_node)
{
if(new_node == NULL)
return ERR;
else
{
return OK;
}
}
void creat_new_node(Link *new_node)
{
do
{
new_node = (Link)malloc(sizeof(Node));
}while(judge_node(new_node) == ERR);
}
Link insert_node_tail(Link *head)
{
Link p;
p = *head;
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
void add_informantaion(Link new_node)
{
if(new_node == NULL)
{
printf("文件出现错误!即将返回\n");
return;
}
else
{
printf("\n输入姓名:");
scanf("%s",new_node->name);
printf("\n输入性别:");
scanf("%s"new_node->sex,);
printf("\n输入住址:");
scanf("%s"new_node->adress,);
printf("\n输入手机号码:");
scanf("%s",new_node->phonenumber);
}
}
void create_new_link(Link *head)
{
*head = (Link)malloc(sizeof(Node));
*head->next = *head;
}
void deleat_information(Link *head,char * phonenumber)
{
int i = 0;
Link p,q;
p = q = *head;
while(p != NULL)
{
if(strcmp(p->phonenumber,phonenumber) == 0)
{
i = 1;
break;//找到要删除的信息,跳出循环
}
q = p;//记录当前位置
p = p->next;
}
if(i == 0)
{
printf("未找到要删除的信息");
return;
}
else
{
q->next = p->next;//删除操作
free(p);
printf("删除成功");
return;
}
}
Link search_information(Link head,char *phonenumber)
{
Link p = head;
while(p != NULL)
{
if(strcmp(p->phonenumber,phonenumber) == 0)
{
return p;
}
else
{
printf("未找到");
return NULL;
getch();
}
}
}
void printf_node(Link new_node)
{
if(new_node == NULL)
{
return;
}
else
{
printf("姓名\t性别\t住址\t联系方式\n");
printf("%s\t%s\t%s\t%s\t",new_node->name,new_node->sex,
new_node->adress,new_node->phonenumber);
return;
}
}
void list_information(Link *head)
{
Link p = *head;
if(p == NULL)
{
printf("没有学生信息!!!");
getch();
return;
}
else
{
printf("姓名\t性别\t住址\t联系方式\n");
while(p != NULL)
{
printf("%s\t%s\t%s\t%s\t",new_node->name,new_node->sex,
new_node->adress,new_node->phonenumber);
p = p->next;
}
}
}