用鏈表存放數據在插入和刪除操作時比用線性表方便很多,鏈表不用去移動那些位置的數據。
這是用鏈表實現的簡單通訊錄,先上代碼
/* * 程序功能:用鏈表儲存學生通訊錄信息 * 作者: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指針將新結點與原有結點鏈接起來。
C++用鏈表存放數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.