编写List类实验报告.docx

上传人:b****7 文档编号:26176366 上传时间:2023-06-17 格式:DOCX 页数:14 大小:49.79KB
下载 相关 举报
编写List类实验报告.docx_第1页
第1页 / 共14页
编写List类实验报告.docx_第2页
第2页 / 共14页
编写List类实验报告.docx_第3页
第3页 / 共14页
编写List类实验报告.docx_第4页
第4页 / 共14页
编写List类实验报告.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

编写List类实验报告.docx

《编写List类实验报告.docx》由会员分享,可在线阅读,更多相关《编写List类实验报告.docx(14页珍藏版)》请在冰豆网上搜索。

编写List类实验报告.docx

编写List类实验报告

 

2015/2016

(1)

 

实验题目List类编写及其成员函数拓展

学生姓名韩笑

学生学号201426811704

学生班级计算机+自动化1402班

任课教师******

提交日期2015-11-13

 

计算机科学与技术学院(软件学院)

实验报告

一、题目的内容

●编写一个算法判断链表中的数据项是否按从小到大排序,该链表的第一个节点由first指向。

●对于给定的整数n,编写一个算法把新的节点插入到链表中第n个节点之后的位置,该链表的第一个节点由first指向。

●编写一个算法来颠倒一个链表,该链表的第一个节点由first指向。

不要复制列表元素;而是重载链接和指针,使得first指向原来的最后一个节点,且节点之间的所有链接都反向。

二、做题思路及设计

分析题目:

作业中共有三题,都是基于链表并对其函数进行扩充,因此首先需要自行编写一个链表类,之后再在链表类中增加所需的函数来达到题目要求。

List类:

先构造一个node内部类,存放各节点的值、尾指针(见第四部分代码)。

在List类中,有内部成员变量头节点(first),list大小(Size);内部成员函数swap、erase(删除指定节点)、get_node(返回第n个节点);功能函数除构造析构外有display(输出函数)、erase(删除第n个节点)、insert(插入节点,见“第二问”)、push_front(在链表首位插入节点)、push_back(在链表尾部插入节点)、reverse(见“第三问”)、judge_sorted(见“第一问”)、empty(判空函数)、size(返回list大小)、copyconstruction(拷贝构造函数)以及对等号的重载。

Next

Node节点示意图:

Key

List示意图:

第一问:

函数名定为judge_sorted,从头节点first开始往后遍历,依次判断前一个数是否小于后一个数(前一个数用中间变量t来存储),如果不满足则直接返回false,然则为true。

第二问:

函数名定为insert,函数中首先判断list中Size是否为0,若为0,则直

接新建first,并指向的key赋值,并同时把Size变为1,关键代码如下:

first=newnode;

first->next=NULL;

first->key=val;

Size=1;

如果节点在队尾,则只需将队尾节点(x)的next指向新建节点(a),关键代码如下:

x->next=a;

对于中间节点,先将原节点(x)的next值赋予新建节点(a)的next,原节点next值改为新建节点,关键代码如下:

a->next=x->next;

x->next=a;

第三问:

函数名定为reverse,从前往后遍历链表,定义中间变量结点temp和r,依次改变每个结点的next值,最后将first指向最后一个节点即可,关键代码如下:

node*q=first,*r,*temp=NULL;

while

(1){//exchangeallnodes'headpointerandtailpointer

r=q->next;

q->next=temp;

if(r==NULL){//letthelastnodetobethefirstone

first=q;

break;

}

temp=q;

q=r;

}

三、程序调试、测试、运行记录

主要的测试经过如下:

四、源代码代码实现

工程名为:

List

具体函数声明/定义如下:

ØList.h

#pragmaonce

#ifndef_List_H_

#define_List_H_

#include

usingnamespacestd;

classList{

private:

classnode{

public:

node*next;//tailpointer

intkey;//number

node();//constructedfunction

node(intx);//Functionoverloading

~node();//destructor

};

node*first;//firstpointer

intSize;//sizeofthelist

inlinevoidswap(node*a,node*b);//exchangethenumberbetweennodeaandb

voiderase(node*x);//removethenodexfromthelist

inlinenode*get_node(intpos);//gettheposthnode'spointer

public:

List();//constructedfunction

List(constList&l);//copyconstructor

~List();//destructor

List&operator=(constList&l);//operator"="overloading

voiderase(intpos);//erasethenodeatthepositionofpos

voiddisplay(ostream&out);//displayfunction

voidinsert(intpos,intval);//insertxtotheposlocationinthelist

voidpush_front(intval);//insertitemtothebeginofthelist

voidpush_back(intval);//insertitemtotheendofthelist

voidreverse();//reversethelist

booljudge_sorted();//judgewhetherthelistisinorder

boolempty();//judgewhetherthelistisempty

intsize();//showsizeofthelist

friendostream&operator<<(ostream&out,List&l);//operator"<<"overloading

};

#endif

ØList.cpp

#include"List.h"

List:

:

node:

:

node(intx):

next(NULL),key(x){}//constructedfunction

List:

:

node:

:

