SoFunction
Updated on 2025-04-06

C++ linked list implements address book design

This article shares the specific code for C++ linked list implementation address book design for your reference. The specific content is as follows

The functions are as follows:

1Add student information
2 Delete student information
3Display student information
4. Query student information
5 Student Information Sorting
6 Clear the screen information
7 Clear document information
8 Exit the management system

Upload the code!

#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>//Read and write the header file of the fileusing 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&amp; 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&amp; q);//Whether to empty all the information 
void Swap(ElementType&amp; x, ElementType&amp; y);//Swap functions in sort
MyQueue BubbleSort(MyQueue q);//Sort by student ID using bubble sort 
void Menu(MyQueue q);//main menu

//Initialize the queueMyQueue Init(MyQueue&amp; q) {
    q = new Queue();
    if (q == NULL) return NULL;
    q-&gt;front = NULL;
    q-&gt;rear = NULL;
    return q;
}

// Check whether the queue is emptybool IsEmpty(MyQueue q) {
    return q-&gt;front == NULL;
}

//Add informationbool Insert(ElementType x, MyQueue q) {
    Node* temp = new Node();
    if (temp == NULL) return false;
    temp-&gt;data = x;//It is necessary to change it to the required content here. It is best to change it to a function form. The function is called when assigning, and the function is also called when printing.    temp-&gt;next = NULL;
    if (IsEmpty(q)) {
        q-&gt;front = temp;
        q-&gt;rear = temp;
        return true;
    }
    else {
        q-&gt;rear-&gt;next = temp;
        q-&gt;rear = temp;
        return true;
    }
}

//Delete functionbool Delete(const int message, MyQueue q) {
    Node* temp = new Node();
    if (temp == NULL) return false;//Application for storage space failed    bool pd = 0;
    //First find this id and then delete it    //First determine whether it is the head node. If you don't use the head node as the head node, then use it as the head node.    if (q-&gt;front-&gt; == message) {//If the header node is deleted        temp = q-&gt;front;
        q-&gt;front = q-&gt;front-&gt;next;
        delete temp;
        temp = NULL;
        pd = 1;
    }
    else if (q-&gt;rear-&gt; == message) {//If the tail node is deleted        //First find the previous node of the tail node        temp = q-&gt;front;
        while (temp-&gt;next-&gt; != message) temp = temp-&gt;next;
        q-&gt;rear = temp;
        q-&gt;rear-&gt;next = NULL;
        pd = 1;
    }
    else {//If you delete the intermediate node        temp = q-&gt;front;
        while (temp-&gt;next != NULL &amp;&amp; temp-&gt;next-&gt; != message) temp = temp-&gt;next;
        if (temp-&gt;next == NULL) return false;//Judge whether it is not found or not, return false        Node* mp = new Node();
        mp = temp-&gt;next;
        temp-&gt;next = temp-&gt;next-&gt;next;
        delete mp;
        mp = NULL;
        pd = 1;
    }
    if (pd == 1) {
        Write(q);
        cout &lt;&lt; "This student information has been successfully deleted!" &lt;&lt; endl;
        return true;
    }
}

//Search by namebool FindByName(const string massage, const MyQueue q) {//This function only has the search function, no printing function, the printing function is in another function    Node* temp = new Node();
    bool pd = 0;
    if (q-&gt;front-&gt; == massage) {
        temp = q-&gt;front;
        Print(temp);
        return true;
    }
    else {
        temp = q-&gt;front;
        while (temp-&gt;next != NULL &amp;&amp; temp-&gt;next-&gt; != massage) temp = temp-&gt;next;
        if (temp-&gt;next == NULL) return false;//The name of this person is not found, return false        Print(temp-&gt;next);
        return true;
    }
}

//Print in singlevoid Print(const Node* q) {
    cout &lt;&lt; "The student's information is:" &lt;&lt; endl;
    cout &lt;&lt; "Student number: " &lt;&lt; q-&gt; &lt;&lt; " Name:" &lt;&lt; q-&gt; &lt;&lt; " telephone number:" &lt;&lt; q-&gt; &lt;&lt; endl;
}

//Print all student informationvoid PrintAll(const MyQueue q) {
    cout &lt;&lt; "Student Number";
    for (int i = 0; i &lt; 10; i++) {
        cout &lt;&lt; "-";
    }
    cout &lt;&lt; "Name";
    for (int i = 0; i &lt; 10; i++) {
        cout &lt;&lt; "-";
    }
    cout &lt;&lt; "telephone number" &lt;&lt; endl;

    Node* temp;
    temp = q-&gt;front;
    while (temp != NULL) {
        cout &lt;&lt; " " &lt;&lt;temp-&gt; &lt;&lt; "          " &lt;&lt; temp-&gt; &lt;&lt; "           " &lt;&lt; temp-&gt; &lt;&lt; endl;
        temp = temp-&gt;next;
    }
    //cout &lt;&lt; endl;
}

// Function function to implement sortingvoid Swap(ElementType&amp; x, ElementType&amp; y) {
    ElementType temp;
    temp = x;
    x = y;
    y = temp;
}

