C++鏈表實現(xiàn)通訊錄設(shè)計
更新時間:2022年06月21日 08:32:43 作者:算法之路慢慢兮,吾將上下而求索
這篇文章主要為大家詳細介紹了C++鏈表實現(xiàn)通訊錄設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++鏈表實現(xiàn)通訊錄設(shè)計的具體代碼,供大家參考,具體內(nèi)容如下
功能如下:
1添加學(xué)生信息
2刪除學(xué)生信息
3顯示學(xué)生信息
4查詢學(xué)生信息
5學(xué)生信息排序
6清空屏幕信息
7清空文檔信息
8退出管理系統(tǒng)
上代碼!
#include <iostream> #include <algorithm> #include <string> #include <fstream>//讀寫文件的頭文件 using namespace std; struct ElementType; struct Node; struct Queue; typedef struct Queue* MyQueue; struct ElementType { ?? ?int id; ?? ?string name; ?? ?int num; }; struct Node { ?? ?ElementType data; ?? ?Node* next; }; struct Queue { ?? ?Node* front; ?? ?Node* rear; }; MyQueue Init(MyQueue& q);//Initialize queue bool IsEmpty(MyQueue q);//Determine if the queue is empty bool Insert(ElementType x, MyQueue q);//Insert the data to the end of the queue bool Delete(const int message, MyQueue q);//Find some data in the queue, and then delete the corresponding node void Print(const Node* q);//Prints all the information in a node void PrintAll(const MyQueue q);//Prints information from all nodes bool FindByName(const string massage, const MyQueue q);//Prints information from all nodes void Input(MyQueue q);//When the address book is empty, re-enter the information into the address book? void Write(MyQueue q);//Write the information from the queue to the document? MyQueue Read();//Write the information from the queue to the document MyQueue ReadOrClear(MyQueue& q);//Whether to empty all the information? void Swap(ElementType& x, ElementType& y);//Swap functions in sort MyQueue BubbleSort(MyQueue q);//Sort by student ID using bubble sort? void Menu(MyQueue q);//main menu //初始化隊列? MyQueue Init(MyQueue& q) { ?? ?q = new Queue(); ?? ?if (q == NULL) return NULL; ?? ?q->front = NULL; ?? ?q->rear = NULL; ?? ?return q; } //查看隊列是否為空? bool IsEmpty(MyQueue q) { ?? ?return q->front == NULL; } //添加信息? bool Insert(ElementType x, MyQueue q) { ?? ?Node* temp = new Node(); ?? ?if (temp == NULL) return false; ?? ?temp->data = x;//這里需要改成需要的內(nèi)容,最好(必須)改成一個函數(shù)的形式,賦值的時候調(diào)用函數(shù),打印的時候也調(diào)用函數(shù) ?? ?temp->next = NULL; ?? ?if (IsEmpty(q)) { ?? ??? ?q->front = temp; ?? ??? ?q->rear = temp; ?? ??? ?return true; ?? ?} ?? ?else { ?? ??? ?q->rear->next = temp; ?? ??? ?q->rear = temp; ?? ??? ?return true; ?? ?} } //刪除功能? bool Delete(const int message, MyQueue q) { ?? ?Node* temp = new Node(); ?? ?if (temp == NULL) return false;//申請儲存空間失敗 ?? ?bool pd = 0; ?? ?//先是找到這個id再進行刪除 ?? ?//先判斷是不是頭節(jié)點,若不是再把頭節(jié)點當(dāng)首節(jié)點進行使用 ?? ?if (q->front->data.id == message) {//如果刪除頭節(jié)點 ?? ??? ?temp = q->front; ?? ??? ?q->front = q->front->next; ?? ??? ?delete temp; ?? ??? ?temp = NULL; ?? ??? ?pd = 1; ?? ?} ?? ?else if (q->rear->data.id == message) {//如果刪除尾節(jié)點 ?? ??? ?//先找到尾節(jié)點的前一個結(jié)點 ?? ??? ?temp = q->front; ?? ??? ?while (temp->next->data.id != message) temp = temp->next; ?? ??? ?q->rear = temp; ?? ??? ?q->rear->next = NULL; ?? ??? ?pd = 1; ?? ?} ?? ?else {//如果刪除中間節(jié)點 ?? ??? ?temp = q->front; ?? ??? ?while (temp->next != NULL && temp->next->data.id != message) temp = temp->next; ?? ??? ?if (temp->next == NULL) return false;//判斷是不是沒有找到,沒有找到返回false ?? ??? ?Node* mp = new Node(); ?? ??? ?mp = temp->next; ?? ??? ?temp->next = temp->next->next; ?? ??? ?delete mp; ?? ??? ?mp = NULL; ?? ??? ?pd = 1; ?? ?} ?? ?if (pd == 1) { ?? ??? ?Write(q); ?? ??? ?cout << "已成功刪除該學(xué)生信息!" << endl; ?? ??? ?return true; ?? ?} } //通過姓名進行查找? bool FindByName(const string massage, const MyQueue q) {//此函數(shù)只有查找功能,沒有打印功能,打印功能在另一個函數(shù) ?? ?Node* temp = new Node(); ?? ?bool pd = 0; ?? ?if (q->front->data.name == massage) { ?? ??? ?temp = q->front; ?? ??? ?Print(temp); ?? ??? ?return true; ?? ?} ?? ?else { ?? ??? ?temp = q->front; ?? ??? ?while (temp->next != NULL && temp->next->data.name != massage) temp = temp->next; ?? ??? ?if (temp->next == NULL) return false;//沒有找到這個人的姓名,返回false ?? ??? ?Print(temp->next); ?? ??? ?return true; ?? ?} } //單個進行打印? void Print(const Node* q) { ?? ?cout << "該學(xué)生的信息為:" << endl; ?? ?cout << "學(xué)號: " << q->data.id << " 姓名:" << q->data.name << " 電話號碼:" << q->data.num << endl; } //打印全部的學(xué)生信息? void PrintAll(const MyQueue q) { ?? ?cout << "學(xué)號"; ?? ?for (int i = 0; i < 10; i++) { ?? ??? ?cout << "-"; ?? ?} ?? ?cout << "姓名"; ?? ?for (int i = 0; i < 10; i++) { ?? ??? ?cout << "-"; ?? ?} ?? ?cout << "電話號碼" << endl; ?? ?Node* temp; ?? ?temp = q->front; ?? ?while (temp != NULL) { ?? ??? ?cout << " " <<temp->data.id << "?? ? ? ? ?" << temp->data.name << " ? ? ? ? ? " << temp->data.num << endl; ?? ??? ?temp = temp->next; ?? ?} ?? ?//cout << endl; } //實現(xiàn)排序的功能函數(shù)? void Swap(ElementType& x, ElementType& y) { ?? ?ElementType temp; ?? ?temp = x; ?? ?x = y; ?? ?y = temp; } MyQueue BubbleSort(MyQueue q) { ?? ?if (q->front == NULL || q->front->next == NULL) return NULL; ?? ?for (Node* i = q->front; i->next != NULL; i = i->next) { ?? ??? ?for (Node* j = q->front; j->next != NULL; j = j->next) { ?? ??? ??? ?if (j->data.id > j->next->data.id) { ?? ??? ??? ??? ?Swap(j->data, j->next->data); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?return q; } //把全部信息存入到文檔中 void Write(MyQueue q) { ?? ?//先根據(jù)學(xué)號進行排序,再進行存儲 ?? ?q=BubbleSort(q); ?? ?ofstream writeIt; ?? ?writeIt.open("data.txt"); ?? ?if (writeIt.fail()) { ?? ??? ?cout << "該文件沒有找到!" << endl; ?? ??? ?cout << "程序已退出!" << endl; ?? ??? ?exit(1); ?? ?} ?? ?Node* temp = new Node(); ?? ?if (q!= NULL) { ?? ??? ?temp= q->front; ?? ??? ?while (temp != NULL) { ?? ??? ??? ?writeIt << temp->data.id << " " << temp->data.name << " " << temp->data.num << endl;; ?? ??? ??? ?temp = temp->next; ?? ??? ?} ?? ?} ?? ?writeIt.close(); } //從文檔中讀出所有的信息 MyQueue Read() { ?? ?ifstream readIt("data.txt"); ?? ?if (readIt.fail()) { ?? ??? ?cout << "該文件沒有找到!" << endl; ?? ??? ?cout << "程序已退出!" << endl; ?? ??? ?exit(1); ?? ?} ?? ?int id1; ?? ?string name1; ?? ?int num1; ?? ?MyQueue q=new Queue(); ?? ?ElementType x; ?? ?while (!readIt.eof()) { ?? ??? ?readIt >> id1 >> name1 >> num1; ?? ??? ?if (readIt.eof()) break; ?? ??? ?x.id = id1; ?? ??? ?x.name = name1; ?? ??? ?x.num = num1; ?? ??? ?Insert(x, q); ?? ?} ?? ?readIt.close(); ?? ?return q; } //讀入文檔中的信息 MyQueue ReadOrClear(MyQueue& q) { ?? ?q=Read(); ?? ?return q; } //使整個隊列置空 void MakeEmpty(MyQueue& q) { ?? ?while (q->front != NULL) { ?? ??? ?Node* temp = new Node(); ?? ??? ?temp = q->front; ?? ??? ?q->front = q->front->next; ?? ??? ?delete temp; ?? ?} } //主菜單 void Menu(MyQueue q) { ?? ?q=ReadOrClear(q); ?? ?while (1) { ?? ??? ?cout << endl; ?? ??? ?cout << "|--------------------學(xué)生通訊錄系統(tǒng)---------------------|" << endl; ?? ??? ?cout << "|--------------------1 添加學(xué)生信息---------------------|" << endl; ?? ??? ?cout << "|--------------------2 刪除學(xué)生信息---------------------|" << endl; ?? ??? ?cout << "|--------------------3 顯示學(xué)生信息---------------------|" << endl; ?? ??? ?cout << "|--------------------4 查詢學(xué)生信息---------------------|" << endl; ?? ??? ?cout << "|--------------------5 學(xué)生信息排序---------------------|" << endl; ?? ??? ?cout << "|--------------------6 清空屏幕信息---------------------|" << endl; ?? ??? ?cout << "|--------------------7 清空文檔信息---------------------|" << endl; ?? ??? ?cout << "|--------------------8 退出管理系統(tǒng)---------------------|" << endl; ?? ??? ?cout << "|-------------------------------------------------------|" << endl; ?? ??? ?int n; ?? ??? ?cout << "輸入您的選擇:" << endl; ?? ??? ?cin >> n; ?? ??? ?switch (n) { ?? ??? ??? ?case 1: { ?? ??? ??? ??? ?ElementType x; ?? ??? ??? ??? ?cout << "請輸入該學(xué)生的信息:學(xué)號 姓名 電話號碼" << endl; ?? ??? ??? ??? ?cin >> x.id >> x.name >> x.num; ?? ??? ??? ??? ?Insert(x, q); ?? ??? ??? ??? ?Write(q); ?? ??? ??? ??? ?cout << "已成功添加該學(xué)生信息!" << endl; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 2: { ?? ??? ??? ??? ?cout << "請輸入該學(xué)生的學(xué)號:" << endl; ?? ??? ??? ??? ?int num1; ?? ??? ??? ??? ?cin >> num1; ?? ??? ??? ??? ?if (!Delete(num1, q)) { ?? ??? ??? ??? ??? ?cout << "該系統(tǒng)中不存在該學(xué)生!" << endl; ?? ??? ??? ??? ?}; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 3: { ?? ??? ??? ??? ?cout << "正在打印全部學(xué)生信息中.......請稍等!" << endl; ?? ??? ??? ??? ?cout << "全部學(xué)生的信息為:" << endl; ?? ??? ??? ??? ?PrintAll(q); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 4: { ?? ??? ??? ??? ?cout << "請輸入該學(xué)生的姓名:" << endl; ?? ??? ??? ??? ?string name1; ?? ??? ??? ??? ?cin >> name1; ?? ??? ??? ??? ?if (!FindByName(name1, q)) { ?? ??? ??? ??? ??? ?cout << "該系統(tǒng)中不存在該學(xué)生!" << endl; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 5: { ?? ??? ??? ??? ?cout << "正在根據(jù)學(xué)生的學(xué)號對學(xué)生進行排序....." << endl; ?? ??? ??? ??? ?cout << "排完序后,結(jié)果為:" << endl; ?? ??? ??? ??? ?BubbleSort(q); ?? ??? ??? ??? ?PrintAll(q); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 6: { ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 7: { ?? ??? ??? ??? ?cout << "請您在三確認是否要清空文檔中的全部學(xué)生信息!清空請輸入“yes”,不清空請輸入“no”。" << endl; ?? ??? ??? ??? ?string s; ?? ??? ??? ??? ?cin >> s; ?? ??? ??? ??? ?if (s == "yes") {? ?? ??? ??? ??? ??? ?//先把隊列中的全部節(jié)點都delete掉,再進行寫入文檔中 ?? ??? ??? ??? ??? ?MakeEmpty(q); ?? ??? ??? ??? ??? ?q = Init(q); ?? ??? ??? ??? ??? ?Write(q); ?? ??? ??? ??? ??? ?cout << "已經(jīng)成功清空文檔中的全部學(xué)生信息!" << endl; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?case 8: { ?? ??? ??? ??? ?cout << "退出成功!" << endl; ?? ??? ??? ??? ?exit(0); ?? ??? ??? ?} ?? ??? ??? ?default: ?? ??? ??? ??? ?cout << "輸入的選項序號有誤,請重新輸入!" << endl; ?? ??? ?} ?? ?} } int main() { ?? ?MyQueue q; ?? ?q = Init(q); ?? ?Menu(q); ?? ?return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++學(xué)習(xí)小結(jié)之二進制轉(zhuǎn)換
這篇文章主要介紹了C++學(xué)習(xí)小結(jié)之二進制轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2015-07-07C語言模式實現(xiàn)C++繼承和多態(tài)的實例代碼
本篇文章主要介紹了C語言模式實現(xiàn)C++繼承和多態(tài)的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07簡單了解C語言中直接插入排序與直接選擇排序?qū)崿F(xiàn)
這篇文章主要介紹了C語言中直接插入排序與直接選擇排序?qū)崿F(xiàn),插入排序的基本操作就是將一個數(shù)據(jù)插入到已經(jīng)排好序的有序數(shù)據(jù)中,從而得到一個新的、個數(shù)加一的有序數(shù)據(jù),需要的朋友可以參考下2016-03-03