Untitled

 avatar
unknown
plain_text
a year ago
5.0 kB
5
Indexable
#include <iostream>
using namespace std;

// Định nghĩa cấu trúc Node
struct Node {
    int data;
    Node* next;
};

// Định nghĩa cấu trúc DSLK
struct LinkedList {
    Node* head;
    LinkedList() : head(nullptr) {}

    // Phương thức thêm đầu
    void themDau(int x) {
        Node* newNode = new Node();
        newNode->data = x;
        newNode->next = head;
        head = newNode;
    }

    // Phương thức thêm cuối
    void themCuoi(int x) {
        Node* newNode = new Node();
        newNode->data = x;
        newNode->next = nullptr;
        if (head == nullptr) {
            head = newNode;
        } else {
            Node* temp = head;
            while (temp->next != nullptr) {
                temp = temp->next;
            }
            temp->next = newNode;
        }
    }

    // Phương thức thêm sau một phần tử
    void themSau(int x, int y) {
        Node* temp = head;
        while (temp != nullptr && temp->data != y) {
            temp = temp->next;
        }
        if (temp != nullptr) {
            Node* newNode = new Node();
            newNode->data = x;
            newNode->next = temp->next;
            temp->next = newNode;
        } else {
            cout << "Khong tim thay phan tu " << y << " trong danh sach." << endl;
        }
    }

    // Phương thức kiểm tra phần tử x có tồn tại không
    bool tonTai(int x) {
        Node* temp = head;
        while (temp != nullptr) {
            if (temp->data == x) return true;
            temp = temp->next;
        }
        return false;
    }

    // Phương thức xóa đầu
    void xoaDau() {
        if (head != nullptr) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }

    // Phương thức xóa phần tử sau node q
    void xoaSau(int y) {
        Node* temp = head;
        while (temp != nullptr && temp->data != y) {
            temp = temp->next;
        }
        if (temp != nullptr && temp->next != nullptr) {
            Node* nodeToDelete = temp->next;
            temp->next = temp->next->next;
            delete nodeToDelete;
        } else {
            cout << "Khong co phan tu nao de xoa sau phan tu " << y << "." << endl;
        }
    }

    // Phương thức xuất các phần tử chẵn
    void xuatChan() {
        Node* temp = head;
        while (temp != nullptr) {
            if (temp->data % 2 == 0) {
                cout << temp->data << " ";
            }
            temp = temp->next;
        }
        cout << endl;
    }

    // Phương thức xuất danh sách
    void xuatDanhSach() {
        Node* temp = head;
        while (temp != nullptr) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
};

// Hàm main
int main() {
    LinkedList list;
    int choice, x, y;

    do {
        cout << "\nMenu:\n";
        cout << "1. Them dau\n";
        cout << "2. Them cuoi\n";
        cout << "3. Them sau mot phan tu\n";
        cout << "4. Kiem tra phan tu ton tai\n";
        cout << "5. Xoa dau\n";
        cout << "6. Xoa sau mot phan tu\n";
        cout << "7. Xuat cac phan tu chan\n";
        cout << "8. Xuat danh sach\n";
        cout << "0. Thoat\n";
        cout << "Nhap lua chon: ";
        cin >> choice;

        switch (choice) {
            case 1:
                cout << "Nhap gia tri: ";
                cin >> x;
                list.themDau(x);
                break;
            case 2:
                cout << "Nhap gia tri: ";
                cin >> x;
                list.themCuoi(x);
                break;
            case 3:
                cout << "Nhap gia tri can them: ";
                cin >> x;
                cout << "Nhap gia tri phan tu y: ";
                cin >> y;
                list.themSau(x, y);
                break;
            case 4:
                cout << "Nhap gia tri can kiem tra: ";
                cin >> x;
                if (list.tonTai(x)) {
                    cout << "Phan tu " << x << " ton tai trong danh sach.\n";
                } else {
                    cout << "Phan tu " << x << " khong ton tai trong danh sach.\n";
                }
                break;
            case 5:
                list.xoaDau();
                break;
            case 6:
                cout << "Nhap gia tri phan tu y: ";
                cin >> y;
                list.xoaSau(y);
                break;
            case 7:
                cout << "Cac phan tu chan: ";
                list.xuatChan();
                break;
            case 8:
                cout << "Danh sach: ";
                list.xuatDanhSach();
                break;
            case 0:
                cout << "Thoat chuong trinh.\n";
                break;
            default:
                cout << "Lua chon khong hop le.\n";
                break;
        }
    } while (choice != 0);

    return 0;
}
Editor is loading...
Leave a Comment