MyQueue BubbleSort(MyQueue q) {
    if (q-&gt;front == NULL || q-&gt;front-&gt;next == NULL) return NULL;
    for (Node* i = q-&gt;front; i-&gt;next != NULL; i = i-&gt;next) {
        for (Node* j = q-&gt;front; j-&gt;next != NULL; j = j-&gt;next) {
            if (j-&gt; &gt; j-&gt;next-&gt;) {
                Swap(j-&gt;data, j-&gt;next-&gt;data);
            }
        }
    }
    return q;
}

//Save all the information into the documentvoid Write(MyQueue q) {
    //Sort first according to the student number, and then store it    q=BubbleSort(q);
    ofstream writeIt;
    ("");
    if (()) {
        cout &lt;&lt; "The file was not found!" &lt;&lt; endl;
        cout &lt;&lt; "The program has been exited!" &lt;&lt; endl;
        exit(1);
    }

    Node* temp = new Node();
    if (q!= NULL) {
        temp= q-&gt;front;
        while (temp != NULL) {
            writeIt &lt;&lt; temp-&gt; &lt;&lt; " " &lt;&lt; temp-&gt; &lt;&lt; " " &lt;&lt; temp-&gt; &lt;&lt; endl;;
            temp = temp-&gt;next;
        }
    }
    ();
}

//Read all the information from the documentMyQueue Read() {
    ifstream readIt("");
    if (()) {
        cout &lt;&lt; "The file was not found!" &lt;&lt; endl;
        cout &lt;&lt; "The program has been exited!" &lt;&lt; endl;
        exit(1);
    }
    int id1;
    string name1;
    int num1;
    MyQueue q=new Queue();
    ElementType x;
    while (!()) {
        readIt &gt;&gt; id1 &gt;&gt; name1 &gt;&gt; num1;
        if (()) break;
         = id1;
         = name1;
         = num1;
        Insert(x, q);
    }
    ();
    return q;
}

//Read the information into the documentMyQueue ReadOrClear(MyQueue&amp; q) {
    q=Read();
    return q;
}

//Empty the entire queuevoid MakeEmpty(MyQueue&amp; q) {
    while (q-&gt;front != NULL) {
        Node* temp = new Node();
        temp = q-&gt;front;
        q-&gt;front = q-&gt;front-&gt;next;
        delete temp;
    }
}

// Main menuvoid Menu(MyQueue q) {
    q=ReadOrClear(q);
    while (1) {
        cout &lt;&lt; endl;
        cout &lt;&lt; "|----------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|----------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|----------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|----------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- &lt;&lt; endl;
        cout &lt;&lt; "|-------------------------------------------------------|" &lt;&lt; endl;
        int n;
        cout &lt;&lt; "Enter your selection:" &lt;&lt; endl;
        cin &gt;&gt; n;
        switch (n) {
            case 1: {
                ElementType x;
                cout &lt;&lt; "Please enter the information of this student: student number, name, phone number" &lt;&lt; endl;
                cin &gt;&gt;  &gt;&gt;  &gt;&gt; ;
                Insert(x, q);
                Write(q);
                cout &lt;&lt; "This student information has been successfully added!" &lt;&lt; endl;
                break;
            }
            case 2: {
                cout &lt;&lt; "Please enter the student number:" &lt;&lt; endl;
                int num1;
                cin &gt;&gt; num1;
                if (!Delete(num1, q)) {
                    cout &lt;&lt; "The student does not exist in this system!" &lt;&lt; endl;
                };
                break;
            }
            case 3: {
                cout &lt;&lt; "All student information is being printed...please wait!" &lt;&lt; endl;
                cout &lt;&lt; "The information for all students is:" &lt;&lt; endl;
                PrintAll(q);
                break;
            }
            case 4: {
                cout &lt;&lt; "Please enter the student's name:" &lt;&lt; endl;
                string name1;
                cin &gt;&gt; name1;
                if (!FindByName(name1, q)) {
                    cout &lt;&lt; "The student does not exist in this system!" &lt;&lt; endl;
                }
                break;
            }
            case 5: {
                cout &lt;&lt; "Sorting students by their student number..." &lt;&lt; endl;
                cout &lt;&lt; "After sorting, the result is:" &lt;&lt; endl;
                BubbleSort(q);
                PrintAll(q);
                break;
            }
            case 6: {
                system("cls");
                break;
            }
            case 7: {
                cout &lt;&lt; "Please confirm in the third to confirm whether you want to clear all student information in the document! Please enter "yes" if you clear it, and if you don't clear it, please enter "no"." &lt;&lt; endl;
                string s;
                cin &gt;&gt; s;
                if (s == "yes") { 
                    // First delete all nodes in the queue, and then write them to the document                    MakeEmpty(q);
                    q = Init(q);
                    Write(q);
                    cout &lt;&lt; "All student information in the document has been successfully cleared!" &lt;&lt; endl;
                }
                break;
            }
            case 8: {
                cout &lt;&lt; "Exit successfully!" &lt;&lt; endl;
                exit(0);
            }
            default:
                cout &lt;&lt; "The number of the option entered is incorrect, please re-enter!" &lt;&lt; endl;
        }
    }
}

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.