通讯录的元素是⼈,所以需要新建⼀个Person类作为链表每个Node的元素。并且为了正常输出,Person还需要⾍重载⼀些运算符,包括输出<<, 判等==
main函数⾥也要写⼀些辅助函数来获取信息。
链表本⾝实现部分不需要修改太多,把原来的的int型元素改为Person类型即可代码如下
//Person.h#pragma once
#include using namespace std;class Person{ friend ostream& operator<<(ostream& out,Person &p) { out << \"Name: \"< Person(string na, int num); Person(); ~Person(); bool operator ==(Person &p) { if ((this->name == p.name) && (this->phone == p.phone)) { return true; } else { return false; } } string name=\"张三\"; int phone=0;private: }; //Person.cpp #include\"Person.h\" Person::Person(string na, int num):name(na),phone(num){} Person::Person(){} Person::~Person(){} //Node.h #pragma once using namespace std;#include public: Node(Person a); Node(); ~Node(); Person person; Node* next;private:}; //Node.cpp #include\"Node.h\" Node::Node(Person a):person(a){} Node::Node(){} Node::~Node(){} //List.h #pragma once #include #define __debug__#ifdef __debug__#endifclass List{ public: List();// ~List();// bool get_add_head(Node* e);//在头节点后⾯插⼊ bool get_add_tail(Node* e);//在尾节点后⾯插⼊ void get_traverse();//遍历 int get_length();//获取长度 bool get_empty();//判空 void get_clear();//清空线性表 int get_ele_num(Node* p);//获取与传⼊参数值相同的链表中元素的序号 bool get_i_ele(int e, Node* value);//获指定位序的元素 void get_delete(int location);//删除某位置元素 bool get_add(int location, Node* e);//添加在某位置元素 bool get_pre(Node* e, Node* pre);//获取前驱 bool get_post(Node* e, Node* pos);//获取后继private: Node* node;//头节点 int length;}; //List.cpp #include\"List.h\"List::List(){ length = 0; node = new Node; node->person.name=\"zhangsan\"; node->person.phone = 0; node->next = NULL;} List::~List(){ get_clear(); delete node; node = NULL;} bool List::get_add_head(Node* e){ Node* c_node = new Node; c_node=node->next; Node* new_node = new Node; if (new_node == NULL) { return false; } new_node->person = e->person; new_node->next = c_node; node->next = new_node; length++; #ifdef __debug__ cout << node->next->person << endl;#endif return true;} bool List::get_add_tail(Node* e){ Node* c_node = new Node; c_node = node; while (c_node->next!=NULL) { c_node = c_node->next; } Node* new_node = new Node; if (new_node == NULL) { return false; } new_node->person = e->person; new_node->next = NULL; c_node->next = new_node; length++; #ifdef __debug__ cout << c_node->next->person << endl;#endif return true;} void List::get_traverse(){ Node* c_node = new Node; c_node = node->next; while (c_node != NULL) { cout << c_node->person << endl; c_node = c_node->next; } }//遍历 int List::get_length(){ return length;}//获取长度 bool List::get_empty(){ return length == 0 ? true : false;} void List::get_clear(){ Node* c_node = new Node; c_node = node; while(c_node->next != NULL) { Node* new_node = new Node; new_node=c_node->next; delete c_node;//清除指向的内容 c_node = new_node; } length = 0; node->next = NULL;//易错,勿忘!}//清空线性表 int List::get_ele_num(Node* p)//获取元素的位序{ Node* c_node = new Node; c_node=node; int count = 1; while (c_node->next != NULL) { c_node = c_node->next; if (c_node->person == p->person) { return count; } else { count++; } } return -1;} bool List::get_i_ele(int e, Node* value){ if (e <1 || e > length) { cout << \"Invalid num!\" << endl; return false; } Node* c_node = new Node; c_node = node; for (int i = 0; i value->person = c_node->person;#ifdef __debug__ cout << c_node->person << endl;#endif return true; }//获指定位序元素的值// void List::get_delete(int location){ if (location<1 || location>length ) { cout << \"invalid num!!\" << endl; } else { Node* c_node = new Node; c_node = node; Node* c_node_before = new Node; for (int i = 0; i < location; i++)//遍历到了location对应的位置,与增加元素有所不同,需要注意! { c_node_before = c_node; c_node = c_node->next; } c_node_before->next = c_node->next; length--; } }//某位置删除元素// bool List::get_add(int location, Node* e){ if (location<1 || location>length) { cout << \"Invalid num!\" << endl; return false; } else { Node* c_node = new Node; c_node=node; for (int i = 0; i < (location-1); i++) { c_node = c_node->next; }//此时是遍历到了(location-1)那个元素的位置,然后在其后⾯插⼊新元素则是第location个元素。 Node* new_node = new Node; if (new_node == NULL) { return false; } new_node->person= e->person; new_node->next = c_node->next; c_node->next = new_node; length++; return true; } }//在某位置插⼊元素 bool List::get_pre(Node* e, Node* pre){ Node* c_node = new Node; c_node=node; Node* pre_node = new Node; while (c_node->next != NULL) { pre_node = c_node; c_node = c_node->next; if (c_node->person== e->person) { if (pre_node==node) { cout << \"No pre!\" << endl; return false; } else { pre->person = pre_node->person;#ifdef __debug__ cout << pre->person << endl; #endif return true; } } } cout << \"No this value!\" << endl; return false;}//获取前驱 bool List::get_post(Node* e, Node* pos){ Node* c_node = new Node; c_node=node; Node* pos_node = new Node; while (c_node->next != NULL) { c_node = c_node->next; if (c_node->person == e->person) { if (c_node->next== NULL) { return false; } else { pos->person = c_node->next->person;#ifdef __debug__ cout << pos->person << endl; #endif return true; } } } return false;}//获取后继 //main.cpp #include\"List.h\"int menu(){ cout << \"----请输⼊你的选择----\" << endl; cout << \"1.新建联系⼈\" << endl; cout << \"2.删除联系⼈\" << endl; cout << \"3.输出通讯录\" << endl; cout << \"4.退出通讯录\" << endl; int order = 0; cin >> order; return order;} void creat_person(List* p){ Node m; cout << \"姓名\" << endl; cin>>m.person.name; cout << \"电话\" << endl; cin >> m.person.phone; p->get_add_tail(&m);} int main(int *argc, char* argv[]){ List* p_list = new List; int order =0; while (order != 4) { order = menu(); switch (order) { case 1: { cout << \"你选择新建联系⼈\" << endl; creat_person(p_list); break; } case 2: { cout << \"你选择删除联系⼈\" << endl; cout << \"你要删除第⼏个?\" << endl; int mm; cin >> mm; p_list->get_delete(mm); break; } case 3: { cout << \"你选择输出联系⼈\" << endl; p_list->get_traverse(); break; } case 4: { cout << \"你选择退出\" << endl; break; } default: { cout << \"What do you mean?\" << endl; break; } } } cout << \"已退出\" << endl; delete p_list; p_list = NULL; return 0;} /*运⾏结果 ----请输⼊你的选择----1.新建联系⼈2.删除联系⼈3.输出通讯录4.退出通讯录 1 你选择新建联系⼈姓名张三电话1235 Name: 张三---Pnone number: 1235----请输⼊你的选择----1.新建联系⼈2.删除联系⼈3.输出通讯录4.退出通讯录3 你选择输出联系⼈ Name: 张三---Pnone number: 1235----请输⼊你的选择----1.新建联系⼈2.删除联系⼈3.输出通讯录4.退出通讯录2 你选择删除联系⼈你要删除第⼏个?1 ----请输⼊你的选择----1.新建联系⼈2.删除联系⼈3.输出通讯录4.退出通讯录4 你选择退出已退出 */ 因篇幅问题不能全部显示,请点此查看更多更全内容