标题:在类模板中定义拷贝构造函数和使用赋值重载操作符
只看楼主
onlyonezwh
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-8
 问题点数:0 回复次数:0 
在类模板中定义拷贝构造函数和使用赋值重载操作符
//7)在上一题基础上,为单链表类模板增加一个拷贝构造函数和赋值运算操作符
#include<iostream>

using namespace std;

template <class T>
class LinearList
{
public:
    LinearList()   
    {
        head=NULL;
        T x;
        cout<<"input the num:";
        cin>>x;
        Node*tail;
        while(x!=0)
        {
            Node*p=new Node;
            p->num=x;
            p->next=NULL;
            if(head==NULL)
                head=p;
            else
                tail->next=p;
            tail=p;
            cin>>x;
        }
    }
    ~LinearList()
    {
        delete head;
    }
    LinearList(const LinearList&s);
    LinearList& operator=(const LinearList&s)
    {
        while(head!=NULL)
        {
            Node*p=head;
            head=head->next;
            delete p;
        }

        Node*p1=head,*p2=s.head;
        while(p2!=NULL)
        {
            p1->num=p2->num;
            p1=p1->next;
            p2=p2->next;
        }
        return *this;

    }
    void dispaly();
    void sort();
    void insert(const int x);
    void del(const int x);
private:

    struct Node
    {
        T num;
        Node*next;
    };
    Node*head;
};

template <class T>
LinearList<T>::LinearList(const LinearList<T>&s)
{
    Node*p0=s.head;
    head=NULL;
    Node*tail;
    while(p0->num!=0)
    {
        Node*p=new Node;
        p->num=p0->num;
        p->next=NULL;
        if(head==NULL)
            head=p;
        else
            tail->next=p;
        tail=p;
        p0=p0->next;
    }
}


template <class T>
void LinearList<T>::dispaly()
{
    Node*p=head;
    while(p!=NULL)
    {
        cout<<p->num<<" ";
        p=p->next;
    }
    cout<<endl;
}

template <class T>
void LinearList<T>::sort()
{
    for(Node*p1=head;p1->next!=NULL;p1=p1->next)
        for(Node*p2=p1->next;p2!=NULL;p2=p2->next)
            if(p1->num>p2->num)
            {
                T temp=p1->num;
                p1->num=p2->num;
                p2->num=temp;
            }
}

template <class T>
void LinearList<T>::insert(int x)
{

    Node*p1=head,*p2;
    Node*p0=new Node;
    p0->num=(T)x;
    if (head==NULL)
    {
        p0->next=head;
        head=p0;
    }
    if (p0->num<=head->num)
    {
        p0->next=head;
        head=p0;
    }
    for(;p1!=NULL;p1=p1->next)
    {
        if(p1->num>=p0->num)
            break;
        p2=p1;
    }
    if (p1==NULL)
    {
        p2->next=p0;
        p0->next=NULL;
    }
    else
    {
        p2->next=p0;
        p0->next=p1;
    }
}

template <class T>
void LinearList<T>::del(int x)
{
    Node*p1=head,*p2;
    if(head==NULL)
    {
        cout<<"the link is empty!"<<endl;
        exit(-1);
    }
    if (p1->num==(T)x)
    {
        head=head->next;
        delete p1;
    }
    for(;p1!=NULL;p1=p1->next)
    {
        if(p1->num>=(T)x)
            break;
        p2=p1;
    }
    if (p1==NULL)
    {
        cout<<"no the jiedian!"<<endl;
    }
    else
    {
        p2->next=p1->next;
        delete p1;
    }
}

int main()
{
    LinearList<int> a;
    a.dispaly();
    a.sort();
    a.dispaly();
    a.insert(4);
    a.dispaly();
    a.del(3);
    a.dispaly();


    LinearList<double> d;
   
    d.sort();
    d.dispaly();
    d.insert(4.0);
    d.dispaly();
    d.del(3.0);
    d.dispaly();
   
    LinearList<double> e(d);
    e.dispaly();
   

    return 0;
}


第一次冒泡,哪位神级的存在帮忙看一下

LinearList<double> e(d);
    e.dispaly();//这里跑不出来
搜索更多相关主题的帖子: include public 
2014-05-10 17:37



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431325-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.111591 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved