C++练习 | 单向链表类模板(包含类模板中静态变量初始化格式)

#include <iostream>
#include <string>
using namespace std;

template <class T>
class List
{
private:
    T data;
    List<T> * next;
    static List<T> * tail;      //指向最后一个结点
    static List<T> * h;      //指向头结点
public:
    List():next(NULL)                  //构造头结点
    {
        h = tail = this;
    }
    List(T newnode):data(newnode),next(NULL)    //构造新结点
    {}
    void append(T node)
    {
        tail->next=new List<T>(node);
        tail=tail->next;
    }//往后面添加结点
    bool insert(T node, T posnode)
    {
        List<T> *cur,*flag;
        cur=h;
        while(cur->data!=posnode&&cur!=tail->next)//遇到第一个数值相等的就跳出循环,保证是第一个
        {
            cur=cur->next;
        }
        if(cur!=NULL)//防止是因为循环到尾部而跳出循环
        {
            if(cur->next!=NULL)//如果是尾部就按append函数的方式插入
            {
                List<T> *p=new List<T>(node);
                flag=cur->next;
                cur->next=p;
                p->next=flag;
            }
            else//如果是链表中间就改变下前面一个和加入节点的next
            {
                cur->next=new List<T>(node);
                tail=cur->next;
            }
            return true;
        }
        return false;
    }//在结点posnode第一次出现的后面插入新结点node, 插入成功返回true,否则false
    void deleteNode(T node)
    {
        List<T> *now;
        now=h->next;
        if(now->data==node)
        {
            if(tail==h->next)//如果b链表尾在头节点后两个节点,tail不太好写,所以拆开来写
            {
                h->next=NULL;
                tail=h;
            }
            else if(tail==h->next->next)
            {
                h->next=h->next->next;
                tail=h->next;
            }
        }
        while(now->next!=NULL)
        {
            if(now->next->data==node)
                now->next=now->next->next;
            else
                now=now->next;
        }
    }//删除结点,注意可能有多个值相同的结点需要删除
    void delList()
    {
        h->next=NULL;
        tail=h;
    }//删除整个链表
    void dispList()
    {
        List<T> *now;
        if(h->next!=NULL)//链表不为空
        {
            now=h->next;
            while(now!=tail->next)
            {
                cout<<now->data<<" ";
                now=now->next;
            }
        }
        cout<<endl;
    }//显示链表
    friend void print(List<T> *now);
};
template<class T>
List<T>*List<T>::tail=NULL;
template<class T>
List<T>*List<T>::h=NULL;

 

转载于:https://www.cnblogs.com/tsj816523/p/11068695.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值