您的位置:首页 > 移动开发 > IOS开发

《第十四周任务二》建立专门的链表类处理有关动态链表的操作

2012-05-23 13:09 417 查看
/*

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生 

* All rights reserved.

* 文件名称:C++第十四周【任务2】                              

* 作    者:   张斌                              

* 完成日期:   2012  年  5  月  23日

* 对任务及求解方法的描述部分

* 输入描述:输入序号和成绩

* 问题描述:建立专门的链表类处理有关动态链表的操作

* 程序输出:利用链表输出

*/

【任务2】建立专门的链表类处理有关动态链表的操作

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

先阅读下面的程序,回顾一下动态链表,阅读程序过程中,请用笔画一画形成链表的过程中指针值的变化。

 
#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;
}
system("pause");
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 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();

system("pause");
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;
};
void MyList::insert(int n,double s)  //插入:将Student(n,s)结点插入链表,该结点作为第一个结点
{
Student *stu=head;
head=new Student(n,s);
head->next=stu;
}
void MyList::append(int n,double s)  //追加:将Student(n,s)结点插入链表,该结点作为最后一个结点
{
Student *stu=head;
while(stu->next != NULL)
{
stu =stu->next;
}
stu->next=new Student(n,s);

}
void MyList::cat(MyList &il) //将链表il连接到当前对象的后面
{
Student *stu=head;
while(stu->next  != NULL)
{
stu = stu->next;
}
stu->next=il.head;

}
int MyList::length()  //返回链表中的结点数
{
int length=0;
Student *stu=head;
while(stu->next != NULL)
{
stu = stu->next;
length++;
}
return length;
}
int MyList::display()  //输出链表,返回值为链表中的结点数
{
int length=0;
Student *stu=head;
while(stu->next != NULL)
{
cout<<"num="<<stu->num<<"score="<<stu->score<<'\t';
stu = stu->next;
length++;
}
return length;
}

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();

system("pause");
return 0;
}


输出结果:

input head1:

2001 99 2002 98 2003 97

head1:

num=2003score=97        num=2002score=98        head2:

num=1001score=98.4      num=1002score=73.5      num=1003score=92.8      length o

f head2 after cat: 6

head2 after cat:

num=1001score=98.4      num=1002score=73.5      num=1003score=92.8      num=1004

score=99.7      num=2003score=97        num=2002score=98        请按任意键继续.

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