C++实现单链表

花了点时间再次把单链表,以及相关各种操作函数实现了。

#include<iostream>
#include<assert.h>
using namespace std;
template<typename T>
struct Node
{
    Node<T>* _next;
    T _value;
    Node(const T& value = T())
        :_value(value)
        , _next(NULL)
    {}
};
template<typename T>
class LinkList
{
    typedef Node<T> Node;
    friend ostream& operator<<(ostream& os, const LinkList& l);
public:
    LinkList()
        :_head(BuyNode())
    {}
    ~LinkList()
    {
        DestoryList(_head);
    }
    LinkList(const LinkList& l)
        :_head(BuyNode())
    {
        _Copy(_head, l);
    }
    //找第一个值为value的节点
    //返回值这个看自己设计,这里我想让它除了可以判断是否在链表中,如果在的话返回前一个节点方便进行插入
    pair<Node*, bool> find(const T& value)
    {
        assert(_head->_next);
        Node* cur = _head;
        while (cur)
        {
            Node* prev = cur;
            cur = cur->_next;
            if (cur!=NULL&&cur->_value == value)
            {
                return make_pair(prev, true);
            }
        }
        //走到这里证明没找到,直接返回就行
        return make_pair(cur, false);
    }
    //这里通过下标来插入
    void insert(const T& value, int pos)
    {
        Node* cur = _head;
        while (--pos)
        {
            if (cur->_next == NULL)
            {
                cur->_next = BuyNode(value);
                return;
            }
            cur = cur->_next;
        }
        Node* newnode = BuyNode(value);
        newnode->_next = cur->_next;
        cur->_next = newnode;
    }
    //这里是通过节点位置来进行插入
    void insert(const T& value,Node* pos)
    {
        assert(pos);
        Node* newnode = BuyNode(value);
        newnode->_next = pos->_next;
        pos->_next = newnode;
    }
    bool remove(const T& value)//这里就用到了刚才find的返回值
    {
        pair<Node*, bool> ret = find(value);
        if (ret.second == false)
            return false;
        Node* prev = ret.first;
        Node* del = prev->_next;
        prev->_next = del->_next;
        delete del;
        return true;
    }
    void push_front(const T& value)
    {
        Node* cur = BuyNode(value);
        cur->_next = _head->_next;
        _head->_next = cur;
    }
    void pop_front()
    {
        Node* del = _head->_next;
        if (NULL != del)
        {
            _head->_next = del->_next;
        }
        else
        {
            return;
        }
        delete del;
    }
    void push_back(const T& value)
    {
        Node* cur = _head;
        while (cur->_next)
        {
            cur = cur->_next;
        }
        cur->_next = BuyNode(value);
    }
    void pop_back()
    {
        Node* cur = _head;
        Node* parent = NULL;
        while (cur->_next)
        {
            parent = cur;
            cur = cur->_next;
        }
        delete cur;
        parent->_next = NULL;
    }
    T& operator[](int pos)
    {
        Node* cur = _head->_next;
        while (--pos >= 0)
        {
            assert(cur != NULL);
            cur = cur->_next;
        }
        return cur->_value;
    }
    LinkList& operator=(const LinkList& l)
    {
        this->erase();
        _Copy(_head, l);
        return *this;
    }
    Node* GetHead()     //给外界实现一个接口
    {
        return _head;
    }
    size_t size()
    {
        size_t count = 0;
        Node* cur = _head;
        //不包括头节点
        while (cur=cur->_next)
        {
            count++;
        }
        return count;
    }
    bool erase()
    {
        return DestoryList(_head);      
    }
    bool empty()
    {
        return NULL == _head->_next;
    }
protected:
    bool DestoryList(Node* node)
    {
        Node* cur = node->_next;
        if (cur == NULL)
            return false;
        node->_next = NULL;
        while (cur)
        {
            Node* del = cur;
            cur = cur->_next;
            delete del;
        }
        return true;
    }
    void _Copy(Node* head,const LinkList<T>& l)
    {
        Node* cur = l._head->_next;
        Node* newnode = head;
        while (cur)
        {
            newnode->_next = BuyNode(cur->_value);
            newnode = newnode->_next;
            cur = cur->_next;
        }
    }
    Node* BuyNode(const T& value=T())
    {
        Node* node = new Node(value);
        return node;
    }
protected:
    Node* _head;
};
template<typename T>
ostream& operator<<(ostream& os, LinkList<T>& l)
{
    Node<T>* cur = l.GetHead();
    while (cur = cur->_next)
    {
            os << cur->_value << " ";
    }
    os << endl;
    return os;
}

随便实现了下,没有太认真。。。可能里面会有bug,有发现的可以说下,谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值