数据结构课程设计-------双链表实现个人通讯录

一、实训目的 
通过课程设计,学会运用数据结构知识,针对具体应用,自己设计合理数据
结构,确定存储结构,并能设计具体操作算法,选择使用具体语言进行实现。掌
握 C++较复杂程序的组织和设计过程,调试技巧。学习解决实际问题的能力。
二、实训环境 

VC++6.0 环境用C++语言实现。

三、课程设计题目

李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友

信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准
备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:
(1)每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。

(2)作为一个完整的系统,应具有友好的界面和较强的容错能力。

四、功能介绍

该系统用带头结点的双链表实现,具有对个人信息的录入、查找、删除、浏览、修改的功能。

录入:用头插法把如姓名,性别,年龄,号码,住址,QQ,微信账号等输入到双链表中

查找:能通过名字查找个人信息

删除:能删除号码作废的联系人信息

浏览:能查看所有的联系人信息

修改:能对存储错误的联系人修改信息

该系统运用了菜单,使主函数更简洁。


源代码:

#include<iostream>

#include<string>

using namespace std;

struct node

{

char name[10];   //姓名

char sex[2];     //性别

char address[20];//住址

char post[6];    //邮政编码

int age;         //年龄

char number[12]; //号码

char QQ[11];     //QQ

char wechat[16]; //微信账号

int year,month,day;//出生年月日

node *prior,*next;

};

class data

{

node *first;

public:

data(){first=new node;first->next=NULL;}  //建立头结点

~data();

void menu();     //菜单

void input();    //输入联系人

void updata();    //修改联系人信息

void dele();     //删除联系人信息

void search();     //查找联系人

void show();     //浏览所有联系人

void run();      //给外部运行的一个接口函数

};

data::~data()   //析构函数

{

node* p=new node;

while(first!=NULL)

{p=first;

first=first->next;

delete p;

}

}

void data::input()          //头插法

{

node *s=new node;

cout<<"输入要保存的信息 : "<<endl;

cout<<"姓名 : ";

cin>>s->name;

cout<<"性别 : ";

cin>>s->sex;

cout<<"住址 : ";

cin>>s->address;

cout<<"邮政编码 : ";

cin>>s->post;

cout<<"年龄 : ";

cin>>s->age;

cout<<"号码 : ";

cin>>s->number;

cout<<"QQ : ";

cin>>s->QQ;

cout<<"微信账号 : ";

cin>>s->wechat;

cout<<"出生年 : ";

cin>>s->year;

cout<<"出生月 : ";

cin>>s->month;

cout<<"出生日 : ";

cin>>s->day;

if(first->next !=NULL)

{

s->prior=first;

s->next=first->next ;

first->next ->prior =s;

first->next =s;

}

else

{

first->next =s;

s->prior=first;

s->next=NULL;

}

}


void data::search()

{

char name[10];

     cout << "请输入要查找的姓名:";

     cin >> name;

node* p=new node;p=first  ;

     while (p!=NULL)

     {

          if (strcmp(p->name, name) == 0)

          {

                cout << "姓名:" << p->name <<endl;

                cout << "性别:" << p->sex <<endl;

                cout << "地址:" << p->address <<endl;

cout << "邮编:" <<p->post<<endl;

cout << "年龄:" << p->age <<endl;

                cout << "号码:" << p->number <<endl;

cout << "QQ:" << p->QQ <<endl;

cout << "微信账号:" << p->wechat <<endl;

cout << "生日:" << p->year<<","<<p->month<<","<<p->day <<endl;

                break;

          }

          else

                p = p->next;

          if (p == NULL)

              cout << "没有查找到目标" << endl;

     }

}

void data::updata()

