当前位置:首页 C++ > 正文

c++结构体链表

作者:野牛程序员:2023-07-05 07:46:54 C++阅读 2848

当在C++中使用结构体(struct)实现链表时,可以按照以下步骤进行:

首先,定义一个表示链表节点的结构体。结构体应该包含一个数据成员来存储节点的值,以及一个指向下一个节点的指针。

struct Node {
    int data;
    Node* next;
};

然后,可以创建一个链表类,其中包含用于操作链表的各种方法,例如插入节点、删除节点和打印链表等。

class LinkedList {
private:
    Node* head; // 链表头节点指针
public:
    LinkedList() {
        head = nullptr; // 初始化链表为空
    }

    // 在链表末尾插入节点
    void insert(int value) {
        Node* newNode = new Node; // 创建新节点
        newNode->data = value;
        newNode->next = nullptr;

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

    // 打印链表
    void print() {
        Node* current = head;
        while (current != nullptr) {
            cout << current->data << " ";
            current = current->next;
        }
        cout << endl;
    }

    // 删除指定值的节点
    void remove(int value) {
        if (head == nullptr) {
            return;
        }

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

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

使用这个链表类,可以创建链表对象并调用方法来操作链表。

int main() {
    LinkedList list;

    list.insert(5);
    list.insert(10);
    list.insert(15);
    list.insert(20);

    list.print(); // 输出: 5 10 15 20

    list.remove(10);

    list.print(); // 输出: 5 15 20

    return 0;
}


这是一个简单的示例,演示了如何使用结构体实现链表以及如何进行插入、删除和打印操作。你可以根据需要扩展链表类的功能。


下面的方法只用结构体不用类?


如果只想使用结构体而不使用类来实现链表,可以将链表操作封装在一组函数中,而不是封装在类的成员函数中。以下是一个使用结构体的链表示例:

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

// 在链表末尾插入节点
void insert(Node** head, int value) {
    Node* newNode = new Node; // 创建新节点
    newNode->data = value;
    newNode->next = nullptr;

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

// 打印链表
void print(Node* head) {
    Node* current = head;
    while (current != nullptr) {
        cout << current->data << " ";
        current = current->next;
    }
    cout << endl;
}

// 删除指定值的节点
void remove(Node** head, int value) {
    if (*head == nullptr) {
        return;
    }

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

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

int main() {
    Node* head = nullptr;

    insert(&head, 5);
    insert(&head, 10);
    insert(&head, 15);
    insert(&head, 20);

    print(head); // 输出: 5 10 15 20

    remove(&head, 10);

    print(head); // 输出: 5 15 20

    return 0;
}

在这个示例中,我们使用结构体 Node 来表示链表节点,并且使用指向指针的指针 Node** 来传递链表头节点的引用。通过这种方式,我们可以修改链表的头节点,而不仅仅是在函数内部进行修改。

我们定义了 insert 函数来在链表末尾插入节点,print 函数用于打印链表,remove 函数用于删除指定值的节点。

通过在主函数中调用这些函数,我们可以创建并操作链表。

请注意,在使用结构体而不是类时,我们需要手动管理内存,包括分配和释放节点的内存。在实际应用中,请确保适当地释放链表节点的内存,以避免内存泄漏。


在C++中,结构体可以包含成员函数,因此可以将链表操作函数封装为结构体的成员函数。以下是一个使用结构体的链表示例,其中包含结构体内部的成员函数:

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;

    // 在链表末尾插入节点
    void insert(int value) {
        Node* newNode = new Node; // 创建新节点
        newNode->data = value;
        newNode->next = nullptr;

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

    // 打印链表
    void print() {
        Node* current = next;
        while (current != nullptr) {
            cout << current->data << " ";
            current = current->next;
        }
        cout << endl;
    }

    // 删除指定值的节点
    void remove(int value) {
        if (next == nullptr) {
            return;
        }

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

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

int main() {
    Node head;

    head.insert(5);
    head.insert(10);
    head.insert(15);
    head.insert(20);

    head.print(); // 输出: 5 10 15 20

    head.remove(10);

    head.print(); // 输出: 5 15 20

    return 0;
}

在这个示例中,我们将链表操作函数 insertprintremove 定义为结构体 Node 的成员函数。因此,你可以通过链表的头节点来调用这些函数。

在主函数中,我们创建了头节点 head,然后使用头节点调用成员函数来操作链表。

请注意,成员函数可以直接访问结构体的成员变量,因此在这个示例中,我们可以直接访问结构体的 next 成员来表示链表的下一个节点。

这种方法使得链表操作更紧凑,将链表的数据和操作封装在一个结构体中,提供了更直接的方式来操作链表。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击