c++实现一个简单的链表

2 篇文章 0 订阅
#include <iostream>
#include <string>

using namespace std;

class Node
{
public:
    Node(int id, string name)
    {
        _id = id;
        _name = name;
        _next = nullptr;
    }

    ~Node() {}

public:
    Node *_next;
    int _id;
    string _name;
};

template <class T>
class LinkList
{
public:
    LinkList(T *node)
    {
        _node = node;
        _length = 1;
    }
    ~LinkList()
    {
        T *p = _node;
        while (p)
        {
            p = _node->_next;
            delete _node;
            _node = p;
        }
        _node = nullptr;
        _length = 0;
    }

    /**
     * @brief 得到链表长度
     *
     * @return size_t
     */
    size_t length() { return _length; }

    /**
     * @brief 指定位置插入节点
     *
     * @param node 需要插入的节点
     * @param position  插入的位置,默认为头插法
     * @return   当前链表的长度
     */
    size_t insert(T *node, size_t position = 0);

    /**
     * @brief 查找节点
     *
     * @param node
     * @return 返回查找到的节点
     */
    T *find(T *node);

    /**
     * @brief 删除某个节点
     *
     * @param node
     * @return true
     * @return false
     */
    bool delete_(T *node);

    /**
     * @brief Get the Node Ptr object
     *
     * @return T*
     */
    T *getNodePtr() { return _node; }

private:
    //头节点
    T *_node;
    //长度
    size_t _length;
};

template <class T>
size_t LinkList<T>::insert(T *node, size_t position)
{
    //插入位置无效
    if ((position > _length) || (find(node)))
    {
        return 0;
    }
    //在头部插入
    if (position == 0)
    {
        node->_next = _node;
        _node = node;
        ++_length;
        return _length;
    }
    //在尾部插入
    if (position == _length)
    {
        //寻找尾节点
        T *p = _node;
        while (p->_next)
        {
            p = p->_next;
        }
        p->_next = node;
        ++_length;
        return _length;
    }
    //在中部插入
    if (position < _length)
    {
        //在中间插入
        size_t length = 0;
        //寻找要插入的节点
        T *p = _node;
        while (p->_next)
        {
            ++length;
            p = p->_next;
            if (position == length)
            {
                break;
            }
        }
        node->_next = p->_next;
        p->_next = node;
        ++_length;
        return _length;
    }
}

template <class T>
T *LinkList<T>::find(T *node)
{
    T *p = _node;
    if (p == node)
    {
        //头节点判断
        return p;
    }

    while (p)
    {
        p = p->_next;
        if (p == node)
        {
            return p;
        }
    }

    return nullptr;
}

template <class T>
bool LinkList<T>::delete_(T *node)
{
    // p为查找到的节点
    T *p = find(node);
    if (p == _node)
    {
        //头节点直接删除
        _node = p->_next;
        --_length;
        delete p;
        return true;
    }

    T *_p = _node;
    if (p)
    {
        //定位到需要删除的前一个节点
        while (_p->_next)
        {
            //_p为需要删除的前一个节点
            if (_p->_next == node)
            {
                //非尾节点
                if (_p->_next->_next)
                {
                    //保存要删除的节点的后一个节点
                    T *s = _p->_next->_next;
                    delete _p->_next;
                    --_length;
                    _p->_next = s;
                    return true;
                }
                else
                {
                    //尾节点
                    delete _p->_next;
                    _p->_next = nullptr;
                    --_length;
                    return true;
                }
            }
            _p = _p->_next;
        }
    }

    return false;
}

int main(int argc, char *argv[])
{
    LinkList<Node> list(new Node(1, "1__"));
    Node *node2 = new Node(2, "2__");
    Node *node3 = new Node(3, "3__");
    Node *node4 = new Node(4, "4__");
    Node *node5 = new Node(5, "5__");
    Node *node6 = new Node(6, "6__");
    Node *node7 = new Node(7, "7__");
    Node *node8 = new Node(8, "8__");
    list.insert(node2, list.length());
    list.insert(node3, list.length());
    list.insert(node4, list.length());
    list.insert(node5, list.length());
    list.insert(node5, list.length());
    list.insert(node4, list.length());
    list.insert(node8);
    list.delete_(node8);
    cout << list.length() << endl;
    cout << list.find(node8) << endl;
    Node *p = list.getNodePtr();
    while (p)
    {
        cout << p->_id << " | " << p->_name << endl;
        p = p->_next;
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值