C++でクラスを使用してリストを実装するにはどうすればよいですか?

リンクリストを実装するためには、まずノードクラスを定義してリストのノードを表し、次にそれらのノードを操作するリストクラスを定義する必要があります。

以下是一个使用クラスを使ってリストを実装する例です:

#include <iostream>

// 链表节点类
class Node {
public:
    int data;
    Node* next;
    
    Node(int value) {
        data = value;
        next = nullptr;
    }
};

// 链表类
class LinkedList {
private:
    Node* head;  // 链表头指针
public:
    LinkedList() {
        head = nullptr;
    }
    
    // 在链表尾部插入一个节点
    void append(int value) {
        Node* newNode = new Node(value);
        
        if (head == nullptr) {
            head = newNode;
        } else {
            Node* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }
            current->next = newNode;
        }
    }
    
    // 在链表指定位置插入一个节点
    void insert(int value, int index) {
        if (index < 0) {
            std::cout << "Invalid index!" << std::endl;
            return;
        }
        
        Node* newNode = new Node(value);
        
        if (index == 0) {
            newNode->next = head;
            head = newNode;
        } else {
            Node* current = head;
            for (int i = 0; i < index - 1; i++) {
                if (current == nullptr) {
                    std::cout << "Invalid index!" << std::endl;
                    return;
                }
                current = current->next;
            }
            newNode->next = current->next;
            current->next = newNode;
        }
    }
    
    // 删除链表指定位置的节点
    void remove(int index) {
        if (index < 0) {
            std::cout << "Invalid index!" << std::endl;
            return;
        }
        
        if (index == 0) {
            Node* temp = head;
            head = head->next;
            delete temp;
        } else {
            Node* current = head;
            for (int i = 0; i < index - 1; i++) {
                if (current == nullptr || current->next == nullptr) {
                    std::cout << "Invalid index!" << std::endl;
                    return;
                }
                current = current->next;
            }
            Node* temp = current->next;
            current->next = temp->next;
            delete temp;
        }
    }
    
    // 打印链表中的所有节点值
    void print() {
        Node* current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }
};

int main() {
    LinkedList myList;
    
    myList.append(1);
    myList.append(2);
    myList.append(3);
    
    myList.insert(4, 1);
    
    myList.print();  // 输出:1 4 2 3
    
    myList.remove(2);
    
    myList.print();  // 输出:1 4 3
    
    return 0;
}

上記の例で、最初にNodeクラスを定義しました。このクラスには整数データメンバdataと次のノードを指すポインタnextが含まれています。

その後、私たちはLinkedListというリストクラスを定義しました。このクラスには、リストの先頭を指すheadポインタが含まれています。このリストクラスでは、リストの末尾にノードを挿入したり、指定位置にノードを挿入したり、指定位置のノードを削除したり、リスト内のすべてのノードを印刷するなど、いくつかの基本的なリスト操作が実装されています。

main関数内で、リストオブジェクトmyListを作成し、いくつかの挿入、削除、および印刷操作を実行し、最後にリストオブジェクトを破棄します。

上記のサンプルコードを実行すると、以下の結果が出力されます:

1 4 2 3
1 4 3

このことは、クラスを使用して実装されたリンクリストが正常に動作することを示しています。

bannerAds