}
friendclassAddrList;
friendvoidadd(AddrList&addrlist);
friendvoidquery(AddrList&addrlist);
friendvoidmodify(AddrList&addrlist);
};
classAddrList
{
Node*HeadPtr;
intTag;
public:
AddrList(Node*headptr=NULL,Node*tailptr=NULL,inttag=0)
{//构造函数,创建空链表,将Tag的值置为1
HeadPtr=headptr;
Tag=tag;
}
voidAddSort(char*id,char*name,char*op,char*hp,char*mp,char*email);//将p指向的结点按Tag指定的顺序插入到链表中
Node*LookUp(char*name);//按姓名查找结点,返回该结点指针
voidDelete(char*name);//删除指定姓名的结点
intSort(int);//按tag指定的关键字重新排序
intShowAll();//显示全部结点,每10个显示一~AddrList();//释放链表空间
voidCreateList(char*filename);//从二进制文件中读入数据,构造链表
voidWriteToFile(char*filename);//将链表中数据写入指定的二制文件
voidWriteToTxtFile(char*filename);//将链表写到文本文件中
voidCreateFromTxt(char*filename);
friendvoidsort(AddrList&addrlist);//按姓名或办公室电话排序
};
voidAddrList:
:
AddSort(char*id,char*name,char*op,char*hp,char*mp,char*email)//将p指向的结点插入链表尾部{
Node*p;
p=newNode(id,name,op,hp,mp,email);
Node*x1=HeadPtr;
Node*x2=x1;
if(id)
{
if(HeadPtr)
{
while(x1=x1->Next){x2=x1;}
x2->Next=p;
p->Next=NULL;
}
elseHeadPtr=p,p->Next=NULL;
}
}
Node*AddrList:
:
LookUp(char*name)//按姓名查找结点,返回该结点指针
{
Node*x=HeadPtr;
while(x!
=NULL&&strcmp(x->Name,name)!
=0){x=x->Next;}
if(x){returnx;}
elsereturn0;
}
voidAddrList:
:
Delete(char*name)//删除指定姓名的结点
{
Node*x1=HeadPtr;
Node*x2=x1;
while(strcmp(x2->Name,name)!
=0&&x2!
=NULL){x1=x2;x2=x2->Next;}
if(x1!
=x2)
{if(x2)
{
x1->Next=x2->Next;
deletex2;
cout<<"已删除"<}
else{cout<<"未找到,请确认后再输入"<}
else{HeadPtr=x1->Next;deletex2;}
}
intAddrList:
:
Sort(intT_T)//按用户指定的关键字重新排序
{
switch(T_T)
{case1:
{
Node*x=HeadPtr,*t;
if(!
HeadPtr)return0;
intm=0,n=0;
Node*p[100]={0};
while(x){p[m]=x;m++;x=x->Next;}
n=m;
inti,j,k;
for(i=0;i{
k=i;
for(j=i+1;j{
if(strcmp(p[j]->Name,p[k]->Name)<=0)
{
k=j;
}
}
{t=p[k];p[k]=p[i];p[i]=t;}
}
HeadPtr=p[0];
i=0;
while(i{
p[i]->Next=p[i+1];
i++;
}
p[i]->Next=NULL;
Tag=1;
break;
}
case2:
{
Node*x=HeadPtr,*t;
if(!
HeadPtr)return0;
intm=0,n=0;
Node*p[100]={0};
while(x){p[m]=x;m++;x=x->Next;}
n=m;
inti,j,k;
for(i=0;i{
k=i;
for(j=i+1;j{
if(strcmp(p[j]->ID,p[k]->ID)<=0)
{
k=j;
}
}
{t=p[k];p[k]=p[i];p[i]=t;}
}
HeadPtr=p[0];
i=0;
while(i{
p[i]->Next=p[i+1];
i++;
}
p[i]->Next=NULL;
Tag=2;
break;}}
}
intAddrList:
:
ShowAll()//显示全部结点,每10个显示一屏
{
Node*x=HeadPtr;
if(x);
elsereturn0;
inti=1;
while
(1)
{
cout<while(i<=10)
{
x->Show();
i++;
if(x=x->Next);
elsebreak;
}
system("pause");
i=1;
if(x);
elsebreak;
}
}
AddrList:
:
~AddrList()//释放链表空间
{
Node*x1=HeadPtr;
Node*x2=x1;
while(x2=x2->Next)
{
deletex1;
x1=x2;
}
deletex2;
}
voidAddrList:
:
CreateList(char*filename)//从二进制文件中读入数据,构造链表
{
chara[30]={0},b[30]={0},c[30]={0},d[30]={0},e[30]={0},f[30]={0};
ifstreamin(filename,ios:
:
binary);
while(in>>a)
{
in>>b>>c>>d>>e>>f;
AddSort(a,b,c,d,e,f);
}
in.close();
}
voidAddrList:
:
CreateFromTxt(char*filename)//从文本文件中读入数据,构造链表
{
chara[30],b[30],c[30],d[30],e[30],f[30];
ifstreamin(filename);
while(in>>a)
{
in>>b>>c>>d>>e>>f;
AddSort(a,b,c,d,e,f);
}
}
voidAddrList:
:
WriteToFile(char*filename)//将链表中数据写入指定的二进制文件
{
Node*p=HeadPtr;
ofstreamout(filename,ios:
:
binary);
out<ID<<"";
out<Name<<"";
out<OfficePhone<<"";
out<HomePhone<<"";
out<MobilePhone<<"";
out<Email<<"";
while(p=p->Next)
{
out<ID<<"";
out<Name<<"";
out<OfficePhone<<"";
out<HomePhone<<"";
out<MobilePhone<<"";
out<Email<<"";
}
out.close();
}
voidAddrList:
:
WriteToTxtFile(char*filename)//将链表写到文本文件中
{
Node*x=HeadPtr;
ofstreamout(filename,ios:
:
binary);
out<out<ID<Name<OfficePhone<HomePhone<MobilePhone<Email<while(x=x->Next)
{
out<ID<Name<OfficePhone<HomePhone<MobilePhone<Email<}
out.close();
}
voidDisplayMenu()//显示菜单
{
inti;
char*menu[]={"","","","",
"***********小巧通讯录管理系统*************",
"",
"1.新增一个联系人",
"2.删除一个联系人",
"3.显示所有联系人",
"4.查找联系人",
"5.更改联系人记录",
"6.从TXT文档中增加记录",
"7.导出联系人到Txt文档",
"8.排序",
"9.退出",
"",
"******************************************",
""
"^_^欢迎使用^_^",
NULL};
system("cls");
for(i=0;menu[i];i++)
cout<