亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)

 更新時(shí)間:2022年06月21日 08:32:43   作者:算法之路慢慢兮,吾將上下而求索  
這篇文章主要為大家詳細(xì)介紹了C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++鏈表實(shí)現(xiàn)通訊錄設(shè)計(jì)的具體代碼,供大家參考,具體內(nèi)容如下

功能如下:

1添加學(xué)生信息
2刪除學(xué)生信息
3顯示學(xué)生信息
4查詢(xún)學(xué)生信息
5學(xué)生信息排序
6清空屏幕信息
7清空文檔信息
8退出管理系統(tǒng)

上代碼!

#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>//讀寫(xiě)文件的頭文件
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

//初始化隊(duì)列?
MyQueue Init(MyQueue& q) {
?? ?q = new Queue();
?? ?if (q == NULL) return NULL;
?? ?q->front = NULL;
?? ?q->rear = NULL;
?? ?return q;
}

//查看隊(duì)列是否為空?
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)容,最好(必須)改成一個(gè)函數(shù)的形式,賦值的時(shí)候調(diào)用函數(shù),打印的時(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;//申請(qǐng)儲(chǔ)存空間失敗
?? ?bool pd = 0;
?? ?//先是找到這個(gè)id再進(jìn)行刪除
?? ?//先判斷是不是頭節(jié)點(diǎn),若不是再把頭節(jié)點(diǎn)當(dāng)首節(jié)點(diǎn)進(jìn)行使用
?? ?if (q->front->data.id == message) {//如果刪除頭節(jié)點(diǎn)
?? ??? ?temp = q->front;
?? ??? ?q->front = q->front->next;
?? ??? ?delete temp;
?? ??? ?temp = NULL;
?? ??? ?pd = 1;
?? ?}
?? ?else if (q->rear->data.id == message) {//如果刪除尾節(jié)點(diǎn)
?? ??? ?//先找到尾節(jié)點(diǎn)的前一個(gè)結(jié)點(diǎn)
?? ??? ?temp = q->front;
?? ??? ?while (temp->next->data.id != message) temp = temp->next;
?? ??? ?q->rear = temp;
?? ??? ?q->rear->next = NULL;
?? ??? ?pd = 1;
?? ?}
?? ?else {//如果刪除中間節(jié)點(diǎn)
?? ??? ?temp = q->front;
?? ??? ?while (temp->next != NULL && temp->next->data.id != message) temp = temp->next;
?? ??? ?if (temp->next == NULL) return false;//判斷是不是沒(méi)有找到,沒(méi)有找到返回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;
?? ?}
}

//通過(guò)姓名進(jìn)行查找?
bool FindByName(const string massage, const MyQueue q) {//此函數(shù)只有查找功能,沒(méi)有打印功能,打印功能在另一個(gè)函數(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;//沒(méi)有找到這個(gè)人的姓名,返回false
?? ??? ?Print(temp->next);
?? ??? ?return true;
?? ?}
}

//單個(gè)進(jìn)行打印?
void Print(const Node* q) {
?? ?cout << "該學(xué)生的信息為:" << endl;
?? ?cout << "學(xué)號(hào): " << q->data.id << " 姓名:" << q->data.name << " 電話(huà)號(hào)碼:" << q->data.num << endl;
}

//打印全部的學(xué)生信息?
void PrintAll(const MyQueue q) {
?? ?cout << "學(xué)號(hào)";
?? ?for (int i = 0; i < 10; i++) {
?? ??? ?cout << "-";
?? ?}
?? ?cout << "姓名";
?? ?for (int i = 0; i < 10; i++) {
?? ??? ?cout << "-";
?? ?}
?? ?cout << "電話(huà)號(hào)碼" << 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;
}

//實(shí)現(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é)號(hào)進(jìn)行排序,再進(jìn)行存儲(chǔ)
?? ?q=BubbleSort(q);
?? ?ofstream writeIt;
?? ?writeIt.open("data.txt");
?? ?if (writeIt.fail()) {
?? ??? ?cout << "該文件沒(méi)有找到!" << 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 << "該文件沒(méi)有找到!" << 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;
}

//使整個(gè)隊(duì)列置空
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 查詢(xún)學(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 << "請(qǐng)輸入該學(xué)生的信息:學(xué)號(hào) 姓名 電話(huà)號(hào)碼" << endl;
?? ??? ??? ??? ?cin >> x.id >> x.name >> x.num;
?? ??? ??? ??? ?Insert(x, q);
?? ??? ??? ??? ?Write(q);
?? ??? ??? ??? ?cout << "已成功添加該學(xué)生信息!" << endl;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 2: {
?? ??? ??? ??? ?cout << "請(qǐng)輸入該學(xué)生的學(xué)號(hào):" << endl;
?? ??? ??? ??? ?int num1;
?? ??? ??? ??? ?cin >> num1;
?? ??? ??? ??? ?if (!Delete(num1, q)) {
?? ??? ??? ??? ??? ?cout << "該系統(tǒng)中不存在該學(xué)生!" << endl;
?? ??? ??? ??? ?};
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 3: {
?? ??? ??? ??? ?cout << "正在打印全部學(xué)生信息中.......請(qǐng)稍等!" << endl;
?? ??? ??? ??? ?cout << "全部學(xué)生的信息為:" << endl;
?? ??? ??? ??? ?PrintAll(q);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 4: {
?? ??? ??? ??? ?cout << "請(qǐng)輸入該學(xué)生的姓名:" << endl;
?? ??? ??? ??? ?string name1;
?? ??? ??? ??? ?cin >> name1;
?? ??? ??? ??? ?if (!FindByName(name1, q)) {
?? ??? ??? ??? ??? ?cout << "該系統(tǒng)中不存在該學(xué)生!" << endl;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 5: {
?? ??? ??? ??? ?cout << "正在根據(jù)學(xué)生的學(xué)號(hào)對(duì)學(xué)生進(jìn)行排序....." << endl;
?? ??? ??? ??? ?cout << "排完序后,結(jié)果為:" << endl;
?? ??? ??? ??? ?BubbleSort(q);
?? ??? ??? ??? ?PrintAll(q);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 6: {
?? ??? ??? ??? ?system("cls");
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 7: {
?? ??? ??? ??? ?cout << "請(qǐng)您在三確認(rèn)是否要清空文檔中的全部學(xué)生信息!清空請(qǐng)輸入“yes”,不清空請(qǐng)輸入“no”。" << endl;
?? ??? ??? ??? ?string s;
?? ??? ??? ??? ?cin >> s;
?? ??? ??? ??? ?if (s == "yes") {?
?? ??? ??? ??? ??? ?//先把隊(duì)列中的全部節(jié)點(diǎn)都delete掉,再進(jìn)行寫(xiě)入文檔中
?? ??? ??? ??? ??? ?MakeEmpty(q);
?? ??? ??? ??? ??? ?q = Init(q);
?? ??? ??? ??? ??? ?Write(q);
?? ??? ??? ??? ??? ?cout << "已經(jīng)成功清空文檔中的全部學(xué)生信息!" << endl;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?case 8: {
?? ??? ??? ??? ?cout << "退出成功!" << endl;
?? ??? ??? ??? ?exit(0);
?? ??? ??? ?}
?? ??? ??? ?default:
?? ??? ??? ??? ?cout << "輸入的選項(xiàng)序號(hào)有誤,請(qǐng)重新輸入!" << endl;
?? ??? ?}
?? ?}
}

int main() {
?? ?MyQueue q;
?? ?q = Init(q);
?? ?Menu(q);
?? ?return 0;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論