List类成员函数拓展实验报告Word文件下载.docx

上传人:b****6 文档编号:21912294 上传时间:2023-02-01 格式:DOCX 页数:18 大小:63.57KB
下载 相关 举报
List类成员函数拓展实验报告Word文件下载.docx_第1页
第1页 / 共18页
List类成员函数拓展实验报告Word文件下载.docx_第2页
第2页 / 共18页
List类成员函数拓展实验报告Word文件下载.docx_第3页
第3页 / 共18页
List类成员函数拓展实验报告Word文件下载.docx_第4页
第4页 / 共18页
List类成员函数拓展实验报告Word文件下载.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

List类成员函数拓展实验报告Word文件下载.docx

《List类成员函数拓展实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《List类成员函数拓展实验报告Word文件下载.docx(18页珍藏版)》请在冰豆网上搜索。

List类成员函数拓展实验报告Word文件下载.docx

//createapointer"

q"

pointstofirst

for(inti=1;

i<

size;

i++){

node*a=newnode(x[i]);

q->

next=a;

q=q->

next;

}

q->

next=NULL;

Size=size;

}

List(int*x,intsize){

if(size==0);

else

first->

key=x[0];

i++){

q->

}

第二问:

函数名定为sort,先复制构造新的链表,利用快速排序实现对新链表的排序,快排最核心的思想就是划分,确定一个枢轴元素(pivot),每一趟划分的目的就是把待排序列分为两部分,前一部分比枢轴小(序列A),后一部分比枢轴大(序列B)。

经过一趟划分之后序列变为:

{A}pivot{B}。

以下是具体步骤:

1、确定每一次划分的枢轴元素为当前待排序列的头节点。

2、设置pHead和pEnd两个游标,pEnd指向序列A中的最后一个元素,初始化为枢轴本身(待排序列头节点)。

让pHead遍历一遍待排序列,当所指元素比枢轴小时,将pEnd往前游一格,交换pEnd和pHead所指元素的值,这样仍能保证pEnd指向的元素是序列A中的最后一个元素。

3、交换pEnd所指元素和枢轴元素的值。

4、对序列A和B重复步骤1~4。

第三问:

函数名定为merge_and_sort,先判断两链表是否已排好序,若没有,则先调用sort函数对其进行排序(此步骤对于本题目可删,但为保留程序可拓展性因此保留),之后创建两个结点依次对应两链表的first结点,从大到小依次通过push_back函数插入新建链表中,并返回该新建链表,具体代码见第四部分。

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

主要的测试经过如下:

四、源代码代码实现

工程名为:

List

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

Ø

List.h

#pragmaonce//compileonlyonce

#ifndef_List_H_//ifdidn'

tdefine"

_List_H_"

before

#define_List_H_//define"

#include<

iostream>

//includeheader"

iostream"

usingnamespacestd;

//usingnamespace"

std"

