数据结构程序设计(2号题)

这篇博客讲述了李刚利用所学数据结构知识,采用双向链表设计个人通讯录管理软件的过程。程序包含了添加、修改联系人等功能,并在VC6.0中成功编译。作者分享了编程体验,认识到理论与实践结合的重要性,尽管过程中遇到挑战,但收获了解决问题的喜悦。

数据结构程序设计

                      ----------(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 黄德健】

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值