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//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&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;iofarray"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
}