这两天复习了一下单链表,将单链表的一些基本操作都自己写了一遍,也弥补了之前上课时不同的一些知识。
这次的通讯录我分了三个文件,对链表的基本操作都用函数封装起来了,这样的代码看起来更加清楚。
写选择操作功能的时候遇到了一个小问题,一开始我的选项是int类型的,但是只要我输入不是int类型的就会让程序崩掉,最后我将选项换成了char类型的,完美的解决了这个问题。虽然这个代码还是有点小bug,但还是能供大家参考的。
以下是通讯录的完整代码:
address.h
#ifndef AdDDRESS_H_HH #define AdDDRESS_H_HH #include <iostream> #include <cstring> #include <cstdlib> using namespace std; struct Node { char name[15]; char phone[20]; char address[30]; struct Node * next; }; extern struct Node* createlist(); extern struct Node* createnode(struct Node info); extern bool insertinfo(struct Node* list, struct Node info); extern void printinfo(struct Node* list); extern void menu(); extern char choose(); extern void work(struct Node* list); extern bool insertoperation(struct Node* list); extern bool deleteinfo(struct Node* list, char *name); extern bool deleteoperation(struct Node* list); extern struct Node searchinfo(struct Node* list, char *name); extern bool searchoperation(struct Node* list); extern void returnmenu(); extern void clearscreen(); #endif
addressfunc.cpp
#include "address.h" //创建一个链表 struct Node* createlist() { struct Node *list = new struct Node; list->next = NULL; return list; } //创建一个结点 struct Node* createnode(struct Node info) { struct Node* newnode = new struct Node; newnode->next = NULL; strcpy(newnode->name, info.name); strcpy(newnode->phone, info.phone); strcpy(newnode->address, info.address); // cout << "创建结点调试" << endl; // cout << newnode->name << endl; return newnode; } //在尾部插入一个结点 bool insertinfo(struct Node* list, struct Node info) { struct Node* newnode = createnode(info); if(newnode->name == NULL) return false; struct Node* temp = list; while (temp->next != NULL) { temp = temp->next; } newnode->next = NULL; temp->next = newnode; // cout << "插入结点调试" << endl; // cout << newnode->name << endl; return true; } //删除联系人 bool deleteinfo(struct Node* list, char *name) { struct Node* temp = list; struct Node* p = list->next; if(p == NULL) { cout << "没有叫 " << name << " 的联系人" << endl; return false; } while(strcmp(p->name, name) != 0) { if(p->next == NULL) { cout << "没有叫 " << name << " 的联系人" << endl; return false; } p = p->next; temp = temp->next; } temp->next = p->next; delete p; return true; } //查找联系人 struct Node searchinfo(struct Node* list, char* name) { struct Node* temp = list; struct Node* p = list->next; struct Node a; if(p == NULL) { cout << "没有叫 " << name << " 的联系人" << endl; strcpy(a.name, "nonono"); strcpy(a.phone, "nonono"); strcpy(a.address, "nonono"); return a; } while (strcmp(p->name, name)) { if(p->next == NULL) { cout << "没有叫 " << name << " 的联系人" << endl; strcpy(a.name, "nonono"); strcpy(a.phone, "nonono"); strcpy(a.address, "nonono"); return a; } temp = temp->next; p = p->next; } cout << "查找联系人调试" << endl; cout << p->name << endl; return *p; } //打印所有的信息 void printinfo(struct Node* list) { struct Node* temp = list->next; // cout << "打印所有的信息调试" << endl; // cout << temp->name << endl; if(temp == NULL) { cout << "没有任何信息" << endl; return; } cout << "以下是所有的信息" << endl; while(temp != NULL) { cout << "名字:" << temp->name << endl << "电话:" << temp->phone << endl << "地址:" << temp->address <<endl << endl; temp = temp->next; } } //菜单栏 void menu() { cout << " -------------------------------------" << endl; cout << " - 通讯录 -" << endl; cout << " - A、存入联系人 -" << endl; cout << " - B、删除联系人 -" << endl; cout << " - C、查找联系人 -" << endl; cout << " - D、打印所有信息 -" << endl; cout << " - E、退出通讯录 -" << endl; cout << " -------------------------------------" << endl; } //选择 char choose() { system("clear"); menu(); char a; cout << "请输入您的选择" << endl; cin >> a; //将小写字母转换成大写字母。 if('a' == a) a = 'A'; else if('b' == a) a = 'B'; else if('c' == a) a = 'C'; else if('d' == a) a = 'D'; else if('e' == a) a = 'E'; while(a < 'A' || a > 'E') { cout << "您是输入不正确,请重新输入" << endl; cin >> a; if('a' == a) a = 'A'; else if('b' == a) a = 'B'; else if('c' == a) a = 'C'; else if('d' == a) a = 'D'; else if('e' == a) a = 'E'; } return a; } //插入信息的具体操作 bool insertoperation(struct Node* list) { struct Node info; cout << "请输入信息" << endl; cout << "名字:"; cin >> info.name; cout << "电话: "; cin >> info.phone; while(strlen(info.phone) != 11) { cout << "请输入11位手机号" << endl; cin >> info.phone; } cout << "地址:"; cin >> info.address; bool insertok = insertinfo(list, info); return insertok; } //删除信息的具体操作 bool deleteoperation(struct Node* list) { cout << "请输入您要删除的联系人的名字" << endl; char name[15] = "0"; cin >> name; bool deleteok = deleteinfo(list, name); return deleteok; } //查找联系人的具体操作 bool searchoperation(struct Node* list) { cout << "请输入您要查找的联系人的名字" << endl; char name[15] = "0"; cin >> name; struct Node searchresult = searchinfo(list, name); if(strcmp(searchresult.name, name) == 0) { cout << "名字:" << searchresult.name << endl << "电话:" << searchresult.phone << endl << "地址:" << searchresult.address <<endl << endl; return true; } else return false; } //操作的流程 void work(struct Node* list) { char a = choose(); switch (a) { case 'A'://插入信息 { clearscreen(); int ret = insertoperation(list); if(ret == true) cout << "信息存入成功" << endl; else cout << "信息存入失败" << endl; returnmenu(); }break; case 'B'://删除信息 { clearscreen(); int ret = deleteoperation(list); if(ret == true) cout << "信息删除成功" << endl; else cout << "信息删除失败" << endl; returnmenu(); }break; case 'C'://查找联系人 { clearscreen(); int ret = searchoperation(list); if(ret == true) cout << "查找联系人成功" << endl; else cout << "查找联系人失败" << endl; returnmenu(); }break; case 'D': //打印所有的信息 { clearscreen(); printinfo(list); returnmenu(); } break; case 'E': exit(1); break; } } //返回菜单栏 void returnmenu() { cout << "输入q返回菜单栏" << endl; char a; cin >> a; while(a != 'q' && a != 'Q') { cout << "输入不正确" << endl; cin >> a; } } //清屏操作 void clearscreen() { system("clear"); cout << endl << endl << endl ; }
addressmain.cpp
#include "address.h" int main() { struct Node* list = createlist(); while(1) { work(list); } return 0; }