第十四周任务二(建立专门链表类处理有关动态链表)

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:链表的操作练习

* 作    者: 冯珍珍                       
* 完成日期: 2012  年 05 月19 日
* 版 本 号:          
* 对任务及求解方法的描述部分
* 输入描述:这一段程序通过键盘输入数据,动态建立起了如图所示的链表,并通过一个循环输出。
            现在我们通过下面的任务,用面向对象的程序设计的思维看待最简单的动态链表,初步体验有关的操作。
		    任务要求是:在已有代码的基础上完善程序,完成动态链表的简单操作。
* 问题描述:
* 程序头部的注释结束

*/


#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() //输出链表,返回值为链表中的结点数
{
	Student *p;
	int n = 0;
	p = head;
	while(p != NULL)   //如果p所指向的结点存在
	{
		cout << p-> num << " " << p->score << endl;   
		p = p->next;   //p指向另一个结点
		++n;
	}
	return n;
}

void MyList::insert1(int n, double s)  //插入:将Student(n,s)结点插入链表,该结点作为第一个结点
{
	Student *p = head;      //将头结点赋给一个指针变量
	head = new Student(n, s);//将新结点赋给头指针
	head->next = p;       //头结点在Student(n,s)结点之后
}
void MyList::append(int n, double s)  //追加:将Student(n,s)结点插入链表,该结点作为最后一个结点
{
	Student *p = head;
	while (p->next != NULL)   //从第一个结点开始到最后一个结点结束
	{
		p = p->next;
	}
	p->next = new Student(n, s);
}

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

int MyList::length()  //返回链表中的结点数
{
	int n = 0;
	Student *p = head;
	while (p->next != NULL)
	{
		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();

	system("pause");
	return 0;
}


参考结果

input head1:
1 89
2 90
3 98
head1:
3 98
2 90
1 89
head2:
1001 98.4
1002 73.5
1003 92.8
1004 99.7
length of head2 after cat: 6
head2 after cat:
1001 98.4
1002 73.5
1003 92.8
1004 99.7
3 98
2 90
1 89
请按任意键继续. . .

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值