用模板实现单链表

#include<iostream>
using namespace std;

template <class T>
struct Node
{
    T _data;
    Node* next;
    Node(const T& d)
        :_data(d)
        ,next(NULL)
    {}
};

template <class T>
class LinkList
{
public:
    LinkList()
        :_head(NULL)
        ,_tail(NULL)
    {}

    ~LinkList()
    {
        if(_head == NULL)
    {}
    else if((_head == _tail)&&(_head != NULL))
    {
        delete _head;
        _head = _tail = NULL;
    }
    else
    {
        Node<T>* cur = _head;
        Node<T>* del = _head;
        while(cur)
        {
            del = cur;
            cur = cur->next ;
            delete del;
        }
    }
    }

public:
    void PushBack(const T& d);
    void PopBack();
    void PushFront(const T& d);
    void PopFront();
    void Display();
    int ListLen();
    void Insert(const int& pos,const T& d);
    void Del_Insert(const int& pos);


private:
    Node<T>* _head;
    Node<T>* _tail;
};

template <class T>
void LinkList<T>::PushBack (const T& d)
{
    if(_head == NULL)
    {
        Node<T>* cur = new Node<T>(d);
        _head = cur;
        _tail = cur;
    }
    else
    {
        Node<T>* cur = new Node<T>(d);
        Node<T>* tmp = _head;

        while(tmp->next )
        {
            tmp = tmp->next ;
        }
        tmp->next = cur;
        _tail = cur;
    }

}


template <class T>
void LinkList<T>::PopBack ()
{
    if(_head == NULL)
    {
        return ;
    }
    else if((_head == _tail)&&(_head != NULL))
    {
        delete _head;
        _head = _tail = NULL;
    }
    else
    {
        /*delete _tail;   
        _tail = NULL;    //只是转移了_tail的指向,使其指向了NULL,并没有改变链表最后一个节点为NULL
        Node<T>* cur = _head;
        while(cur->next)
        {
            cur = cur->next;
        }
        _tail = cur;*/
        Node<T>* cur = _head;
        while(cur->next)
        {
            _tail = cur;
            cur = cur->next;
        }
        delete cur;
        _tail->next = NULL;

    }
}

template <class T>
void LinkList<T>::PushFront(const T& d)
{
    if(_head == NULL)
    {
        Node<T>* cur = new Node<T>(d);
        _head = cur;
        _tail = cur;
    }
    else 
    {
        Node<T>* cur = new Node<T>(d);
        cur->next = _head;
        _head = cur;
    }
}

template <class T>
void LinkList<T>::PopFront ()
{
        if(_head == NULL)
    {
        return ;
    }
    else if((_head == _tail)&&(_head != NULL))
    {
        delete _head;
        _head = _tail = NULL;
    }
    else
    {
        Node<T>* cur = _head;
        _head = _head->next ;
        delete cur;
    }
}

template <class T>
void LinkList<T>::Display()
{
    Node<T>* cur = _head;
    while(cur )
    {
        cout<<cur->_data <<" ";
        cur = cur->next ;
    }
    cout<<endl;
}
template <class T>
int LinkList<T>::ListLen()
{
    int count = 0;
    Node<T>* cur = _head;
    while(cur)
    {
        count++;
        cur= cur->next ;
    }
    return count;
}


template <class T>
void LinkList<T>:: Insert(const int& pos,const T& d)
{
    int num  = pos;
    if(num > ListLen()+1 )
    {
        cout<<"NULL 链表"<<endl;
        return ;
    }
    else
    {
        Node<T>* cur = new Node<T>(d);
        if(num == 1)
        {
            cur->next = _head;
            _head = cur;
        }
        else
        {
            Node<T>* tmp1 = _head;
            Node<T>* tmp2 = _head;
            while(--num)
            {
                tmp2 = tmp1;
                tmp1 = tmp1->next ;

            }
            cur->next = tmp1;
            tmp2->next = cur;
            //_head->next = cur;  应该是tmp的上一个节点指向cur
        }
    }

}
template <class T>
void LinkList<T>::Del_Insert(const int& pos)
{
    int num  = pos;
    if(_head == NULL)
    {
        cout<<"NULL 链表"<<endl;
        return ;
    }
    else if((_head == _tail)&& (_head != NULL))
    {
        delete _head;
        _head = _tail = NULL;
    }
    else
    {
        Node<T>*  cur = _head ;
        if(num == 1)
        {
            _head= _head ->next ;
            delete cur;
        }
        else 
        {
            Node<T>* tmp1 = _head;
            Node<T>* tmp2 = _head;
            while(--num)
            {
                tmp2 = tmp1;
                tmp1 = tmp1->next ;
            }
            tmp2->next = tmp1->next ;
            delete tmp1;

        }

    }
}

void test()
{
    LinkList<int> l1;
    l1.PushBack (1);
    l1.PushBack (2);
    l1.PushBack (3);
    l1.PushBack (4);
    l1.PushBack (5);
    l1.PopBack ();
    l1.PushFront (0);
    l1.PopFront ();
    l1.Display ();
    l1.PushFront (5);
    l1.Display ();
    cout<<"Listlen:"<<l1.ListLen ()<<endl;

    l1.Insert (1,6);
    l1.Display ();
    l1.Insert (2,7);
    l1.Display ();
    l1.Insert (8,8);
    l1.Display ();
    cout<<"Listlen:"<<l1.ListLen ()<<endl;

    l1.Del_Insert (1);
    l1.Display ();
    cout<<"Listlen:"<<l1.ListLen ()<<endl;

}

int main()
{

    test();
    return 0;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值