網站首頁 編程語言 正文
本文實例為大家分享了C++鏈表實現通訊錄設計的具體代碼,供大家參考,具體內容如下
功能如下:
1添加學生信息
2刪除學生信息
3顯示學生信息
4查詢學生信息
5學生信息排序
6清空屏幕信息
7清空文檔信息
8退出管理系統
上代碼!
#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;//這里需要改成需要的內容,最好(必須)改成一個函數的形式,賦值的時候調用函數,打印的時候也調用函數
?? ?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再進行刪除
?? ?//先判斷是不是頭節點,若不是再把頭節點當首節點進行使用
?? ?if (q->front->data.id == message) {//如果刪除頭節點
?? ??? ?temp = q->front;
?? ??? ?q->front = q->front->next;
?? ??? ?delete temp;
?? ??? ?temp = NULL;
?? ??? ?pd = 1;
?? ?}
?? ?else if (q->rear->data.id == message) {//如果刪除尾節點
?? ??? ?//先找到尾節點的前一個結點
?? ??? ?temp = q->front;
?? ??? ?while (temp->next->data.id != message) temp = temp->next;
?? ??? ?q->rear = temp;
?? ??? ?q->rear->next = NULL;
?? ??? ?pd = 1;
?? ?}
?? ?else {//如果刪除中間節點
?? ??? ?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 << "已成功刪除該學生信息!" << endl;
?? ??? ?return true;
?? ?}
}
//通過姓名進行查找?
bool FindByName(const string massage, const MyQueue q) {//此函數只有查找功能,沒有打印功能,打印功能在另一個函數
?? ?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 << "該學生的信息為:" << endl;
?? ?cout << "學號: " << q->data.id << " 姓名:" << q->data.name << " 電話號碼:" << q->data.num << endl;
}
//打印全部的學生信息?
void PrintAll(const MyQueue q) {
?? ?cout << "學號";
?? ?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;
}
//實現排序的功能函數?
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) {
?? ?//先根據學號進行排序,再進行存儲
?? ?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 << "|--------------------學生通訊錄系統---------------------|" << endl;
?? ??? ?cout << "|--------------------1 添加學生信息---------------------|" << endl;
?? ??? ?cout << "|--------------------2 刪除學生信息---------------------|" << endl;
?? ??? ?cout << "|--------------------3 顯示學生信息---------------------|" << endl;
?? ??? ?cout << "|--------------------4 查詢學生信息---------------------|" << endl;
?? ??? ?cout << "|--------------------5 學生信息排序---------------------|" << endl;
?? ??? ?cout << "|--------------------6 清空屏幕信息---------------------|" << endl;
?? ??? ?cout << "|--------------------7 清空文檔信息---------------------|" << endl;
?? ??? ?cout << "|--------------------8 退出管理系統---------------------|" << endl;
?? ??? ?cout << "|-------------------------------------------------------|" << endl;
?? ??? ?int n;
?? ??? ?cout << "輸入您的選擇:" << endl;
?? ??? ?cin >> n;
?? ??? ?switch (n) {
?? ??? ??? ?case 1: {
?? ??? ??? ??? ?ElementType x;
?? ??? ??? ??? ?cout << "請輸入該學生的信息:學號 姓名 電話號碼" << endl;
?? ??? ??? ??? ?cin >> x.id >> x.name >> x.num;
?? ??? ??? ??? ?Insert(x, q);
?? ??? ??? ??? ?Write(q);
?? ??? ??? ??? ?cout << "已成功添加該學生信息!" << endl;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 2: {
?? ??? ??? ??? ?cout << "請輸入該學生的學號:" << endl;
?? ??? ??? ??? ?int num1;
?? ??? ??? ??? ?cin >> num1;
?? ??? ??? ??? ?if (!Delete(num1, q)) {
?? ??? ??? ??? ??? ?cout << "該系統中不存在該學生!" << endl;
?? ??? ??? ??? ?};
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 3: {
?? ??? ??? ??? ?cout << "正在打印全部學生信息中.......請稍等!" << endl;
?? ??? ??? ??? ?cout << "全部學生的信息為:" << endl;
?? ??? ??? ??? ?PrintAll(q);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 4: {
?? ??? ??? ??? ?cout << "請輸入該學生的姓名:" << endl;
?? ??? ??? ??? ?string name1;
?? ??? ??? ??? ?cin >> name1;
?? ??? ??? ??? ?if (!FindByName(name1, q)) {
?? ??? ??? ??? ??? ?cout << "該系統中不存在該學生!" << endl;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 5: {
?? ??? ??? ??? ?cout << "正在根據學生的學號對學生進行排序....." << endl;
?? ??? ??? ??? ?cout << "排完序后,結果為:" << endl;
?? ??? ??? ??? ?BubbleSort(q);
?? ??? ??? ??? ?PrintAll(q);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 6: {
?? ??? ??? ??? ?system("cls");
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 7: {
?? ??? ??? ??? ?cout << "請您在三確認是否要清空文檔中的全部學生信息!清空請輸入“yes”,不清空請輸入“no”。" << endl;
?? ??? ??? ??? ?string s;
?? ??? ??? ??? ?cin >> s;
?? ??? ??? ??? ?if (s == "yes") {?
?? ??? ??? ??? ??? ?//先把隊列中的全部節點都delete掉,再進行寫入文檔中
?? ??? ??? ??? ??? ?MakeEmpty(q);
?? ??? ??? ??? ??? ?q = Init(q);
?? ??? ??? ??? ??? ?Write(q);
?? ??? ??? ??? ??? ?cout << "已經成功清空文檔中的全部學生信息!" << endl;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 8: {
?? ??? ??? ??? ?cout << "退出成功!" << endl;
?? ??? ??? ??? ?exit(0);
?? ??? ??? ?}
?? ??? ??? ?default:
?? ??? ??? ??? ?cout << "輸入的選項序號有誤,請重新輸入!" << endl;
?? ??? ?}
?? ?}
}
int main() {
?? ?MyQueue q;
?? ?q = Init(q);
?? ?Menu(q);
?? ?return 0;
}
原文鏈接:https://blog.csdn.net/cristemw/article/details/109959848
相關推薦
- 2022-10-03 Redis之SDS數據結構的使用_Redis
- 2023-05-21 詳解Python中文件路徑_python
- 2022-06-23 Python+Turtle制作獨特的表白圖_python
- 2022-03-20 ubuntu開機自啟動服務設置_Linux
- 2022-09-19 Redis緩存穿透/擊穿工具類的封裝_Redis
- 2023-07-28 獲取當前日期以及前6天的日期集合
- 2022-10-28 Python入門之列表用法詳解_python
- 2021-11-02 Linux環境下生成openssl證書注意細節介紹_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支