从造轮子做起: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;
};



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

猜你喜欢

转载自blog.csdn.net/liuchenxia8/article/details/81734947