{

char name[10];

     cout << "请输入要修改的姓名:";

     cin >> name;

node* p=new node;

p=first->next ;

     while (p!=NULL)

     {

          if (strcmp(p->name, name) == 0)

          {

char nam[10];   //姓名

char sex[2];     //性别

char address[20];//住址

char post[6];    //邮政编码

int age;         //年龄

char number[12]; //号码

char QQ[11];     //QQ

char wechat[16]; //微信账号

int year,month,day;//出生年月日

cout<<"输入修改后的信息 : "<<endl;

cout<<"姓名 : ";

cin>>nam;

cout<<"性别 : ";

cin>>sex;

cout<<"住址 : ";

cin>>address;

cout<<"邮政编码 : ";

cin>>post;

cout<<"年龄 : ";

cin>>age;

cout<<"号码 : ";

cin>>number;

cout<<"QQ : ";

cin>>QQ;

cout<<"微信账号 : ";

cin>>wechat;

cout<<"出生年 : ";

cin>>year;

cout<<"出生月 : ";

cin>>month;

cout<<"出生日 : ";

cin>>day;

strcpy(p->name,nam);

strcpy(p->sex,sex);

strcpy(p->address,address);

strcpy(p->post,post);

p->age=age;

strcpy(p->number,number);

strcpy(p->QQ,QQ);

strcpy(p->wechat,wechat);

p->year=year;

p->month=month;

p->day=day;

                break;

          }

          else

                p = p->next;

     }

if (p == NULL)

              cout << "没有查找到要修改的目标" << endl;



}

void data::show()

{

node *p=new node;

p=first->next ;

if (p  == NULL)

          cout <<"通讯录为空!" << endl;

     while (p != NULL)

{

cout<<endl;

cout << "姓名:" << p->name <<endl;

        cout << "性别:" << p->sex <<endl;

        cout << "地址:" << p->address <<endl;

cout << "邮编:" <<p->post<<endl;

cout << "年龄:" << p->age <<endl;

        cout << "号码:" << p->number <<endl;

cout << "QQ:" << p->QQ <<endl;

cout << "微信账号:" << p->wechat <<endl;

cout << "生日:" << p->year<<","<<p->month<<","<<p->day <<endl;

          p = p->next;

          cout << endl;

     }

}

void data::dele()

{

node* p=new node;

p=first->next ;

char name[10];

     cout << "请输入要删除信息的姓名:";

     cin >> name;

     while (p!=NULL)

     {

if (strcmp(p->name, name) == 0)

{

  if(p->next ==NULL )

p->prior ->next =NULL ;

cout<<"已删除"<<endl;

delete p;

break;

}

else 

{

p->prior ->next =p->next ;

p->next ->prior =p->prior ;

cout<<"已删除"<<endl;

delete p;

break;

}

}

else

p=p->next ;

}

if(p ==NULL)cout<<"没有该联系人信息,删除失败"<<endl;

}

void data::menu()

{

cout<<"***0.退出***"<<endl;

cout<<"***1.输入***"<<endl;

cout<<"***2.查找***"<<endl;

cout<<"***3.删除***"<<endl;

cout<<"***4.浏览***"<<endl;

cout<<"***5.修改***"<<endl;

}

void data::run()

{

int k;

do

{

menu();

cout<<"请输入你的选择 : ";

cin>>k;

switch(k)

{

case 0:break;

case 1:input();break;

case 2:search();break;

case 3:dele();break;

case 4:show();break;

case 5:updata();break;

default:cout<<"选择错误,重新选择。"<<endl;break;

}

}while(k);

}



int main()

{

data d;

d.run();

return 0;

}


五、程序运行图

主菜单:

数据结构课程设计-------双链表实现个人通讯录

输入操作:

数据结构课程设计-------双链表实现个人通讯录

查找操作:

数据结构课程设计-------双链表实现个人通讯录

删除操作:

数据结构课程设计-------双链表实现个人通讯录

浏览操作:

数据结构课程设计-------双链表实现个人通讯录

修改操作:

数据结构课程设计-------双链表实现个人通讯录

退出程序:

数据结构课程设计-------双链表实现个人通讯录


六、收获及不足

收获:  经过一段时间的了解,通过运用双链表实现该简单个人通讯录系统,让我明白了头指针和头结点的区别(头指针和头结点的概念清楚,现在连代码书写的不同也明白了),这使我对链表的使用更加熟练。

体会:原以为双链表会简单一点,没想到并没有自己想象的那么简单,最初把头指针及头结点的代码实现混淆了,导致无法正常运行,还好通过查找资料,弄明白了两者的区别,最后把程序写完并能够运行无错。

不足:这个系统也有不少不足之处,举一例:无法对保存的联系人信息按名字进行排序。希望随着今后不断学习,自己编程能力的提高后,能够把这个个人通讯录系统更加完善。