数据结构之实现单链表的插入,删除,查找等功能

题目描述

(1) 建一个单链表 ,要求:具有插入、删除、查找等功能。

(2) 将5个学生按照学号升序排列顺序存储到LinkList中,学生信息包括name,age等。

(3) 对以上的LinkList执行输入、输出、插入、删除等操作。

源代码

#include <iostream>
using namespace std;

// 学生结构体
struct Student {
    string name;
    int age;
};

// 链表节点结构体
struct Node {
    Student data;
    Node* next;
};

// 单链表类
class LinkedList {
private:
    Node* head; // 头节点指针

public:
    // 构造函数
    LinkedList() {
        head = nullptr;
    }

    // 插入节点
    void insertNode(Student student) {
        Node* newNode = new Node;
        newNode->data = student;
        newNode->next = nullptr;

        if (head == nullptr) {
            head = newNode;
        } else {
            Node* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }
            current->next = newNode;
        }
    }

    // 删除节点
    void deleteNode(string name) {
        if (head == nullptr) {
            return;
        }

        if (head->data.name == name) {
            Node* temp = head;
            head = head->next;
            delete temp;
            return;
        }

        Node* current = head;
        while (current->next != nullptr) {
            if (current->next->data.name == name) {
                Node* temp = current->next;
                current->next = current->next->next;
                delete temp;
                return;
            }
            current = current->next;
        }
    }

    // 查找节点
    Node* searchNode(string name) {
        Node* current = head;
        while (current != nullptr) {
            if (current->data.name == name) {
                return current;
            }
            current = current->next;
        }
        return nullptr;
    }

    // 输出链表内容
    void displayList() {
        Node* current = head;
        while (current != nullptr) {
            cout << "Name: " << current->data.name << ", Age: " << current->data.age << endl;
            current = current->next;
        }
    }
};

int main() {
    LinkedList list;

    // 插入节点
    Student student1 = {"Alice", 20};
    Student student2 = {"Bob", 21};
    Student student3 = {"Charlie", 19};
    Student student4 = {"David", 22};
    Student student5 = {"Eve", 18};

    list.insertNode(student1);
    list.insertNode(student2);
    list.insertNode(student3);
    list.insertNode(student4);
    list.insertNode(student5);

    // 输出链表内容
    cout << "Linked List:" << endl;
    list.displayList();

    // 查找节点
    string searchName = "Charlie";
    Node* searchResult = list.searchNode(searchName);
    if (searchResult != nullptr) {
        cout << "Found student: " << searchResult->data.name << ", Age: " << searchResult->data.age << endl;
    } else {
        cout << "Student not found." << endl;
    }

    // 删除节点
    string deleteName = "Bob";
    list.deleteNode(deleteName);
    cout << "After deleting student " << deleteName << ":" << endl;
    list.displayList();

    return 0;
}

上述代码实现了一个单链表类LinkedList,其中包括插入节点insertNode、删除节点deleteNode、查找节点searchNode和显示链表内容displayList等功能。在主函数中,我们创建了一个LinkedList对象list,并插入了5个学生节点。然后,我们输出链表内容,并演示了查找节点和删除节点的操作。

运行上述代码,将会得到以下输出:

图片[1]-数据结构之实现单链表的插入,删除,查找等功能-QQ沐编程

© 版权声明
THE END
喜欢就支持一下吧
点赞8赞赏 分享