建立专门的链表类处理有关动态链表的操作 扩展二

#include<iostream>
using namespace std;
template <class TT,class PP>
class Student  //结点类
{
public:
    Student(TT n,PP s){num=n;score=s;next=NULL;}
    ~Student(){};
    Student *next;   //指向下一个结点
    TT num;
    PP score;
};
class MyList  //链表类
{
public:
    MyList(){head=NULL;}
    MyList(int n,double s); //以Student(n,s)作为单结点的链表
    ~MyList();
    int display();  //输出链表,返回值为链表中的结点数
    void insert(int n,double s);  //插入一系列结点:将Student(n,s)结点插入链表,该结点作为第一个结点
    void append(int n,double s);  //追加:将Student(n,s)结点插入链表,该结点作为最后一个结点
    void cat(MyList &il); //将链表il连接到当前对象的后面
    int length();  //返回链表中的结点数
private:
    Student<int,double> *head;   //链表的头结点
};
//以下为类成员函数的定义
MyList::MyList(int n,double s)
{
    head=new  Student<int,double>(n,s);
}
MyList::~MyList()
{
    while(head!=NULL)
    {
        Student<int,double> *p=head;
        head=head->next;
        delete p;
    }
    head=NULL;
}
int MyList::display()
{
    int i=0;
    Student<int,double> *head1=head;
    while(head1!=NULL)
    {
        cout<<"num:"<<head1->num<<"\t"<<"score:"<<head1->score<<endl;
        head1=head1->next;
        ++i;
    }
    head1=NULL;
    return i;
}
void MyList::insert (int n,double s)//熟练运用点的两种顺序插入
{
    Student<int,double> *pt=new Student<int,double>(n,s);
    pt->next=head;
    head=pt;
}
void MyList::append(int n,double s)//关键点在于如何遍历到最后一点
{
    Student<int,double> *head2=head;
    if(head==NULL)head=new Student<int,double>(n,s);
    else
    {
    while(head2->next!=NULL)head2=head2->next;
    head2->next=new Student<int,double>(n,s);
    }
//  head2->next->num=n;//之前的错误写法,没有分配空间就赋值
//  head2->next->score=s;
//  head2->next->next=NULL;
}
//void MyList::cat(MyList &il)//错误在于将一个链表直接附加到另外的链表后,导致析构时附加的链表被析构两次
//{                                                  //在函数尾部将附加的链表头置为NULL,就不会出错
//                                                   //但是这样貌似也不是很好
//  Student *pt=head;
//  while(pt->next!=NULL)pt=pt->next;
//  pt->next=il.head;
//  il.head=NULL;
//}
void MyList::cat(MyList& il)
{
    Student<int,double> *pt=il.head;
    while(pt)
    {
        append(pt->num,pt->score);
        pt=pt->next;
    }
}
int MyList::length()
{
    int i=0;
    Student<int,double> *head1=head;
    while(head1!=NULL)
    {
        head1=head1->next;
        ++i;
    }
    head1=NULL;
    return i;
}
//测试函数
int main()
{
    int n;
    double s;
    MyList head1;
    cout<<"input head1: "<<endl;  //输入head1链表
    for(int i=0;i<2;i++)
    {
        cin>>n>>s;
        head1.insert(n,s);  //通过“插入”的方式
    }
    cout<<"head1: "<<endl; //输出head1
    head1.display();


    MyList head2(1001,98.4);  //建立head2链表
    head2.append(1002,73.5);  //通过“追加”的方式增加结点
    head2.append(1003,92.8);
    head2.append(1004,99.7);
    cout<<"head2: "<<endl;   //输出head2
    head2.display();


    head2.cat(head1);   //把head1追加到head2后面
    cout<<"length of head2 after cat: "<<head2.length()<<endl;
    cout<<"head2 after cat: "<<endl;   //显示追加后的结果
    head2.display();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值