项目要求
动态链表也是程序设计中的一种非常有用的数据结构。可以说,是否能够理解有关操作的原理,决定了你是否有资格称为“科班”出身。在后续的专业基础课中,相关的内容还会从不同的角度,反复地认识,反复地实践。不过,在现阶段多些体验,也是很有必要的了。
(1)阅读下面的程序,回顾一下动态链表,阅读程序过程中,请用笔画一画形成链表的过程中指针值的变化。
#include <iostream>
using namespace std;
struct Student
{ int num;
double score;
struct Student *next;
};
int main( )
{ Student *head=NULL,*p,*q;
//建立动态链表
for(int i=0;i<3;i++)
{
p = new Student;
cin>>p->num>>p->score;
p->next=NULL;
if (i==0) head=p;
else q->next=p;
q=p;
}
//输出动态链表
p=head;
while(p!=NULL)
{ cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}
}
(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 *next;
int num;
double score;
};
class MyList
{
public:
MyList()
{
head=NULL;
}
MyList(int n,double s)
{
head=new Student(n,s); //以Student(n,s)作为单结点的链表
}
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 MyList::display()
{
if (head==NULL)
{
cout<<"empty\n";
return 0;
}
int n=0;
Student *p=head;
while (p)
{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
n++;
}
return n;
}
void MyList::insert(int n,double s)//!!!
{
Student *p;
p=new Student(n,s);
p->next=head;
head=p;
}
void MyList::append(int n,double s)
{
Student *p;
p=new Student(n,s);
if (head==NULL)
head=p;
else
{
Student *pt=head;
Student *pts=pt->next;
while(pts)//!!!!之前写成while(pt)一直报错 简直要哭
{
pt=pts;
pts=pt->next;
}
pt->next=p;
}
}
void MyList::cat(MyList &il)
{
Student *p=il.head;
while (p)
{
append(p->num,p->score);
p=p->next;
}
}
int MyList::length()
{
int n=0;
Student *p=head;
while (p)
{
p=p->next;
n++;
}
return n;
}
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;
}
<span style="font-family:Microsoft YaHei;font-size:12px;">
</span>
运行结果
学习心得
感觉没太懂 为了找到Bug几乎是对着贺老的解答进行修正
反复看了云学堂第21讲视频…还是不太懂 keep on learning