数据结构程序设计
----------(2号题)
一、题目2:
李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:
(1)每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。
(2)作为一个完整的系统,应具有友好的界面和较强的容错能力。
二、代码
1.头文件
#ifndef dullist_h
#define dullist_h
#include<iostream>
#include<string>
using namespace std;
struct Contacts
{
string name;
char sex;
string address;
string postcode;
string phonenum;
string age;
string QQ;
string Wechat;
string Birthday;
};
struct Node
{
Contacts data;
Node *prior,*next;
};
class dullist
{
public:
dullist();
dullist(Contacts a[],int m);
~dullist();
void Search();
void Printlist();
void Modifylist();
void Insert();
void Delete();
private:
Node *first;
};
#endif
2.源代码
#include<iostream>
#include<string>
using namespace std;
#include "contact.h"
dullist::dullist()
{
first=new Node;
first->next=first;
first->prior=first;
}
dullist::dullist(Contacts a[],int m)
{
Node *f,*s;
first=new Node;
f=first;
for(int i=0;i<m;i++)
{
s=new Node;
s->data.name=a[i].name;
s->data.sex=a[i].sex;
s->data.address=a[i].address;
s->data.postcode=a[i].postcode;
s->data.phonenum=a[i].phonenum;
s->data.age=a[i].age;
s->data.QQ=a[i].QQ;
s->data.Wechat=a[i].Wechat;
s->data.Birthday=a[i].Birthday;
s->prior=f;
f->next=s;
f=f->next;
}
f->next=first;
first->prior=f;
}
dullist::~dullist()
{
while(first!=first)
{
Node *p=first;
first=first->next;
delete p;
}
delete first;
}
void dullist::Search()
{
if(first->next==first)
cout<<"不存在信息."<<endl;
else
{
cout<<"请输入查找的联系人姓名:"<<endl;
string x;
cin >> x;
Node *p=first->next;
int count=1;
while(p->data.name != first->data.name)
{
if (p->data.name==x)break;
p=p->next;
count++;
}
if(p->data.name==first->data.name)
cout<<"不存在该联系人信息"<<endl;
else
{
cout<<"姓名:"<<p->data.name<<endl
<<"性别:"<<p->data.sex<<endl
<<"住址:"<<p->data.address<<endl
<<"邮编:"<<p->data.postcode<<endl
<<"年龄:"<<p->data.age<<endl
<<"电话:"<<p->data.phonenum<<endl
<<" QQ:"<<p->data.QQ<<endl
<<"Wechat:"<<p->data.Wechat<<endl
<<"生日:"<<p->data.Birthday<<endl;
}
}
}
void dullist::Printlist()
{
if(first->next==first)
cout<<"不存在该联系人信息"<<endl;
else
{
cout<<"输出联系人信息:"<<endl;
Node *q=first->next;
int count = 1;
while(q!=first)
{
cout<<count<<".";
cout<<"姓名:"<<q->data.name<<endl
<<"性别:"<<q->data.sex<<endl
<<"住址:"<<q->data.address<<endl
<<"邮编:"<<q->data.postcode<<endl
<<"年龄:"<<q->data.age<<endl
<<"电话:"<<q->data.phonenum<<endl
<<" QQ:"<<q->data.QQ<<endl
<<"Wechat:"<<q->data.Wechat<<endl
<<"生日:"<<q->data.Birthday<<endl;
q=q->next;
++count;
}
}
}
void dullist::Modifylist()
{
int m,count;
cout << "修改第几个联系人信息?"<<endl;
cin>>m;
Contacts temp;
Node *p=first->next;
count =1;
while(p!=first && count < m)
{
p=p->next;
count++;
}
if(p==first)
cout<<"对不起,你欲修改的信息不存在"<<endl;
else
{
cout<<"请按次序输入要修改的信息:"<<endl
<<"姓名:"<<endl;
cin>>temp.name;
p->data.name=temp.name;
cout<<"性别:"<<endl;
cin>>temp.sex;
p->data.sex=temp.sex;
cout<<"住址:"<<endl;
cin>>temp.address;
p->data.address=temp.address;
cout<<"邮编:"<<endl;
cin>>temp.postcode;
p->data.postcode=temp.postcode;
cout<<"年龄:"<<endl;
cin>>temp.age;
p->data.age=temp.age;
cout<<"电话:"<<endl;
cin>>temp.phonenum;
p->data.phonenum=temp.phonenum;
cout<<"QQ:"<<endl;
cin>>temp.QQ;
p->data.QQ=temp.QQ;
cout<<"Wechat:"<<endl;
cin>>temp.Wechat;
p->data.Wechat=temp.Wechat;
cout<<"Birthday:"<<endl;
cin>>temp.Birthday;
p->data.Birthday=temp.Birthday;
cout<<"信息修改成功"<<endl;
}
}
void dullist::Insert()
{
int m;
Node *s=new Node;
if
(first->next==first)
m=1;
else
{
cout<<"需要插入到哪个位置?"<<endl;
cin>>m;
}
cout<<"请按次序输入联系人信息:"<<endl;
cout<<endl<<"姓名:"<<endl;
cin>>s->data.name;
cout<<endl<<"性别:"<<endl;
cin>>s->data.sex;
cout<<endl<<"住址:"<<endl;
cin>>s->data.address;
cout<<endl<<"邮编:"<<endl;
cin>>s->data.postcode;
cout<<endl<<"年龄:"<<endl;
cin>>s->data.age;
cout<<endl<<"电话:"<<endl;
cin>>s->data.phonenum;
cout<<endl<<" QQ:"<<endl;
cin>>s->data.QQ;
cout<<endl<<"Wechat:"<<endl;
cin>>s->data.Wechat;
cout<<endl<<"Birthday:"<<endl;
cin>>s->data.Birthday;
Node *p=first;
int count=0;
while(count<m-1)
{
p=p->next;
count++;
}
s->prior=p;
s->next=p->next;
p->next->prior=s;
p->next=s;
cout<<"信息插入成功"<<endl;
}
void dullist::Delete()
{
if(first->next==first)
cout<<"信息不存在"<<endl;
else
{
int m;
cout<<"需要删除第几个联系人"<<endl;
cin>>m;
Node *q=first->next;
int count=1;
while(count<m)
{
q=q->next;
count++;
}
q->prior->next=q->next;
q->next->prior=q->prior;
delete q;
cout<<"信息删除成功"<<endl;
}
}
3.主程序
#include <iostream>
using namespace std;
#include "contact.h"
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<<" "<<endl;
cout<<"******************************************"<<endl;
}
int main()
{
dullist k;
menu();
int a;char b;
cout<<"请输入数字1-6:"<<endl;
cin >> a;
while ( a<1 || a>=6 )
{
cout<<"没有这个选项,请适当输入数字1-6,谢谢"<<endl;
cin>>a;
}
while ( a>=1 && a<=6 )
{
switch(a)
{
case 1:system("cls");k.Search();break;
case 2:system("cls");k.Printlist();break;
case 3:system("cls");k.Insert();break;
case 4:system("cls");k.Modifylist();break;
case 5:system("cls");k.Delete();break;
default:exit(0);
}
cout<<"是否返回主菜单?(Y:是/N:否)"<<endl;
cin>>b;
system("cls");
menu();
cout<<"请输入数字1-6:"<<endl;
cin >> a;
}
return 0;
}
上述编码在VC6.0中编译通过。
部分程序截图:
添加联系人:
修改通讯录联系人信息:
三、体会与心得
这个程序已经迟了一天上交了,望老师见谅。是基础不扎实的原因吧,需要比别人用更多的时间和精力去探讨怎么打代码、写程序;之后需要重新琢磨怎么完成这一道题目。
虽然过程比较困难,但学会了很多,当自己将一个个错误,慢慢地改正过来,让程序可以成功运行,就会感到有一种喜悦感。总而言之,需要多实践,光是理论懂没用,自己要真真正正地学会如何去编程,怎么找错误,才是学习的目的。理论与实践相结合,才会学得更多。
【物联网1132 黄德健】