数据结构课程设计
来源:互联网 发布:西澳大学 知乎 编辑:程序博客网 时间:2024/05/18 23:26
一. 问题描述
李刚是一爱折腾的人,当然爱折腾的人均有梦想,他想当中国的盖次呢。可不,现在个人好友信息多了,复杂了,他想制作一个个人通讯录的制作管理软件。 刚好这个学期学了数据结构课,所以他准备使用数据结构知识来实现了。并考虑使用双向链表作数据结构。
1、每个好友信息包含姓名、性别、住址、邮编、几岁、电话、QQ、微信帐号、生日等。
2、作为一个完整的系统,应具有友好的界面和较强的容错能力。
二. 问题分析
对于通讯录,个人认为实现数据的输入、显示、插入、删除、查询的功能。程序的基本功能为:建立通讯者信息,包括姓名、编号、联系方式、生日添加新的通讯者信息;按姓名查询某个通讯者的信息;按姓名删除某个通讯者的信息;输出显示通讯录的所有信息。
在这里我选择运用双链表进行储存。双链表是在单链表的每个节点中再设置一个指向其前驱节点的指针域。双链表是一种对称结构,对于储存通讯录来说比较方便。
(1)内容:
1、通讯录内容包括:编号、姓名、联系方式、生日
(2)功能描述
1、遍历通信录。可以查看通信录中所有通信录成员的记录,该功能由函数voidPrintFromHead()实现
2、插入新联系人功能。用户可以输入以上所有内容,建立一个新的通信记录。
3、删除功能。把旧的联系人进行删除释放储存空间
4、查询功能。最重要的功能,具有姓名查询的具体功能
5、修改功能。用户可以根据需要,对任意的某个联系人的每一项信息
进行修改。
6、退出系统
三. 逻辑结构和存储结构设计
从题目的要求中,我们可以看到题目要求我使用双链表来完成这次的课程设计,因此,这次的逻辑结构为线性表。线性表是一种最基本最简单的数据结构,数据元素之间仅具有单一的前驱和后驱关系。
由于这次的题目是构造一个通信录,需要插入和删除,查询和修改等功能,因此顺序表是静态存储分配并不适合,因此在这里选取链表来克服顺序表的缺点。
链表是用一组任意的存储单元存放线性表的元素,其中,双链表是在单链表的每个结点中再设置一个指向其前驱结点的指针域。在链表中实现插入和删除操作,无需移动结点,在将工作指针指向合适的位置后,仅需修改节点之间的链接关系,所以在通信录这次的题目中使用双链表比较合理。
四. 概要算法设计
1、数据类型定义
typedef struct person//定义一个结构体类型
{
charname[15];
charsex[30];
char age[20];
charstreet[20];
char eip[20];
charphone[20];
charqq[20];
char weixin[20];
charbirth[20];
}person;
typedef struct Dulnode//双向链表结点结构类型定义
{
person *list; //值用指针形式存放
struct Dulnode *prior;
struct Dulnode *next;
}Dulnode,*Dullinklist
2、创建临时指针保留上一个节点及相应函数进行录入
Dullinklist head;
Dullinklist Enter()
{
Dullinklisth,p,s;
h=newDulnode; //头接点为空
h->prior=NULL;
h->next=NULL;
p=h;
stringstr;
cout<<"输入编号(n结束):";
cin>>str;
while(str!="n")
{
s=newDulnode;
p->next=s;
s->list=newperson;
cout<<"输入姓名:";cin>>s->list->name;
cout<<"输入性别:";cin>>s->list->sex;
cout<<"输入年龄:";cin>>s->list->age;
cout<<"输入住址:";cin>>s->list->street;
cout<<"输入邮编:";cin>>s->list->eip;
cout<<"输入电话:";cin>>s->list->phone;
cout<<"输入QQ号:";cin>>s->list->qq;
cout<<"输入微信:";cin>>s->list->weixin;
cout<<"输入生日:";cin>>s->list->birth;
cout<<endl;
s->prior=p;
s->next=NULL;
p=s;
cout<<"输入编号(n结束):";
cin>>str;
}
h->prior=s;
p->next=h;
return(h);
之后的相关操作,以此类推。
五. 时间复杂度和空间复杂度分析
和单链表类似,双链表一般也由头指针唯一确定,增加头结点也能使双链表的某些操作变得方便。这时,由书本上可知,按值查找:与输入实例有关,平均时间复杂度均为O(n),双链表上的插入和删除时间复杂度均为O (1)。空间复杂度为O(n)。
六. 源代码
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
typedef struct person//定义一个结构体类型
{
charname[15];
charsex[30];
charage[20];
charstreet[20];
char eip[20];
charphone[20];
charqq[20];
char weixin[20];
charbirth[20];
}person;
typedef struct Dulnode//双向链表结点结构类型定义
{
person *list; //值用指针形式存放
struct Dulnode *prior;
struct Dulnode *next;
}Dulnode,*Dullinklist;
Dullinklist head;//创建临时指针保留上一个节点
Dullinklist Enter()
{
Dullinklisth,p,s;
h=newDulnode; //头接点为空
h->prior=NULL;
h->next=NULL;
p=h;
stringstr;
cout<<"输入编号(n结束):";
cin>>str;
while(str!="n")
{
s=newDulnode;
p->next=s;
s->list=newperson;
cout<<"输入姓名:";cin>>s->list->name;
cout<<"输入性别:";cin>>s->list->sex;
cout<<"输入年龄:";cin>>s->list->age;
cout<<"输入住址:";cin>>s->list->street;
cout<<"输入邮编:";cin>>s->list->eip;
cout<<"输入电话:";cin>>s->list->phone;
cout<<"输入QQ号:";cin>>s->list->qq;
cout<<"输入微信:";cin>>s->list->weixin;
cout<<"输入生日:";cin>>s->list->birth;
cout<<endl;
s->prior=p;
s->next=NULL;
p=s;
cout<<"输入编号(n结束):";
cin>>str;
}
h->prior=s;
p->next=h;
return(h);
}
int Length(Dullinklist h)
{
Dullinklistp;
inte=0;
p=h->next;
while(p!=h)
{
e++;
p=p->next;
}
returne;
}
void Display(Dullinklist t)
{
Dullinklistq;
q=t->next;
while(q!=t&&q!=NULL)
{
cout<<"姓名:";cout<<q->list->name;
cout<<"性别:";cout<<q->list->sex;
cout<<"生日:";cout<<q->list->birth;
cout<<"年龄:";cout<<q->list->age;
cout<<"住址:";cout<<q->list->street;
cout<<"邮编:";cout<<q->list->eip;
cout<<"电话:";cout<<q->list->phone;
cout<<"QQ号:";cout<<q->list->qq;
cout<<"微信:";cout<<q->list->weixin;
cout<<endl;
q=q->next;
}
system("pause");
}
int Search(Dullinklist h,string str1)
{
Dullinklistp;
intm=0;
stringch,str,name;
p=h->next;
while(p!=h&&p!=NULL)
{
str=p->list->name;
if(str==str1)
{
cout<<endl<<"此人的信息如下:"<<endl;
cout<<"姓名:";cout<<p->list->name;
cout<<"性别:";cout<<p->list->sex;
cout<<"生日:";cout<<p->list->birth;
cout<<"年龄:";cout<<p->list->age;
cout<<"住址:";cout<<p->list->street;
cout<<"邮编:";cout<<p->list->eip;
cout<<"电话:";cout<<p->list->phone;
cout<<"QQ号:";cout<<p->list->qq;
cout<<"微信:";cout<<p->list->weixin;
cout<<endl;
system("pause");
m++;
}
p=p->next;
}
returnm;
}
void Del1(Dullinklist h,int e)
{
Dullinklistp;
charch;
stringstr;
p=h;
for(inti=0;i<e;i++)
p=p->next;
cout<<endl<<"此人的信息如下:"<<endl;
cout<<"姓名:";cout<<p->list->name<<endl;
cout<<"性别:";cout<<p->list->sex;
cout<<"年龄:";cout<<p->list->age;
cout<<"住址:";cout<<p->list->street;
cout<<"邮编:";cout<<p->list->eip;
cout<<"电话:";cout<<p->list->phone;
cout<<"QQ号:";cout<<p->list->qq;
cout<<"微信:";cout<<p->list->weixin;
cout<<"生日:";cout<<p->list->birth;
cout<<endl;
cout<<"确定删除(y/n)?: ";cin>>ch;
if(ch=='y'||ch=='Y')
{
p->prior->next=p->next;
p->next->prior=p->prior;
cout<<"删除成功!"<<endl;
}
}
int Del2(Dullinklist h,string nam)
{
Dullinklistp;
charch;
intm=0;
stringstr;
p=h->next;
while(p->next!=h->next)
{
str=p->list->name;
if(str==nam)
{
cout<<endl<<"此人的信息如下:"<<endl;
cout<<"姓名:"<<p->list->name<<endl;
cout<<"性别:";cout<<p->list->sex;
cout<<"年龄:";cout<<p->list->age;
cout<<"住址:";cout<<p->list->street;
cout<<"邮编:";cout<<p->list->eip;
cout<<"电话:";cout<<p->list->phone;
cout<<"QQ号:";cout<<p->list->qq;
cout<<"微信:";cout<<p->list->weixin;
cout<<"生日:";cout<<p->list->birth;
cout<<endl;
m++;
cout<<"确定删除(y/n)?: ";
cin>>ch;
if(ch=='y'||ch=='Y')
{
p->prior->next=p->next;
p->next->prior=p->prior;
cout<<"删除成功!"<<endl;
}
}
p=p->next;
}
returnm;
}
void Insert(Dullinklist h)
{
Dullinklists,p;
intm,length;
p=h;
s=newDulnode;
s->list=newperson;
length=Length(h);
cout<<"输入您要添加的位置:";
cin>>m;
if(m<=0||m>length)
{
cout<<"请重新输入(1~"<<length<<"):";
cin>>m;
}
cout<<"输入添加人的信息:"<<endl;
for(inti=0;i<m;i++)
p=p->next;
cout<<"姓名:";cin>>s->list->name;
cout<<"性别:";cin>>s->list->sex;
cout<<"年龄:";cin>>s->list->age;
cout<<"住址:";cin>>s->list->street;
cout<<"邮编:";cin>>s->list->eip;
cout<<"电话:";cin>>s->list->phone;
cout<<"QQ号:";cin>>s->list->qq;
cout<<"微信:";cin>>s->list->weixin;
cout<<"生日:";cin>>s->list->birth;
cout<<endl;
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
cout<<"\t添加成功!"<<endl;
}
int main()
{
while(true)
{
system("cls");
unsignedch;
charsh;
Dullinklists,p;
stringnam;
intm,length;
cout<<"\n\t\t\t****'志文'通讯录管理系统****"<<endl;
cout<<"\t\t---------------------------------------------"<<endl;
cout<<"\t\t 1 输入 2 查找 "<<endl;
cout<<"\t\t 3 删除 4 显示 "<<endl;
cout<<"\t\t 5 添加 0 退出 "<<endl;
cout<<"\t\t---------------------------------------------"<<endl;
cout<<endl<<"\t\t\t选择操作(0~5): ";
cin>>ch;
switch(ch)
{
case0:exit(-1);break;
case1:head=Enter();
break;
case2:if(head->next==NULL||head->prior==NULL)
cout<<"通讯录已空!";
else
{
cout<<"要查找人姓名:";cin>>nam;
m=Search(head,nam);
if(!m)
{
cout<<"无此人记录!"<<endl;
cout<<"是否查找其他(y/n)?:";
cin>>sh;
if(sh=='y'||sh=='Y')
{
cout<<"\t查找人姓名:";
cin>>nam;
m=Search(head,nam);
}
}
}
break;
case3:if(head->next==NULL||head->prior==NULL)
cout<<"通讯录已空!";
else
{
cout<<"\n\t\t1按位置删除 OR2 按姓名删除: ";
cin>>sh;
if(sh=='1')
{
length=Length(head);
cout<<"删除对象位置(1~"<<length<<"):";
cin>>m;
if(length==0)
cout<<"通讯录已空!";
if(m<=0||m>length)
{
cout<<"请重新输入(1~"<<length<<"):";
cin>>m;
}
Del1(head,m);
}
if(sh=='2')
{
cout<<"要删除人姓名:";cin>>nam;
m=Del2(head,nam);
if(!m)
{
cout<<"无此人记录!"<<endl;
cout<<"是否还要删除(y/n)?:";
cin>>sh;
if(sh=='y'||sh=='Y')
{
m=Del2(head,nam);
}
}
}
}
break;
case4:if(head->next==NULL||head->prior==NULL)
cout<<"通讯录已空!";
else
{
length=Length(head);
cout<<"\t通讯录信息如下:"<<endl;
cout<<"\t\t\n通讯录中共有"<<length<<"条记录。"<<endl;
Display(head);
}
break;
case5:
if(head->next==NULL||head->prior==NULL)
{
p=head;
s=newDulnode;
s->list=newperson;
cout<<"通讯录已空!";
p->next=s;
cout<<"姓名:";cin>>s->list->name;
cout<<"性别:";cout<<s->list->sex;
cout<<"年龄:";cout<<s->list->age;
cout<<"住址:";cout<<s->list->street;
cout<<"邮编:";cout<<s->list->eip;
cout<<"电话:";cout<<s->list->phone;
cout<<"QQ号:";cout<<s->list->qq;
cout<<"微信:";cout<<s->list->weixin;
cout<<"生日:";cout<<s->list->birth;
cout<<endl;
s->prior=p;
s->next=NULL;
p=s;
p=head;
cout<<"\t添加成功!"<<endl;
}
else
Insert(head);
break;
}
}
return0;
}
七. 程序运行结果
1、主界面
2、输入新联系人,按照提示依次输入信息
3、对通讯录进行查找
4、显示通讯录里的个人信息
5、添加通讯录新成员
6、删除通讯录成员
八. 心得
这次的程序设计也让我深刻地认识到一味地看书而不加以实际操作的话,那相当于纸上谈兵。个人对本程序的设计,深有感触,从拿到这个题目到设计完毕这个过程中,不知道从何开始做,如何很好地运用数据结构来做好、如何把上学期学的内容与本学期所学的内容结合起来,于是我只能开始在互联网上查找相关的信息,包括这些程序,通过参考别人做的通信录,找到我所需要的信息,结合参考并开始设计这个程序,在这个过程中,学到不少东西,如个人认为,当遇到自己难以解决的问题,通过跟老师沟通、和同学讨论又或者在网上学习参考是个不错的选择。由于本次的通信录是主要用本学期所学的数据机构设计的,通过运用链表这个存储结构来储存我们平时用到的信息。主要是用双链表,但是我对这些其实还不是很理解,出现了不少错误,这让我体会到,程序设计这项工作需要很大的耐心与信心,即使过程中会遇到很多的不理解不懂的地方,但是在解决这个问题的过程中我们能学到很多东西,不仅能巩固我们所学的内容还能学到其他新的知识。
通过这些实践,我发现程序设计蛮有趣的,相信这是一种兴趣,虽然现在大二了,我一样相信,学好信息技术这门课为时尚不晚。事实上,过去对待本专业实在是兴趣都不大,很多作业实验报告敷衍了之,几乎不做,包括这次课程设计,毫不讳言,并非我认真而为之。然而,无论过去怎样,期待对待学习态度与行动积极的自己!
- 数据结构课程设计
- 数据结构课程设计
- 课程设计----数据结构
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 《数据结构》课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构课程设计
- 数据结构 课程设计
- android图片加载防止OOM LruCache和DiskLruCache使用
- javascript原型
- 《现代操作系统4th》英文版阅读笔记 4.3.5章 实现文件共享
- Ubuntu14.04下安装ffmpeg
- nyoj 844 A+B Problem(V)
- 数据结构课程设计
- LeetCode Palindrome Number
- 克服跨洋网络延迟,使用Docker Hub Mirror加速Docker官方镜像下载
- 《数据结构课程设计》报告
- IP地址如何用十六进制表示
- 通过比喻来了解spring
- 内存池--高效实现
- 浅谈 my_strcmp() 函数
- Trace and profile function calls with GCC