C++ day6

一、练习

使用类模板封装一个链表,模板如下
class List{
public:
    struct node{
        T val;
        node* next;
        node* prev;可选                
    };
private:
    node* head;
    node* tail;
    构造函数
    析构函数
    增删改查排遍历 6个函数
}

#include <iostream>
using namespace std;

template <typename T>
class List {
public:
    // 节点结构
    struct Node {
        T val;
        Node* next;
        Node* prev; // 可选,用于双向链表
        Node(T value) : val(value), next(nullptr), prev(nullptr) {}
    };

private:
    Node* head;
    Node* tail;

public:
    // 构造函数
    List() : head(nullptr), tail(nullptr) {}

    // 析构函数
    ~List() {
        while (head) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }

    // 增:在链表尾部添加节点
    void push_back(T value) {
        Node* newNode = new Node(value);
        if (!head) {
            head = tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }

    // 删:删除链表中第一个值为 value 的节点
    void remove(T value) {
        Node* current = head;
        while (current) {
            if (current->val == value) {
                if (current->prev) {
                    current->prev->next = current->next;
                } else {
                    head = current->next;
                }
                if (current->next) {
                    current->next->prev = current->prev;
                } else {
                    tail = current->prev;
                }
                delete current;
                return;
            }
            current = current->next;
        }
    }

    // 改:修改链表中第一个值为 oldValue 的节点的值为 newValue
    void update(T oldValue, T newValue) {
        Node* current = head;
        while (current) {
            if (current->val == oldValue) {
                current->val = newValue;
                return;
            }
            current = current->next;
        }
    }

    // 查:查找链表中是否存在值为 value 的节点
    bool contains(T value) {
        Node* current = head;
        while (current) {
            if (current->val == value) {
                return true;
            }
            current = current->next;
        }
        return false;
    }

    // 排:对链表进行排序(简单实现冒泡排序)
    void sort() {
        if (!head) return;
        bool swapped;
        do {
            swapped = false;
            Node* current = head;
            while (current->next) {
                if (current->val > current->next->val) {
                    swap(current->val, current->next->val);
                    swapped = true;
                }
                current = current->next;
            }
        } while (swapped);
    }

    // 遍历:打印链表中的所有节点值
    void traverse() {
        Node* current = head;
        while (current) {
            cout << current->val << " ";
            current = current->next;
        }
        cout << endl;
    }
};

int main() {
    List<int> list;
    list.push_back(3);
    list.push_back(1);
    list.push_back(2);
    list.push_back(4);

    cout << "Original List: ";
    list.traverse();

    list.sort();
    cout << "Sorted List: ";
    list.traverse();

    list.remove(2);
    cout << "After Removing 2: ";
    list.traverse();

    list.update(3, 5);
    cout << "After Updating 3 to 5: ";
    list.traverse();

    cout << "Contains 4? " << (list.contains(4) ? "Yes" : "No") << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值