一. 问题描述:
李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。并制定了初步要求:
(1)每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。
(2)作为一个完整的系统,应具有友好的界面和较强的容错能力。
二. 问题分析:
(1)实现双链表的插入,查询以及删除,处理好结点之间指针的关系
(2)信息的录入
(3)界面清晰明了,双链表有一定得容错能力
三. 逻辑结构和存储结构设计
逻辑结构:线性表
存储结构:双链表
四. 算法设计
蛮力法
五. 时间复杂度和空间复杂度分析
蛮力法:O(n)
六. 源代码
头文件Person.h:
#ifndef PERSON_H
#define PERSON_H
#include <string>
using std::string;
struct Person //定义个人
{
string Name;
string Sex;
string Number;
Person *pre, *next;
Person(string, string, string);
Person(string, string, string, bool);
};
#endif
头文件PHONE_SYSTEM:
#ifndef PHONE_SYSTEM
#define PHONE_SYSTEM
struct Person;
class Phone_System
{
public:
Phone_System();
void Build();
void Watch();
void Search_Cut();
void Search_Alter();
private:
Person* Scan();
Person* Search();
void Cut(Person*);
void Alter(Person*);
Person *First;
Person *Rear;
};
#endif
源文件:
#include <iostream>
#include "Phone_System.h"
#include "Person.h"
using namespace std;
void main()
{
Phone_System ps; //建立系统对象
unsigned choice = 0; //给选择初始化负值
while (true)
{
system("cls"); //清屏
cout << "\t* ------------------------------------ *\n";
cout << "\t* 学院:信息学院 班级:信管1133班 *\n";
cout << "\t* 学号:201311671332 *\n";
cout << "\t* 姓名:朱宇鹏 *\n";
cout << "\t* __A___A______A_______A______A___ *\n";
cout << "\t* ---v----V-----V--Y----v---Y----- *\n";
cout << "\t* 欢迎使用 *\n";
cout << "\t* 学生通讯录管理系统 *\n";
cout << "\t* ------------------------------------ *\n";
cout << "\t* --------------0:退出--------------- *\n";
cout << "\t* --------------1:新建--------------- *\n";
cout << "\t* --------------2:删除--------------- *\n";
cout << "\t* --------------3:查询--------------- *\n";
cout << "\t* --------------4:修改--------------- *\n";
cout << "\t* ------------------------------------ *\n";
cout << "请输入你的选择:" << ends;
cin >> choice; //输入选择
switch (choice)
{
case 0:exit(-1); break;
case 1:ps.Build(); break;
case 2:ps.Search_Cut(); break;
case 3:ps.Watch(); break;
case 4:ps.Search_Alter(); break;
default:
cout << "非法输入!" << endl;
system("pause"); //任意键返回
break;
}
}
}
Person::Person
(string name, string sex, string number) :
Name(name), Sex(sex), Number(number), pre(nullptr), next(nullptr) //初始化
{
system("cls");
cout << "新建成功" << endl;
cout << "姓名:" << Name << endl;
cout << "号码:" << Number << endl;
system("pause");
}
Person::Person //构造函数
(string name, string sex, string number, bool) :
Name(name), Sex(sex), Number(number), pre(nullptr), next(nullptr){}
Phone_System::Phone_System() : //建立头尾结点的联系
First(nullptr), Rear(nullptr)
{
First = new Person("#", "#", "#", true);
Rear = new Person("#", "#", "#", true);
First->next = Rear; //头节点的后继指针指向尾结点
Rear->pre = First; //尾节点的前驱指针指向头结点
system("cls");
cout << "系统启动成功" << endl;
system("pause");
system("cls");
}
Person* Phone_System::Scan()
{
system("cls");
string name;
cout << "请输入姓名:" << ends;
cin >> name;
unsigned sex = 0;
string sexual;
while (sex != 1 && sex != 2)
{
cout << "请选择性别:1:女,2:男" << ends;
cin >> sex;
if (sex == 1)
sexual = "女";
else sexual = "男";
}
string number;
cout << "请输入电话号码:" << ends;
cin >> number;
Person *a = new Person(name, sexual, number); //输入新的学生信息
return a;
}
void Phone_System::Build()
{
Person *p = nullptr; //设置P为空指针(安全)
while (true)
{
system("cls");
cout << "是否继续录入:Y/N" << endl;
char chosse = 'Y';
cin >> chosse;
if (chosse == 'n' || chosse == 'N')
break;
p = Scan(); //录入信息于p
p->next = First->next; //将p放于First后,建立连接
First->next = p;
p->next->pre = p;
p->pre = First;
}
}
Person* Phone_System::Search()
{
cout << "1.名字搜索" << endl;
cout << "2.号码搜索" << endl;
cout << "请输入查询方式:" << ends;
unsigned choice = 0;
cin >> choice;
Person *p = nullptr;
system("cls");
if (choice == 1)
{
string name;
cout << "请输入姓名:" << ends;
cin >> name;
p = First->next; //从头结点开始
while (p != nullptr&&p->Name != name) //当p不为空并且不是输入名字
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
else
{
string number;
cout << "请输入账号:" << ends;
cin >> number;
p = First->next;
while (p != nullptr&&p->Number != number)
p = p->next;
if (p == nullptr)
{
cout << "无该用户!" << endl;
system("pause");
}
}
return p;
}
void Phone_System::Watch()
{
system("cls");
cout << "请按照提示进行查询操作" << endl;
Person *p = Search();
if (p != nullptr)
{
system("cls");
cout << "账户信息:" << endl;
cout << "姓名:" << p->Name << endl;
cout << "号码:" << p->Number << endl;
system("pause");
}
}
void Phone_System::Cut(Person *a)
{
a->next->pre = a->pre; //a后一位的前指针域指向a的前一位
a->pre->next = a->next; //a前一位的后指针域指向a的后一位
delete a;
cout << "删除成功!" << endl;
system("pause");
}
void Phone_System::Search_Cut()
{
system("cls");
cout << "请按照提示进行删除前的查询操作" << endl;
Person *p = Search();
if (p != nullptr)
Cut(p);
else
{
system("cls");
cout << "删除失败!" << endl;
system("pause");
}
}
void Phone_System::Alter(Person *p)
{
cout << "1:姓名" << endl;
cout << "2:性别" << endl;
cout << "3:号码" << endl;
cout << "请选择要修改的项:" << ends;
unsigned choice;
cin >> choice;
unsigned sex = 0;
switch (choice)
{
case 1:
cout << "请输入:" << ends;
cin >> p->Name; break;
case 2:
while (sex != 1 && sex != 2)
{
cout << "请选择性别:1:女,2:男" << ends;
cin >> sex;
if (sex == 1)
p->Sex = "女";
else p->Sex = "男";
}break;
case 3:
cout << "请输入:" << ends; //endl多了个换行
cin >> p->Number; break;
default:cout << "非法操作" << endl;
}
}
void Phone_System::Search_Alter()
{
system("cls");
cout << "请按照提示进行修改前的查询操作" << endl;
Person *p = Search();
if (p != nullptr)
Alter(p);
else
{
system("cls");
cout << "修改失败!" << endl;
system("pause");
}
}
七. 程序运行结果
八·心得:
一开始抱着很不自信的心态来写的,因为我一直觉得数据结构很难,会这些东西都是程序员才做的出来的。看着课程设计的日子越来越近我也等不住了就开始上网学,请教同学来进行编程。
没想到比我想象中的容易多了,其实也是,只要肯学没什么是学不会的。之前我一直都不知道双链表中的First是个什么东西也没有想要去学会它,只是在想考试不要考到就好了。后面才发现这样的学习态度是不行的,不懂就放弃是什么机会都没有的。
其实我也很感谢我的朋友们,在我什么都不懂的的时候都在耐心的教我,尽管他们也有他们的设计他们的事。相信不久的将来我的编程技术会更上一层楼,和我们班主任说的,大学先把自己的专业学好,以后的路就自己会变好的。
参考文献:《数据结构》