node():

next(NULL){}//Functionoverloading

List:

:

node:

:

~node(){//destructor

next=NULL;

}

inlinevoidList:

:

swap(node*a,node*b){//exchangethenumberbetweennodeaandb

inttemp=a->key;

a->key=b->key;

b->key=temp;

}

voidList:

:

erase(node*x){//removethenodeattherightofnodexfromthelist

if(Size==1);

elseif(x->next==NULL);

else{

if(first==x){

first=x->next;

}

else{

node*temp=first;

while(temp!

=x){

temp=temp->next;

}

temp->next=x->next;

}

}

deletex;

x=NULL;

Size--;

}

inlineList:

:

node*List:

:

get_node(intpos){//gettheposthnode'spointer

if(pos>Size||pos<0){//exceptionalhandling

cerr<<"List:

indexrangeerror\n";

exit

(1);

}

node*x=first;

while(pos>0){

x=x->next;

pos--;

}

returnx;

}

List:

:

List(constList&l){//copyconstructor

first=newnode();

Size=0;

node*p=l.first;

while(p!

=NULL){

push_back(p->key);

p=p->next;

}

}

List:

:

List(){//constructedfunction

first=newnode();

first->next=NULL;

Size=0;

}

List:

:

~List(){//destructor

node*p;

while(first!

=NULL){

p=first;

first=first->next;

deletep;

}

}

voidList:

:

erase(intpos){//erasethenodeatthepositionofpos

erase(get_node(pos-1));

}

voidList:

:

display(ostream&out){//displayfunction

node*p=first;

intcnt=Size;

while(cnt--){

out<key;

if(cnt!

=0)

out<<'';

p=p->next;

}

}

voidList:

:

insert(intpos,intval){//insertxtotheposlocationinthelist

if(Size==0){

first=newnode;

first->next=NULL;

first->key=val;

Size=1;

return;

}

node*x=get_node(pos-1);

node*a=newnode(val);

if(x->next==NULL){

x->next=a;

a->next=NULL;

}

else{

a->next=x->next;

x->next=a;

}

Size++;

}

voidList:

:

push_front(intval){//insertitemtothebeginofthelist

insert(1,val);

swap(first,first->next);

}

voidList:

:

push_back(intval){//insertitemtotheendofthelist

insert(Size,val);

}

voidList:

:

reverse(){//reversethelist

node*q=first,*r,*temp=NULL;

while

(1){//exchangeallnodes'headpointerandtailpointer

r=q->next;

q->next=temp;

if(r==NULL){//letthelastnodetobethefirstone

first=q;

break;

}

temp=q;

q=r;

}

}

boolList:

:

judge_sorted(){//judgewhetherthelistisinorder

intt=first->key;

node*p=first->next;

if(p->key

while(p!

=NULL){

if(p->key>t)

returnfalse;

p=p->next;

}

}

else{

while(p!

=NULL){

if(tkey)

returnfalse;

p=p->next;

}

}

returntrue;

}

boolList:

:

empty(){//judgewhetherthelistisempty

returnSize==0;

}

intList:

:

size(){//showsizeofthelist

returnSize;

}

ostream&operator<<(ostream&out,List&l){//operator"<<"overloading

l.display(out);

returnout;

}

List&List:

:

operator=(constList&l){//operator"="overloading

node*p=l.first;

while(p!

=NULL){

push_back(p->key);

p=p->next;

}

Size=l.Size;

return*this;

}

Ømain.cpp

#include"List.h"

intmain(){

Listl;

cout<<"Testpush_back

(1):

";

l.push_back

(1);

cout<

//----------------------------------------------------------------------

cout<<"Testpush_front

(2):

";

l.push_front

(2);

cout<

//----------------------------------------------------------------------

cout<<"Testpush_back

(2):

";

l.push_back

(2);

cout<

//----------------------------------------------------------------------

cout<<"Testinsert(1,3):

";

l.insert(1,3);

cout<

//----------------------------------------------------------------------

cout<<"Testerase

(1):

";

l.erase

(1);

cout<

//----------------------------------------------------------------------

cout<<"Testreverse():

";

l.reverse();

cout<

//----------------------------------------------------------------------

cout<<"Testempty

(1):

";

if(l.empty())

cout<<"Thelistisempty."<

else

cout<<"Thelistisn'tempty."<

//----------------------------------------------------------------------

cout<<"Testsize():

";

cout<

//----------------------------------------------------------------------

cout<<"Testjudge_sorted():

";

if(l.judge_sorted())

cout<<"Thislisthasalreadysorted."<

else

cout<<"Thislisthasn'tsortedyet."<

//----------------------------------------------------------------------

cout<<"Testcopyconstructor:

";

Listtemp(l);

cout<

//----------------------------------------------------------------------

cout<<"Testoperator\"=\"overloading:

";

Listcnt;

cnt=l;

cout<

//----------------------------------------------------------------------

cout<<"Exceptional:

";

temp.erase(100);

}

五、总结

从本次实验我基本掌握list的内部实现,对于自己的编写代码的能力又有所提高。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1