C++类模板实现单链表

/********************************线性表抽象类的定义***************************/
template <class dataType>
class list{
    public:
      virtual void empty()=0;   //清空线性表
      virtual int getLength()=0;   //求表的长度
      virtual void insert(int i,const dataType& x)=0;   //在表中第i个位置插入值为x的元素
      virtual void remove(int i)=0; //删除表中第i个位置的元素
      virtual int search(const dataType& x)=0;  //查找并返回值为x的元素在表中的位置
      virtual dataType visit(int i)=0;  //访问表中第i个元素的值
      virtual void traverse()=0;    //遍历线性表
};

/*******************************单链表类的定义********************************/
template <class dataType>
class linkList:public list<dataType>{   //公有继承自list类
    public:
        linkList(); //构造函数,创建对象时生成空链表
        void empty();   //清空链表
        int getLength();    //求表的长度
        void insert(int i,const dataType& x);   //在表中第i个位置插入值为x的元素
        void remove(int i); //删除表中第i个位置的元素
        int search(const dataType& x);  //查找并返回值为x的元素在表中的位置
        dataType visit(int i);  //访问表中第i个元素的值
        void traverse();    //将表中的元素逐一输出
        ~linkList();    //析构函数
    private:
        //定义节点
        struct node{
            dataType data;
            node* next;
            node():next(NULL){};
            node(dataType d):data(d),next(NULL){};
        };

        node* head;   //链表头
        node* tail;   //链表尾
        int currentLength;  //链表长度
};

/*********************************单链表类的实现******************************/
template <class dataType>
linkList<dataType>::linkList(){
    head=new node;
    tail=new node;
    head->next=tail;
    currentLength=0;

    cout<<"\nCreate list success!\n";
}

template <class dataType>
void linkList<dataType>::empty(){
    node* nowPos=head->next;
    while(nowPos!=tail){
        node* tmp=nowPos;
        nowPos=nowPos->next;
        delete tmp;
    }
    head->next=tail;
    currentLength=0;

    cout<<"\nEmpty list success!\n";
}

template <class dataType>
int linkList<dataType>::getLength(){
    return currentLength;
}

template <class dataType>
void linkList<dataType>::insert(int i,const dataType& x){
    if(i<0||i>currentLength){
        cout<<"\nThe index is out of range!\n";
        return;
    }

    node* add=new node(x);
    if(i==0){
        add->next=head->next;
        head->next=add;
    }
    else{
        //找到第i-1个节点
        node* nowPos=head->next;
        while(--i){
            nowPos=nowPos->next;
        }

        add->next=nowPos->next;
        nowPos->next=add;
    }
    ++currentLength;

    cout<<"\nInsert data success!\n";
}

template <class dataType>
void linkList<dataType>::remove(int i){
    if(i<0||i>=currentLength){
        cout<<"\nThe index is out of range!\n";
        return;
    }

    if(i==0){
        node* tmp=head->next->next;
        delete head->next;
        head->next=tmp;
    }
    else{
        //找到第i-1个节点
        node* nowPos=head->next;
        while(--i){
            nowPos=nowPos->next;
        }

        node* tmp=nowPos->next->next;
        delete nowPos->next;
        nowPos->next=tmp;
    }
    --currentLength;

    cout<<"\nDelete data success!\n";
}

template <class dataType>
int linkList<dataType>::search(const dataType& x){
    node* nowPos=head;
    int i;
    for(i=0;i<currentLength;++i){
        nowPos=nowPos->next;
        if(nowPos->data==x) break;
    }
    if(i==currentLength) return -1;
    return i;
}

template <class dataType>
dataType linkList<dataType>::visit(int i){
    //下标越界抛出异常值0
    if(i<0||i>=currentLength){
        throw 0;
    }

    //找到第i个节点
    node* nowPos=head->next;
    while(i--){
        nowPos=nowPos->next;
    }

    return nowPos->data;
}

template <class dataType>
void linkList<dataType>::traverse(){
    if(currentLength==0){
        cout<<"\nThe list is empty!\n";
        return;
    }

    node* nowPos=head->next;
    cout<<nowPos->data;
    nowPos=nowPos->next;
    while(nowPos!=tail){
        cout<<" "<<nowPos->data;
        nowPos=nowPos->next;
    }
    cout<<endl;
}

template <class dataType>
linkList<dataType>::~linkList(){
    empty();
    delete head;
    delete tail;
}

面向对象程序设计课程作业 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、付费专栏及课程。

余额充值