C++第15周项目2 -链表类

简介: 课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/9018813【项目2】建立专门的链表类处理有关动态链表的操作  动态链表也是程序设计中的一种非常有用的数据结构。可以说,是否能够理解有关操作的原理,决定了

课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/9018813


【项目2】建立专门的链表类处理有关动态链表的操作
  动态链表也是程序设计中的一种非常有用的数据结构。可以说,是否能够理解有关操作的原理,决定了你是否有资格称为“科班”出身。在后续的专业基础课中,相关的内容还会从不同的角度,反复地认识,反复地实践。不过,在现阶段多些体验,也是很有必要的了。

  现在,请在已有代码的基础上完善程序,完成动态链表的简单操作,程序运行的截图供参考。

class Student  //结点类
{
public:
	Student(int n,double s){num=n;score=s;next=NULL;}
	~Student();
	Student *next;   //指向下一个结点
	int num;
	double 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 *head;   //链表的头结点
};
//以下为类成员函数的定义
……
//测试函数
int main()
{
	int n;
	double s;
	MyList head1;
	cout<<"input head1: "<<endl;  //输入head1链表
	for(int i=0;i<3;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;
}


  运行结果示例:

  


参考解答:

#include<iostream>
using namespace std;

class Student  //结点类
{
public:
    Student(int n,double s)
    {
        num=n;
        score=s;
        next=NULL;
    }
    ~Student()
    {
        if(!next)
            delete next;
        next=NULL;
    };
    Student *next;   //指向下一个结点
    int num;
    double 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 *head;   //链表的头结点
};

MyList::MyList(int n,double s)
{
    head=new Student(n,s);
}

MyList::~MyList()
{
    Student *p=head, *q;
    while (p != NULL)
    {
        q = p;
        p = p->next;
        delete q;
    }
    head = NULL;
}

int MyList::display()
{
    if(head==NULL)
    {
        cout<<"empty\n";
        return 0;
    }
    int cnt=0;
    Student *pt=head;
    while(pt)
    {
        ++cnt;
        cout<<pt->num<<", "<<pt->score<<endl;
        pt=pt->next;
    }
    return cnt;
}

void MyList::insert(int n, double s)
{
    Student * pt=new Student(n,s);
    pt->next =head;
    head=pt;
}

void MyList::append(int n,double s)
{
    Student * pt=new Student(n,s);
    if(head==NULL)
        head=pt;
    else
    {
        Student *pts=head;
        Student *pte=pts->next;
        while(pte)
        {
            pts=pte;
            pte=pts->next;
        }
        pts->next=pt;
    }
}

void MyList::cat(MyList& il)
{
    Student *pt=il.head;
    while(pt)
    {
        append(pt->num,pt->score);
        pt=pt->next;
    }
}

int MyList::length()
{
    int cnt=0;
    Student *pt=head;
    while(pt)
    {
        ++cnt;
        pt=pt->next ;
    }
    return cnt;
}

int main()
{
    int n;
    double s;
    MyList head1;
    cout<<"input head1: "<<endl;  //输入head1链表
    for(int i=0; i<3; 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;
}


目录
相关文章
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
17 0
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
1天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
1天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
5天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
7天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
27天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
34 0
|
27天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
32 0
|
7天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”