数据结构实验一 通讯录 下载本文

北京邮电大学信息与通信工程学院

5.查看所有用户

6.修改已有用户

第9页

北京邮电大学信息与通信工程学院

7.退出通讯簿管理系统

4. 总结

通过这次的数据结构上机试验,让我回顾了有关线性表这一章的知识,学会使用线性表解决实际问题,让我有了更深的认识和了解,能灵活的使用,同时也发现了自己的不足,有些地方理解还不够深刻,出现了不少问题

调试时问题主要出在内存错误上,有时指针悬挂,有时尾指针指向错误,这些都需要一步步耐心的调试才能发现,以后编程我会注意改正这些

依照实验要求,在不同操作时加入了异常处理,比如要查找,删除,修改用户时,如果输入的ID不存在,系统会报错并终止操作,提高了系统的操作性 这次设计的通讯簿管理系统还有些可以改进的地方,比如加入文件流后可以将每次输入的信息保存在后台的一个文件里,每次使用时读取这个文件内的信息,实现了保存每次更新的数据。我对于文件流的使用还不是很熟悉,所以没来及加入这项功能;还有在查询这一块,只实现了按编号查询,还未加入按姓名查找。

附:所有源程序 #include using namespace std;

//个人数据结构体 struct DataType{ int ID; //用户ID; char name[10]; //姓名 char ch; //性别

第10页

北京邮电大学信息与通信工程学院

char phone[13]; //手机号码 char addr[31]; //地址 };

//用户结点 struct Node{ DataType data; struct Node * next; };

//通讯簿类声明 class ContactBook {

public: ContactBook(){front=new Node;rear=new Node;front->next=NULL;rear=front;} ContactBook(DataType a[],int n); //建立 ~ContactBook(); //析构 void Add(DataType a); //添加 DataType * Get(int i); //查找 DataType Delete(int i); //删除 void Modify(DataType a,int i); //修改 void PrintList(); //遍历 private: Node * front; //头结点 Node * rear; //尾结点 };

//通讯簿的建立

ContactBook::ContactBook(DataType a[],int n)//尾插法 { front=new Node; rear=new Node; rear=front; for(int i=0;idata=a[i]; rear->next=s; rear=s; } rear->next=NULL; }

//通讯簿的添加

第11页

北京邮电大学信息与通信工程学院

void ContactBook::Add(DataType a)//尾插法 { Node * s=new Node; s->data=a; rear->next=s; rear=s; rear->next=NULL; }

//通讯簿按ID查找

DataType * ContactBook::Get(int i) { Node * p=front->next; while(p) { if(p->data.ID==i)return &(p->data); 回位置 p=p->next; } return NULL; 针 }

//通讯簿修改

void ContactBook::Modify(DataType a,int i) { DataType * p=Get(i); * p=a; }

//通讯簿删除

DataType ContactBook::Delete(int i) { Node * p=front; while(p->next) { if(p->next->data.ID==i)break; 环 p=p->next; } Node * q=p->next; if(q) { p->next=q->next;

第12页//根据ID找到被查元素,返 //若找不到 ,返回空指 //按ID找到要修改的用户 //修改用户的个人数据 //找到要删除的用户,跳出循 //找到该用户