classList{//statementofclasslist

private:

//statementofprivatefunctions

classnode{//statementofclassnode

public:

//statementofpublicfunctions

node*next;

//tailpointer

intkey;

//number

node();

//constructedfunction

node(intx);

//Functionoverloading

};

//theendofclassnode

intSize;

//sizeofthelist

node*getpartion(node*pBegin,node*pEnd);

//getthepivotalnode

voidquicksort(node*pBeign,node*pEnd);

//quicksort

inlinevoidswap(node*a,node*b);

//exchangethenumberbetweennodeaandb

voiderase(node*x);

//removethenodexfromthelist

inlinenode*get_node(intpos);

//gettheposthnode'

spointer

public:

//statementofpublicfunctions

node*first;

//firstpointer

List();

//constructedfunction

List(constList&

l);

//copyconstructor

List(int*x,intsize);

//usearraytocreatealist

~List();

//destructor

List&

operator=(constList&

//operator"

="

overloading

voiderase(intpos);

//erasethenodeatthepositionofpos

voiddisplay(ostream&

out);

//displayfunction

voidinsert(intpos,intval);

//insertxtotheposlocationinthelist

voidadd(int*x,intsize);

//usearraytocreatealist

voidpush_front(intval);

//insertitemtothebeginofthelist

voidpush_back(intval);

//insertitemtotheendofthelist

voidreverse();

//reversethelist

booljudge_sorted(charc='

>

'

);

//judgewhetherthelistisinorder

boolempty();

//judgewhetherthelistisempty

intsize()const;

//showsizeofthelist

friendostream&

operator<

<

(ostream&

out,List&

//operator"

"

ListList:

:

sort();

//sort

Listmerge_and_sort(Listl);

//mergetwolistandsort

};

//theendofstatementofclassnode

#endif//theendof"

ifndef"

List.cpp

#include"

List.h"

//includeheader"

List:

node:

node(intx):

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

node():

next(NULL){}//Functionoverloading

node*List:

getpartion(node*pBegin,node*pEnd){//getthepivotalnode

intkey=pBegin->

key;

//assignthekeyof"

pBegin"

tonewkey

node*p=pBegin;

//assignthenode"

tonewnode"

p"

node*q=p->

/*assignthenextnodeofnode"

tonewnode"

*/

while(q!

=pEnd){//judgewhether"

equals"

pEnd"

if(q->

key>

key){/*ifdo,judgewhetherifthekeyofnode"

biggerthanthekeyofthenode"

before*/

p=p->

/*assigntheaddressofthenextnodeofnode"

to

theaddressofnode"

swap(p,q);

//exchangethenumberbetweennodepandq

}//theendofifstatement

/*assigntheaddressofthenextnodeofnode"

}//theendofwhilestatement

swap(p,pBegin);

//exchangethenumberbetweennodepandpBegin

returnp;

//returnnode*p

}//theendoffunction"

getpartion"

inlinevoidList:

swap(node*a,node*b){/*exchangethenumber(notthenode)

betweennodeaandb*/

inttemp=a->

/*definenumber,"

temp"

asamiddlesection,

assignthekeyof"

a"

totemp*/

a->

key=b->

//assignthekeyof"

b"

tothekeyof"

b->

key=temp;

//assigntemptothekeyof"

swap"

voidList:

erase(node*x){//removethenodeattherightofnode"

x"

fromthelist

if(Size==1);

/*judgetheSizewhetherequalsone

ifdo,gotoline56;

ifnot,gonext*/

elseif(x->

next==NULL);

/*judgewhetherthenodeisthelastnodeofthelist

ifnotgonext*/

else{//elsestatement

if(first==x)/*judgetheaddressofnode"

whetherequals

first=x->

/*ifdo,assigntheaddressofwhichthenextnodeof

node"

to"

else{//elsestatement

node*temp=first;

//definenode,"

asamiddlesection

while(temp!

=x)/*judgetheaddressof"

whethernotequals

theadressof"

temp=temp->

/*assigntheaddressofwhichthenextnodeof

node"

totheaddressofthenode"

temp->

next=x->

/*assigntheaddressofwhichthenextnodeof

node“x”totheaddressofwhichthenextnodeofnode"

}//theendofelsestatement(line52)

}//theendofelsestatement(line47)

deletex;

//deletethenode"

x=NULL;

//assignNULLto"

Size--;

//sizeminusone

}//theendof"

erase(node*)"

inlineList:

get_node(intpos){//gettheposthnode'

if(pos>

Size||pos<

0){//exceptionalhandling

cerr<

"

indexrangeerror\n"

;

//printexceptionalstatement

exit

(1);

//exittheprogram

}//endif

node*x=first;

/*declare*x,assigntheadressofnode"

totheaddressofnode"

while(pos>

0){//judgewhetherposbiggerthanzero

x=x->

/*ifdo,thepointerof"

pointsto

thepointerofthenextnodeof"

pos--;

//posminusone

returnx;

//return*x

}//theendoffunctionget_node

List(constList&

l){//copyconstructor

first=newnode(l.first->

key);

/*createanewnodetothepointer"

thevalue

ofthenodeequalstothevalueofthefistnodeof

list"

l"

node*p=l.first->

next,*q=first;

/*definepointer"

pointstothenext

nodeofwhichisthefirstnodeofthelist"

pointer"

pointstothefirstpointerofthislist*/

while(p!

=NULL){//judgewhether"

isnone

node*a=newnode(p->

//createanewnodeandassignthekeyofnode"

toit

//thepointer"

next"

innode"

pointstonode"

movetothenext

p=p->

//letthepointer"

inthelastnodenull

Size=l.size();

//letSizeequalsthesizeoflist"

}//theendofcopyconstructor

List(int*x,intsize){//usearraytocreatealist

if(size==0)//judgewhethersizeequalszero

//ifdo,createanullnodetonode"

else//elsestatement

//createanodewhichvalueisx[0]tonode"

//createapointer"

i++){/*intifrom1tosize-1,whichmeansthesubscript

ofarray"

//createanodewhichvalueisx[i]tonode"

}//theendofloop

inthelastnodenull

//assignsizetoSize

}//theendofoverloadingconstructor

List(){//constructedfunction

first=newnode();

//createanullnodetonode"

first->

//assignthepointer"

tonull

Size=0;

//assignzerotoSize

}//theendofconstructor

~List(){//destructor

node*p;

//defineanodepointer"

while(first!

=NULL){//judgewhetherfirstisnotnull

p=first;

//ifdo,letpequalstofirst

first=first->

//firstmovetothenext

deletep;

//deletenodep

}

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

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

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

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