List类成员函数拓展实验报告.docx
- 文档编号:8776108
- 上传时间:2023-02-01
- 格式:DOCX
- 页数:18
- 大小:63.57KB
List类成员函数拓展实验报告.docx
《List类成员函数拓展实验报告.docx》由会员分享,可在线阅读,更多相关《List类成员函数拓展实验报告.docx(18页珍藏版)》请在冰豆网上搜索。
List类成员函数拓展实验报告
2015/2016
(1)
实验题目List类成员函数拓展
学生姓名韩笑
学生学号201426811704
学生班级计算机+自动化1402班
任课教师******
提交日期2015-11-15
计算机科学与技术学院(软件学院)
实验报告
一、题目的内容
●将A1和B1中的数据导入链表中,形成链表A2和B2,并打印各自链表元素;
●将链表A2和B2中的元素各自排序(从大到小),形成链表A3和B3,并打印各自链表元素。
●合并链表A3,B3,合并后的链表C的元素从大到小排列,并打印链表C。
对于给定的整数n,编写一个算法把新的节点插入到链表中第n个节点之后的位置,该链表的第一个节点由first指向。
二、做题思路及设计
分析题目:
作业中共有三题,都是基于链表并对其函数进行扩充,链表类在前一次实验中已经编写过了,因此本次实验只需在已有链表类的基础上增加成员函数即可。
List类:
在本实验中不再重复描述,大致结构框架如下
Next
Node节点示意图:
Key
Pre
List示意图:
第一问:
函数名定为add,并在构造函数中增加对数组的重载,从头节点first开始往后遍历,由于如果利用push_back函数在每插入一元素时都会对链表进行一次遍历,在时间效率上不高,因此不直接采用push_back函数而直接在循环过程中保留上次插入结点的位置,与上次插入的结点后直接插入结点,代码如下(详细含义见第四部分代码注释):
voidadd(int*x,intsize){
if(size==0)
first=newnode();
else
first=newnode(x[0]);whichvalueisx[0]tonode"first"
node*q=first;//createapointer"q"pointstofirst
for(inti=1;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]; node*q=first; for(inti=1;i node*a=newnode(x[i]);q->next=a; q=q->next; } q->next=NULL; Size=size; } 第二问: 函数名定为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"_List_H_" #include 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&l);//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&l);//operator"<<"overloading ListList: : sort();//sort Listmerge_and_sort(Listl);//mergetwolistandsort };//theendofstatementofclassnode #endif//theendof"ifndef" ØList.cpp #include"List.h"//includeheader"List.h" List: : node: : node(intx): next(NULL),key(x){}//constructedfunction List: : node: : node(): next(NULL){}//Functionoverloading List: : node*List: : getpartion(node*pBegin,node*pEnd){//getthepivotalnode intkey=pBegin->key;//assignthekeyof"pBegin"tonewkey node*p=pBegin;//assignthenode"pBegin"tonewnode"p" node*q=p->next;/*assignthenextnodeofnode"pBegin" tonewnode"q"*/ while(q! =pEnd){//judgewhether"q"equals"pEnd" if(q->key>key){/*ifdo,judgewhetherifthekeyofnode"q" biggerthanthekeyofthenode"q"before*/ p=p->next;/*assigntheaddressofthenextnodeofnode"p"to theaddressofnode"p"*/ swap(p,q);//exchangethenumberbetweennodepandq }//theendofifstatement q=q->next;/*assigntheaddressofthenextnodeofnode"q"to theaddressofnode"q"*/ }//theendofwhilestatement swap(p,pBegin);//exchangethenumberbetweennodepandpBegin returnp;//returnnode*p }//theendoffunction"getpartion" inlinevoidList: : swap(node*a,node*b){/*exchangethenumber(notthenode) betweennodeaandb*/ inttemp=a->key;/*definenumber,"temp",asamiddlesection, assignthekeyof"a"totemp*/ a->key=b->key;//assignthekeyof"b"tothekeyof"a" b->key=temp;//assigntemptothekeyof"b" }//theendoffunction"swap" voidList: : erase(node*x){//removethenodeattherightofnode"x"fromthelist if(Size==1);/*judgetheSizewhetherequalsone ifdo,gotoline56;ifnot,gonext*/ elseif(x->next==NULL);/*judgewhetherthenodeisthelastnodeofthelist ifdo,gotoline56;ifnotgonext*/ else{//elsestatement if(first==x)/*judgetheaddressofnode"first"whetherequals theaddressofnode"x"*/ first=x->next;/*ifdo,assigntheaddressofwhichthenextnodeof node"x"to"first"*/ else{//elsestatement node*temp=first;//definenode,"temp",asamiddlesection while(temp! =x)/*judgetheaddressof"temp"whethernotequals theadressof"x"*/ temp=temp->next;/*assigntheaddressofwhichthenextnodeof node"temp"totheaddressofthenode"temp"*/ temp->next=x->next;/*assigntheaddressofwhichthenextnodeof node“x”totheaddressofwhichthenextnodeofnode"temp"*/ }//theendofelsestatement(line52) }//theendofelsestatement(line47) deletex;//deletethenode"x" x=NULL;//assignNULLto"x" Size--;//sizeminusone }//theendof"erase(node*)" inlineList: : node*List: : get_node(intpos){//gettheposthnode'spointer if(pos>Size||pos<0){//exceptionalhandling cerr<<"List: indexrangeerror\n";//printexceptionalstatement exit (1);//exittheprogram }//endif node*x=first;/*declare*x,assigntheadressofnode"first" totheaddressofnode"x"*/ while(pos>0){//judgewhetherposbiggerthanzero x=x->next;/*ifdo,thepointerof"x"pointsto thepointerofthenextnodeof"x"*/ pos--;//posminusone }//theendofwhilestatement returnx;//return*x }//theendoffunctionget_node List: : List(constList&l){//copyconstructor first=newnode(l.first->key);/*createanewnodetothepointer"first",thevalue ofthenodeequalstothevalueofthefistnodeof list"l"*/ node*p=l.first->next,*q=first;/*definepointer"p"pointstothenext nodeofwhichisthefirstnodeofthelist"l",pointer"q"pointstothefirstpointerofthislist*/ while(p! =NULL){//judgewhether"p"isnone node*a=newnode(p->key);//createanewnodeandassignthekeyofnode"p"toit q->next=a;//thepointer"next"innode"q"pointstonode"a" q=q->next;//thepointer"q"movetothenext p=p->next;//thepointer"p"movetothenext }//theendofwhilestatement q->next=NULL;//letthepointer"next"inthelastnodenull Size=l.size();//letSizeequalsthesizeoflist"l" }//theendofcopyconstructor List: : List(int*x,intsize){//usearraytocreatealist if(size==0)//judgewhethersizeequalszero first=newnode();//ifdo,createanullnodetonode"first" else//elsestatement first=newnode(x[0]);//createanodewhichvalueisx[0]tonode"first" node*q=first;//createapointer"q"pointstofirst for(inti=1;i ofarray"x"*/ node*a=newnode(x[i]);//createanodewhichvalueisx[i]tonode"a" q->next=a;//thepointer"next"innode"q"pointstonode"a" q=q->next;//thepointer"q"movetothenext }//theendofloop q->next=NULL;//letthepointer"next"inthelastnodenull Size=size;//assignsizetoSize }//theendofoverloadingconstructor List: : List(){//constructedfunction first=newnode();//createanullnodetonode"first" first->next=NULL;//assignthepointer"next"tonull Size=0;//assignzerotoSize }//theendofconstructor List: : ~List(){//destructor node*p;//defineanodepointer"p" while(first! =NULL){//judgewhetherfirstisnotnull p=first;//ifdo,letpequalstofirst first=first->next;//firstmovetothenext deletep;//deletenodep }//theendofwhilestatement }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- List 成员 函数 拓展 实验 报告