#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//定义一个通讯录里面的信息
typedef struct People
{
char name[20];
char gender[5];
int phone;
char city[30];
int elp;
}People;
//节点
typedef struct Node
{
People people;//数据域
struct Node* pNext;//指针域
}BiTNode,*Node ;
//----------------------------------------------------声明函数-----------------------------------------------
//显示英文提示选单
void Menu();
//退出选单
int Quit();
//创建新的通讯录(初始化链表)
Node create();
//添加
Node add();
//查找
void Find();
//修改
void Alter();
//删除
void Delete();
//显示所有的记录(打印链表)
void List();
//---------------------------------------------开始实施功能-------------------------------------------------------------------------------------
//主函数根据选单的选项调用函数
int main()
{ Node head =0;
Node s;
int a;
int op;
head=create(head);
s=head;
a=1;
Menu();
while(a){
printf("请选择功能\n");
scanf("%d",&op);
switch(op)
{
case 1://添加
s=add(s);
getchar();
system("cls");
Menu();
break;
case 2://打印
List(head);
Menu();
break;
case 3://查找
Find(head);
getchar();
system("cls");
Menu();
break;
case 4://修改
Alter(head);
getchar();
system("cls");
Menu();
break;
case 5://删除
Delete(head);
getchar();
system("cls");
Menu();
break;
case 0://退出
a=Quit();
}
}
return 0;
}
//Menu()函数功能
void Menu(){
printf("******************************************************\n");
printf("* 苏苏王专业通讯录 *\n ");
printf("*****************************************************\n");
printf("* 请选择你需要的功能 *\n");
printf("*****************************************************\n");
printf("*\t\t1.添加信息\t\t\t *\n");
printf("*\t\t2.打印信息\t\t\t *\n");
printf("*\t\t3.查找信息\t\t\t *\n");
printf("*\t\t4.修改信息\t\t\t *\n");
printf("*\t\t5.删除信息\t\t\t *\n");
printf("*\t\t0.退出系统\t\t\t *\n");
printf("*****************************************************\n");
}
//链表的初始化
Node create(Node head)
{
head = (Node)malloc(sizeof(BiTNode));
(head)->people.elp=0;
(head)->people.phone=0;
strcpy((head)->people.city,"NULL");
strcpy((head)->people.name,"NULL");
strcpy((head)->people.gender,"NULL");
return head;
}
//退出功能
int Quit()
{
return 0;
}
//添加功能
Node add(Node s)
{
Node p;
p = (Node)malloc(sizeof(BiTNode));
s->pNext=p;
p->pNext=NULL;
s=p;
printf("请输入名字:");
scanf("%s",p->people.name);
printf("");
printf("请输入性别:");
scanf("%s",p->people.gender);
printf("");
printf("请输入城市:");
scanf("%s",p->people.city);
printf("");
printf("请输入邮编:");
scanf("%d",&p->people.elp);
printf("");
printf("请输入电话号:");
scanf("%d",&p->people.phone);
printf("添加成功!!!!!");
return s;
}
//打印功能
void List(Node head){
Node p;
if(head->pNext!=NULL){
p=head->pNext;
while(p!=NULL)
{
printf("\t%s\t%s\t%d\t%s\t%d\n",
p->people.name,
p->people.gender,
p->people.phone,
p->people.city,
p->people.elp);
p=p->pNext;
}
}
if(head->pNext==NULL){
printf("通讯录未有联系人");}
}
//查找功能(按照手机号查找)
void Find(Node head)
{
Node p;
int phone;
printf("请输入联系人的电话号码:");
scanf("%d",&phone);
//遍历链表
p=head;
while(p!=NULL)
{
if(p->people.phone==phone)
{
printf("找到了该联系人");
printf("\t%s\t%s\t%d\t%s\t%d\n",
p->people.name,
p->people.gender,
p->people.phone,
p->people.city,
p->people.elp);
break;
}
else
{
p=p->pNext;
}
}
if(p==NULL)
{printf("未寻找到该联系人");}
}
//修改
void Alter(Node head)
{
Node p;
int phone;
printf("请输入联系人的电话号码");
scanf("%d",&phone);
//遍历链表
p=head;
while(p!=NULL)
{
if(p->people.phone==phone)
{
printf("请输入名字:");
scanf("%s",p->people.name);
printf("");
printf("请输入性别:");
scanf("%s",p->people.gender);
printf("");
printf("请输入城市:");
scanf("%s",p->people.city);
printf("");
printf("请输入邮编:");
scanf("%d",&p->people.elp);
printf("");
printf("请输入电话号:");
scanf("%d",&p->people.phone);
printf("修改成功\n");
break;
}
else
{
p=p->pNext;
}
}
if(p==NULL)
{printf("未寻找到该联系人");}
}
//删除
void Delete(Node head)
{
Node p;
int phone;
printf("请输入联系人的电话号码:");
scanf("%d",&phone);
p=head->pNext;
//遍历链表
//判断是不是头节点
//是头节点
if(head->pNext->people.phone==phone)
{
head->pNext=p->pNext;
free(p);
}
//不是头节点
while(p->pNext!=NULL){
if(p->pNext->people.phone==phone)
{
p->pNext=p->pNext->pNext;
free(p->pNext);
}
else
{
p=p->pNext;
}
}
printf("删除成功");
}
这里问题主要有两个地方就是在打印的时候如果只有head节点的时候它打印就会报错而且无法打印那个printf打印函数里面的东西
然后删除功能也有问题说的是内存溢出,我想再加一个把链表送到文件里面然后重新加载的时候读文件不知道怎么办