我这个链表该怎么改
#include<iostream>#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<fstream>
#include<windows.h>
#include<malloc.h>
using namespace std;
typedef struct List//链表结构体
{
string name;
string street;
string city;
string state;
string phone;
struct List *next;
}list,*linklist;
void creatlist(linklist &p);
void insertlist(linklist &p, list *q);
linklist search(linklist p);
void del(linklist &p,linklist s);
void tou();
void print(linklist p);
void xianshi(linklist p);
void tianjiaxinxi(linklist &p);
void shanchu(linklist &p);
void gai(linklist &p);
void savelist(linklist p);
void loadlist(linklist &p);
void menu();
int main()
{
linklist l=NULL;
int choice;//自己输入的选项
int sum=7;
while(choice!=sum)//建立循环使菜单能重复出现并实现退出功能
{
menu();
cin>>choice;
switch(choice)//判断用户选项并做以下内容
{
case 1:
{
tianjiaxinxi(l);
break;
}
case 2:
{
shanchu(l);
break;
}
case 3:
{
linklist d=search(l);
system("cls");
if(d)
{
system("cls");
tou();
print(d);
Sleep(1500);
}
else
{
system("cls");
cout<<"查无此人!"<<endl;
Sleep(1500);
}
break;
}
case 4:
tou();
xianshi(l);
system("pause");
break;
case 5:
gai(l);
break;
case 6:
loadlist(l);
break;
case 7:
savelist(l);
break;
}
}
return 0;
}
void creatlist(linklist &p)//链表初始化
{
//申请头结点
p=new list;
p->next=NULL;
}
void insertlist(linklist &p, list *q)//插入
{
list *a=NULL;
a=p;
q->next=p->next;
p->next=q;
}
linklist search(linklist p)//查找信息
{
string name;
cout<<"请输入你想要查找的名字:"<<endl;
cin>>name;
linklist a=p->next;
while(a)
{
if(a->name==name)
break;
else
a=a->next;
}
return a;
}
void del(linklist &p,linklist s)//删除信息
{
linklist q,d;
q=p->next;
if(q!=s)
{
d=q;
q=q->next;
}
else
{
d->next=q->next;
delete q;
cout<<"删除成功!"<<endl;
}
}
void tou()
{
cout<<"姓名"<<"\t";
cout<<"城市"<<"\t";
cout<<"电话"<<"\t\t\t";
cout<<"国家"<<"\t";
cout<<"街道"<<endl;
}
void print(linklist p)//显示一条信息
{
cout<<p->name<<"\t";
cout<<p->city<<"\t";
cout<<p->phone<<"\t\t\t";
cout<<p->state<<"\t";
cout<<p->street<<"\t"<<endl;
}
void xianshi(linklist p)//显示信息
{
if(!p)
{
cout<<"无通讯人信息!"<<endl;
}
else
{
linklist j=p->next;
while(j)
{
print(j);
j=j->next;
}
}
}
void tianjiaxinxi(linklist &p)//添加信息
{
list *L=new list;//分配新的空间
system("cls");
while(true)//名字
{
cout<<"请输入名字"<<endl;
cin>>L->name;
int shuzi;
cout<<"是否保存?(保存请按“1”)(取消请按“2”)"<<endl;
cin>>shuzi;
if(shuzi==1)
{
insertlist(p,L);
cout<<"保存成功!"<<endl;
Sleep(1000);
break;
}
else if(shuzi==2)
{
cout<<"重新输入!"<<endl;
}
else if(shuzi!=1||shuzi!=2)
{
cout<<"请输入有效数字!"<<endl;
}
}
system("cls");
while(true)//城市
{
cout<<"请输入城市"<<endl;
cin>>L->city;
int shuzi;
cout<<"是否保存?(保存请按“1”)(取消请按“2”)"<<endl;
cin>>shuzi;
if(shuzi==1)
{
insertlist(p,L);
cout<<"保存成功!"<<endl;
Sleep(1000);
break;
}
else if(shuzi==2)
{
cout<<"重新输入!"<<endl;
}
else if(shuzi!=1||shuzi!=2)
{
cout<<"请输入有效数字!"<<endl;
}
}
system("cls");
while(true)//国家
{
cout<<"请输入国家"<<endl;
cin>>L->state;
int shuzi;
cout<<"是否保存?(保存请按“1”)(取消请按“2”)"<<endl;
cin>>shuzi;
if(shuzi==1)
{
insertlist(p,L);
cout<<"保存成功!"<<endl;
Sleep(1000);
break;
}
else if(shuzi==2)
{
cout<<"重新输入!"<<endl;
}
else if(shuzi!=1||shuzi!=2)
{
cout<<"请输入有效数字!"<<endl;
}
}
system("cls");
while(true)//街道
{
cout<<"请输入街道"<<endl;
cin>>L->street;
int shuzi;
cout<<"是否保存?(保存请按“1”)(取消请按“2”)"<<endl;
cin>>shuzi;
if(shuzi==1)
{
insertlist(p,L);
cout<<"保存成功!"<<endl;
Sleep(1000);
break;
}
else if(shuzi==2)
{
cout<<"重新输入!"<<endl;
}
else if(shuzi!=1||shuzi!=2)
{
cout<<"请输入有效数字!"<<endl;
}
}
system("cls");
while(true)//电话
{
cout<<"请输入电话"<<endl;
cin>>L->phone;
int shuzi;
cout<<"是否保存?(保存请按“1”)(取消请按“2”)"<<endl;
cin>>shuzi;
if(shuzi==1)
{
insertlist(p,L);
cout<<"保存成功!"<<endl;
Sleep(1000);
break;
}
else if(shuzi==2)
{
cout<<"重新输入!"<<endl;
}
else if(shuzi!=1||shuzi!=2)
{
cout<<"请输入有效数字!"<<endl;
}
}
system("cls");
}
void shanchu(linklist &p)//删除
{
string name;
linklist l;
cout<<"请输入你要删除的名字:";
cin>>name;
linklist a=p->next;
del(p,a);
system("pause");
}
void gai(linklist &p)//修改
{
system("cls");
linklist b=search(p);
cout<<"需要修改的名字:";
cin>>b->name;
cout<<"需要修改的街道:";
cin>>b->street;
cout<<"需要修改的城市:";
cin>>b->city;
cout<<"需要修改的国家;";
cin>>b->state;
cout<<"需要修改的电话:";
cin>>b->phone;
cout<<"修改成功!";
Sleep(2000);
}
void savelist(linklist p)//保存文件
{
linklist j=p->next;
FILE *fp=NULL;
if((fp=fopen("通讯信息.txt","wb"))==NULL)
{
cout<<"无文件内容";
exit(1);//异常退出
}
fp=fopen("通讯信息.txt","wb");
while(j)
{
fprintf(fp,"%s %s %s %s %s ",j->name.c_str(),j->city.c_str(),j->phone.c_str(),j->state.c_str(),j->street.c_str());
j=j->next;
}
fclose(fp);
cout << "已保存到文件!" << endl<<"欢迎下次使用!"<<endl;
}
void loadlist(linklist &p)
{
FILE *fp=NULL;
if ((fp=(fopen("通讯信息.txt", "rb"))) == NULL)
{
cout << "找不到文件!" << endl;
Sleep(3000);
}
fp=fopen("通讯信息.txt", "rb");
linklist k=NULL;
while(1)
{
k=new list;
if (fread(k, sizeof(list), 1, fp) > 0)
{
insertlist(p,k);
}
else
{
break;
}
}
fclose(fp);
cout<<"加载中....."<< endl;
Sleep(1000);
cout<<"加载成功!"<<endl;
}
void menu()//菜单
{
cout<<"***********************************"<<endl;
cout<<" 通讯管理系统"<<endl;
cout<<" 1.输入通讯人信息"<<endl;
cout<<" 2.删除通讯人信息"<<endl;
cout<<" 3.查找通讯人信息"<<endl;
cout<<" 4.显示通讯人信息"<<endl;
cout<<" 5.修改通讯信息"<<endl;
cout<<" 6.加载文件"<<endl;
cout<<" 7.保存文件并退出"<<endl;
cout<<"***********************************"<<endl;
cout<<"请选择:";
} C++? 傻眼貓咪 发表于 2022-3-27 17:23
C++?
是的
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<fstream>
#include<windows.h>
#include<malloc.h>
using namespace std;
typedef struct List//链表结构体
{
string name;
string street;
string city;
string state;
string phone;
struct List* next;
}list, * linklist;
void creatlist(linklist& p);
void insertlist(linklist& p, list* q);
linklist search(linklist p);
void del(linklist& p, linklist s);
void tou();
void print(linklist p);
void xianshi(linklist p);
void tianjiaxinxi(linklist& p);
void shanchu(linklist& p);
void gai(linklist& p);
void savelist(linklist p);
void loadlist(linklist& p);
void menu();
int main()
{
linklist l = NULL;
int choice;//自己输入的选项
int sum = 7;
while (choice != sum) // <------------------------------------------------------你的 chooice 未初始值,只是声明而已,没有任何有效值
{
menu();
cin >> choice;
switch (choice)//判断用户选项并做以下内容
{
case 1:
{
tianjiaxinxi(l);
break;
}
case 2:
{
shanchu(l);
break;
}
case 3:
{
linklist d = search(l);
system("cls");
if (d)
{
system("cls");
tou();
print(d);
Sleep(1500);
}
else
{
system("cls");
cout << "查无此人!" << endl;
Sleep(1500);
}
break;
}
case 4:
tou();
xianshi(l);
system("pause");
break;
case 5:
gai(l);
break;
case 6:
loadlist(l);
break;
case 7:
savelist(l);
break;
}
}
return 0;
}
void creatlist(linklist& p)//链表初始化
{
//申请头结点
p = new list;
p->next = NULL;
}
void insertlist(linklist& p, list* q)//插入
{
list* a = NULL;
a = p;
q->next = p->next;
p->next = q;
}
linklist search(linklist p)//查找信息
{
string name;
cout << "请输入你想要查找的名字:" << endl;
cin >> name;
linklist a = p->next;
while (a)
{
if (a->name == name)
break;
else
a = a->next;
}
return a;
}
void del(linklist& p, linklist s)//删除信息
{
linklist q, d;
q = p->next;
if (q != s)
{
d = q;
q = q->next;
}
else
{
d->next = q->next; // <-------------------------------- 注意你的 d 没有分配空间,不能用 next,只能用来指向地址
delete q;
cout << "删除成功!" << endl;
}
}
void tou()
{
cout << "姓名" << "\t";
cout << "城市" << "\t";
cout << "电话" << "\t\t\t";
cout << "国家" << "\t";
cout << "街道" << endl;
}
void print(linklist p)//显示一条信息
{
cout << p->name << "\t";
cout << p->city << "\t";
cout << p->phone << "\t\t\t";
cout << p->state << "\t";
cout << p->street << "\t" << endl;
}
void xianshi(linklist p)//显示信息
{
if (!p)
{
cout << "无通讯人信息!" << endl;
}
else
{
linklist j = p->next;
while (j)
{
print(j);
j = j->next;
}
}
}
void tianjiaxinxi(linklist& p)//添加信息
{
list* L = new list;//分配新的空间
system("cls");
while (true)//名字
{
cout << "请输入名字" << endl;
cin >> L->name;
int shuzi;
cout << "是否保存?(保存请按“1”)(取消请按“2”)" << endl;
cin >> shuzi;
if (shuzi == 1)
{
insertlist(p, L);
cout << "保存成功!" << endl;
Sleep(1000);
break;
}
else if (shuzi == 2)
{
cout << "重新输入!" << endl;
}
else if (shuzi != 1 || shuzi != 2)
{
cout << "请输入有效数字!" << endl;
}
}
system("cls");
while (true)//城市
{
cout << "请输入城市" << endl;
cin >> L->city;
int shuzi;
cout << "是否保存?(保存请按“1”)(取消请按“2”)" << endl;
cin >> shuzi;
if (shuzi == 1)
{
insertlist(p, L);
cout << "保存成功!" << endl;
Sleep(1000);
break;
}
else if (shuzi == 2)
{
cout << "重新输入!" << endl;
}
else if (shuzi != 1 || shuzi != 2)
{
cout << "请输入有效数字!" << endl;
}
}
system("cls");
while (true)//国家
{
cout << "请输入国家" << endl;
cin >> L->state;
int shuzi;
cout << "是否保存?(保存请按“1”)(取消请按“2”)" << endl;
cin >> shuzi;
if (shuzi == 1)
{
insertlist(p, L);
cout << "保存成功!" << endl;
Sleep(1000);
break;
}
else if (shuzi == 2)
{
cout << "重新输入!" << endl;
}
else if (shuzi != 1 || shuzi != 2)
{
cout << "请输入有效数字!" << endl;
}
}
system("cls");
while (true)//街道
{
cout << "请输入街道" << endl;
cin >> L->street;
int shuzi;
cout << "是否保存?(保存请按“1”)(取消请按“2”)" << endl;
cin >> shuzi;
if (shuzi == 1)
{
insertlist(p, L);
cout << "保存成功!" << endl;
Sleep(1000);
break;
}
else if (shuzi == 2)
{
cout << "重新输入!" << endl;
}
else if (shuzi != 1 || shuzi != 2)
{
cout << "请输入有效数字!" << endl;
}
}
system("cls");
while (true)//电话
{
cout << "请输入电话" << endl;
cin >> L->phone;
int shuzi;
cout << "是否保存?(保存请按“1”)(取消请按“2”)" << endl;
cin >> shuzi;
if (shuzi == 1)
{
insertlist(p, L);
cout << "保存成功!" << endl;
Sleep(1000);
break;
}
else if (shuzi == 2)
{
cout << "重新输入!" << endl;
}
else if (shuzi != 1 || shuzi != 2)
{
cout << "请输入有效数字!" << endl;
}
}
system("cls");
}
void shanchu(linklist& p)//删除
{
string name;
linklist l;
cout << "请输入你要删除的名字:";
cin >> name;
linklist a = p->next;
del(p, a);
system("pause");
}
void gai(linklist& p)//修改
{
system("cls");
linklist b = search(p);
cout << "需要修改的名字:";
cin >> b->name;
cout << "需要修改的街道:";
cin >> b->street;
cout << "需要修改的城市:";
cin >> b->city;
cout << "需要修改的国家;";
cin >> b->state;
cout << "需要修改的电话:";
cin >> b->phone;
cout << "修改成功!";
Sleep(2000);
}
void savelist(linklist p)//保存文件
{
linklist j = p->next;
FILE* fp = NULL;
if ((fp = fopen("通讯信息.txt", "wb")) == NULL)
{
cout << "无文件内容";
exit(1);//异常退出
}
fp = fopen("通讯信息.txt", "wb");
while (j)
{
fprintf(fp, "%s %s %s %s %s ", j->name.c_str(), j->city.c_str(), j->phone.c_str(), j->state.c_str(), j->street.c_str());
j = j->next;
}
fclose(fp);
cout << "已保存到文件!" << endl << "欢迎下次使用!" << endl;
}
void loadlist(linklist& p)
{
FILE* fp = NULL;
if ((fp = (fopen("通讯信息.txt", "rb"))) == NULL)
{
cout << "找不到文件!" << endl;
Sleep(3000);
}
fp = fopen("通讯信息.txt", "rb");
linklist k = NULL;
while (1)
{
k = new list;
if (fread(k, sizeof(list), 1, fp) > 0)
{
insertlist(p, k);
}
else
{
break;
}
}
fclose(fp);
cout << "加载中....." << endl;
Sleep(1000);
cout << "加载成功!" << endl;
}
void menu()//菜单
{
cout << "***********************************" << endl;
cout << " 通讯管理系统" << endl;
cout << " 1.输入通讯人信息" << endl;
cout << " 2.删除通讯人信息" << endl;
cout << " 3.查找通讯人信息" << endl;
cout << " 4.显示通讯人信息" << endl;
cout << " 5.修改通讯信息" << endl;
cout << " 6.加载文件" << endl;
cout << " 7.保存文件并退出" << endl;
cout << "***********************************" << endl;
cout << "请选择:";
} 傻眼貓咪 发表于 2022-3-27 17:47
这个我试过了,输入信息时不能保存在链表
闲时 发表于 2022-3-27 19:06
这个我试过了,输入信息时不能保存在链表
每次输入新数据之前,tianjiaxinxi 这里要 new 分配内存空间这是没错,但你在初始化时 creatlist 已经分配一次,却没有用?初始化只需指向 NULL 便可,不用分配空间。 傻眼貓咪 发表于 2022-3-27 20:31
每次输入新数据之前,tianjiaxinxi 这里要 new 分配内存空间这是没错,但你在初始化时 creatlist 已经分 ...
你运行一下看看那个输入信息,随便输几个数据后它就不输入了QAQ
页:
[1]