C++模板类实现单链表

c++模板类实现单链表

#include<iostream>
using namespace std;
#include<assert.h>
#include<string>
template<class T>
struct  LinkNode
{
    LinkNode<T>* _next;
    T _data;

    LinkNode(const T& x)
        :_data(x)
        ,_next(NULL)
    {}
};

template<class T>
class List
{
    typedef LinkNode<T> Node;
public:
    List() //构造函数
        :_head(NULL), _tail(NULL)
    {
    }
    List(const List<T>& s) //拷贝构造函数
        :_head(NULL), _tail(NULL)
    {
        if (s._head == NULL)
        {
            return;
        }
        else
        {
            Node *cur(s._head);    //调用尾插的方法
            while (cur)
            {
                PushBack(cur->_data);
                cur = cur->_next;
            }
        }
    }
    void Swap(List<T>& s) //交换函数
    {
        swap(_head, s._head);
        swap(_tail, s._tail);
    }
    List<T>& operator=(List<T> s) //赋值运算符的重载
    {
        Swap(s);
        return *this;
    }
    ~List() //析构函数
    {
        clear();
    }
    void clear() //清理函数
    {
        Node *cur = _head;
        while (cur)
        {
            Node *next = cur->_next;
            delete cur;
            cur = next;
        }
        _head = _tail = NULL;
    }
    void Display() //显示函数
    {
        Node *cur = _head;
        while (cur)
        {
      
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值