从造轮子做起:List

今天来造一个简单版本的 List 。

#include <iostream>
using namespace std;

template <class T>
struct ListNode{
    T _val;
    ListNode* _next;
    ListNode(T val)
        :_val(val)
        , _next(nullptr)
    {}
};

template <class T>
class List {
    typedef ListNode<T> Node;
public:
    List() 
        :_head(nullptr)
    {}

    List(const List& l) 
        :_head(nullptr) {
        Node* cur = l._head;
        while (cur != nullptr) {
            Push_Back(cur->_val);
            cur = cur->_next;
        }
    }

    List(List&& l) 
        :_head(l._head){
        l._head = nullptr;
    }

    void Push_Back(T val) {
        if (_head == nullptr) {
            _head = new Node(val);
        }
        else {
            Node* cur = _head;
            while (cur->_next != nullptr)
                cur = cur->_next;
            cur->_next = new Node(val);
        }
    }

    void Pop_Back() {
        if (_head == nullptr)
            return;

        Node* fast = _head->_next;
        Node* slow = _head;
        while (fast->_next != nullptr) {
            fast = fast->_next;
            slow = slow->_next;
        }
        delete fast;
        slow->_next = nullptr;
    }

    void Push_Front(T val) {
        Node* cur = _head;
        _head = new Node(val);
        _head->_next = cur;
    }

    void Pop_Front() {
        if (_head == nullptr)
            return;
        Node* cur = _head;
        _head = _head->_next;
        delete cur;
    }

    void Insert(Node* pos, T val) {
        if (_head == nullptr)
            return;

        Node* cur = _head;
        while (cur->_next != nullptr) {
            if (cur == pos)
                break;
            cur = cur->_next;
        }
        Node* next = cur->_next;
        cur->_next = new Node(cur->_next);
        cur->_next->_next = next;
        cur->_val = val;
    }

    List& operator=(List l) {
        swap(_head, l._head);
        return *this;
    }

    bool isEmpty() {
        return _head == nullptr;
    }

    void Erase(Node* pos) {
        if (pos == nullptr || _head == nullptr)
            return;

        if (_head == pos) {
            Node* cur = _head->_next;
            delete _head;
            _head = cur;
        }
        else {
            Node* fast = _head->_next;
            Node* slow = _head;
            while (fast != nullptr) {
                if (fast == pos)
                    break;
                fast = fast->_next;
                slow = slow->_next;
            }
            if (fast == nullptr)
                return;
            slow->_next = fast->_next;
            delete fast;
        }
    }

    ~List() {
        if (_head == nullptr)
            return;
        Node* cur = _head;
        while (cur != nullptr) {
            Node* next = cur->_next;
            delete cur;
            cur = next;
        }
        _head = nullptr;
    }

    Node* Find(T val) {
        if (_head == nullptr)
            return;

        Node* cur = _head;
        while (cur != NULL) {
            if (cur->_val = val)
                break;
            cur = cur->_next;
        }
        return cur;
    }

    void TestPrint() {
        if (_head == nullptr)
            return;
        Node* cur = _head;
        while (cur != NULL) {
            cout << cur->_val << " ";
            cur = cur->_next;
        }
        cout << endl;
    }

private:
    Node* _head;
};



欢迎大家共同讨论,如有错误及时联系作者指出,并改正。谢谢大家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值