C++用链表存放数据

来源:互联网 发布:珠江新闻眼回看软件 编辑:程序博客网 时间:2024/06/05 04:23

用链表存放数据在插入和删除操作时比用线性表方便很多,链表不用去移动那些位置的数据。

这是用链表实现的简单通讯录,先上代码

/** 程序功能:用链表储存学生通讯录信息* 作者:BossMao* 版本:2.0*/#include<iostream>#include<string>using namespace std;struct student_Info{double stu_Num;//学生学号string stu_Name;//学生姓名string stu_Parent_Name;//学生家长姓名int stu_Age;//学生年龄string stu_Sex;//学生性别string stu_Address;//学生住址char stu_PhoneNum[20];//学生电话号码char stu_Parent_PhoneNum[20];//学生家长电话号码student_Info *priority;//前指针student_Info *next;//后指针};class Student{private:int current_stuNum;student_Info *head;student_Info *temp;public:Student();~Student();void menu();void insert_student_Info();//插入学生信息void update_student_Info(string name);//修改学生信息void delete_student_Info(string name);//删除学生信息student_Info *search_student_Info(string name);//查找学生信息void show_student_Info();//显示学生信息};Student::Student(){current_stuNum = 0;head = new student_Info;//创建第一个结点head->next = NULL;head->priority = NULL;temp = head;}Student::~Student(){}void Student::update_student_Info(string name){char choose;student_Info *s;s = search_student_Info(name);//询问是否修改cout << "是否修改此学生的信息?(Y/N)" << endl;cin >> choose;if (choose == 'N'||choose == 'n'){return;}if (choose == 'Y'||choose == 'y'){cout << "======================================" << endl;cout << "学号:";cin >> s->stu_Num;cout << "姓名:";cin >> s->stu_Name;cout << "家长姓名:";cin >> s->stu_Parent_Name;cout << "年龄:";cin >> s->stu_Age;cout << "性别: ";cin >> s->stu_Sex;cout << "地址:";cin >> s->stu_Address;cout << "学生电话号码:";cin >> s->stu_PhoneNum;cout << "家长电话号码:";cin >> s->stu_Parent_PhoneNum;cout << "======================================" << endl;cout << "修改成功!!" << endl;cout << "======================================" << endl;}}void Student::delete_student_Info(string name){char choose;student_Info *s;s = search_student_Info(name);//询问是否删除cout << "是否删除此学生的信息?(Y/N)" << endl;cin >> choose;if (choose == 'N'||choose == 'n'){return;}if (choose == 'Y'||choose == 'y'){if (s->next == NULL){s->priority->next = s->next;//将要删除节点的前一结点的尾指针置空temp = s->priority;delete s;cout << "======================================" << endl;cout << "删除成功!!" << endl;cout << "======================================" << endl;}else{s->priority->next = s->next;//将要删除结点的前一结点的尾指针  修改为要删除结点的尾指针s->next->priority = s->priority;//将要删除结点的下一结点的前指针  修改为要删除结点的的前指针delete s;}}}student_Info *Student::search_student_Info(string name){bool have = 0;student_Info *p, *found = NULL;for (p = head; p != NULL;p=p->next){if (p->stu_Name == name){have = 1;found = p;cout << "此学生的信息为:" << endl;cout << "======================================" << endl;cout << "学号:";cout << p->stu_Num << endl;cout << "姓名:";cout << p->stu_Name << endl;cout << "家长姓名:";cout << p->stu_Parent_Name << endl;cout << "年龄:";cout << p->stu_Age << endl;cout << "性别: ";cout << p->stu_Sex << endl;cout << "地址:";cout << p->stu_Address << endl;cout << "学生电话号码:";cout << p->stu_PhoneNum << endl;cout << "家长电话号码:";cout << p->stu_Parent_PhoneNum << endl;cout << "======================================" << endl;}}if (have == 0){cout << "======================================" << endl;cout << "查无此人!!" << endl;cout << "======================================" << endl;menu();}return found;}void Student::show_student_Info(){student_Info *p = head->next;cout << "学生信息为:" << endl;if (p == NULL){cout << "======================================" << endl;cout << "并没有学生!!" << endl;cout << "======================================" << endl;return;}for (; p != NULL; p = p->next){cout << "======================================" << endl;cout << "学号:";cout << p->stu_Num << endl;cout << "姓名:";cout << p->stu_Name << endl;cout << "家长姓名:";cout << p->stu_Parent_Name << endl;cout << "年龄:";cout << p->stu_Age << endl;cout << "性别: ";cout << p->stu_Sex << endl;cout << "地址:";cout << p->stu_Address << endl;cout << "学生电话号码:";cout << p->stu_PhoneNum << endl;cout << "家长电话号码:";cout << p->stu_Parent_PhoneNum << endl;cout << "======================================" << endl;}}void Student::insert_student_Info(){char s;while (1){cout << "需要插入学生的信息(Y/N)?" << endl;while (1){cin >> s;if (s == 'Y' || s == 'y' || s == 'N' || s == 'n'){break;}else{cout << "输入有误,请重新输入!!" << endl;cout << "需要插入学生的信息(Y/N)?" << endl;}}if (s == 'N' || s == 'n'){break;}else{student_Info *tail = new student_Info;//创建新结点temp->next = tail;//将新结点与原有结点连接起来tail->priority = temp;tail->next = NULL;//新结点的指针域为空temp = tail;cout << "======================================" << endl;cout << "学号:";cin >> tail->stu_Num;cout << "姓名:";cin >> tail->stu_Name;cout << "家长姓名:";cin >> tail->stu_Parent_Name;cout << "年龄:";cin >> tail->stu_Age;cout << "性别: ";cin >> tail->stu_Sex;cout << "地址:";cin >> tail->stu_Address;cout << "学生电话号码:";cin >> tail->stu_PhoneNum;cout << "家长电话号码:";cin >> tail->stu_Parent_PhoneNum;cout << "======================================" << endl;}}}void Student::menu(){int choose;string name;cout << "1.插入学生信息" << endl;cout << "2.修改学生信息" << endl;cout << "3.删除学生信息" << endl;cout << "4.查询学生信息" << endl;cout << "5.显示学生信息" << endl;cout << "0.退出" << endl;cout << endl;cout << "请输入你要执行功能对应的数字:" << endl;cin >> choose;if (choose >= 0 && choose < 6){switch (choose){case 1:{insert_student_Info();break;}case 2:{cout << "请输入你要修改的学生姓名:" << endl;cin >> name;update_student_Info(name);break;}case 3:{ cout << "请输入你要删除的学生姓名:" << endl; cin >> name; delete_student_Info(name); break;}case 4:{cout << "请输入你要查找的学生姓名:" << endl;cin >> name;search_student_Info(name);break;}case 5:{show_student_Info(); break;}case 0:exit(0);}}else{cout << "======================================" << endl;cout << "输入有误!!请重新输入" << endl;cout << "======================================" << endl;}}void main(){Student stu;while (1){stu.menu();}}

实现这个简单程序时,我用双链表的数据结构

看到程序后就会发现,做这个小系统用链表真的方便多了。函数的调用都是用菜单函数去调用对应的功能函数,很简单。

这里值得注意的是,当要做删除最后一个结点的操作时,要记得将倒数第二个结点的next指针保存到temp指针里,否则当再次向链表尾插入新的结点时,不能用temp指针将新结点与原有结点链接起来。


0 0