if(element[i]==t)
returni;
return-1;
}
templatevoidA:
:
SetElement(intindex,constT&value){
element[index]=value;
}
intmain(){
AintAry;//用int实例化,建立模板类对象
AdouAry;//用double实例化,建立模板类对象
inti;
for(i=0;i<5;i++)
intAry.SetElement(i,i+3);
for(i=0;i<10;i++)
douAry.SetElement(i,(i+i)*0.35);
i=intAry.Search(7);
if(i>=0)cout<
i=douAry.Search(0.7);
if(i>=0)cout<
return0;
}
8.设计一个单向链表类模板,结点数据域中数据从小到大排列,并设计插入、删除节点的成员函数。
【解答】
#include
usingnamespacestd;
templateclassList;
templateclassNode{
Tinfo;//数据域
Node*link;//指针域
public:
Node();//生成头结点的构造函数
Node(constT&data);//生成一般结点的构造函数
friendclassList;
};
templateNode:
:
Node(){link=NULL;}
templateNode:
:
Node(constT&data){
info=data;
link=NULL;
}
//定义链表类
templateclassList{
Node*head;//链表头指针和尾指针
public:
List();//构造函数,生成头结点(空链表)
~List();//析构函数
voidMakeEmpty();//清空一个链表,只余表头结点
Node*Find(Tdata);//搜索数据域与data相同的结点,返回该结点的地址
voidPrintList();//打印链表的数据域
voidInsertOrder(Node*p);//按升序生成链表
Node*CreatNode(Tdata);//创建一个结点(孤立结点)
Node*DeleteNode(Node*p);//删除指定结点
};
templateList:
:
List(){
head=newNode(-9999);//头结点,最小的数据从小到大插入
}
templateList:
:
~List(){
MakeEmpty();
deletehead;
}
templatevoidList:
:
MakeEmpty(){
Node*tempP;
while(head->link!
=NULL){
tempP=head->link;
head->link=tempP->link;//把头结点后的第一个节点从链中脱离
deletetempP;//删除(释放)脱离下来的结点
}
}
templateNode*List:
:
Find(Tdata){
Node*tempP=head->link;
while(tempP!
=NULL&&tempP->info!
=data)tempP=tempP->link;
returntempP;//搜索成功返回该结点地址,不成功返回NULL
}
templatevoidList:
:
PrintList(){
Node*tempP=head->link;
while(tempP!
=NULL){
cout<info<<'\t';
tempP=tempP->link;
}
cout<}
templatevoidList:
:
InsertOrder(Node*p){
Node*tempP=head,*tempQ=head;//tempQ指向tempP前面的一个节点
while(tempP!
=NULL){
if(p->infoinfo)break;//找第一个比插入结点大的结点,由tempP指向
tempQ=tempP;
tempP=tempP->link;
}
p->link=tempP;
tempQ->link=p;
}
templateNode*List:
:
CreatNode(Tdata){//建立新节点
Node*tempP=newNode(data);
returntempP;
}
templateNode*List:
:
DeleteNode(Node*p){
Node*tempP=head->link,*tempQ=head,*tempC;
while(tempP!
=NULL&&tempP!
=p){
tempQ=tempP;
tempP=tempP->link;
}
tempC=tempP;
tempQ->link=tempP->link;
returntempC;
}
intmain(){
Node*P1;
Listlist1;
inta[10]={20,12,0,-5,9,-18,6,11,5,3},i,j;
for(i=0;i<10;i++){
P1=list1.CreatNode(a[i]);
list1.InsertOrder(P1);
}
list1.PrintList();
cout<<"请输入一个要求删除的整数"<cin>>j;
P1=list1.Find(j);
if(P1!
=NULL){
P1=list1.DeleteNode(P1);
deleteP1;
list1.PrintList();
}
elsecout<<"未找到"<cout<<"请输入一个要求插入的整数"<cin>>j;
P1=list1.CreatNode(j);
list1.InsertOrder(P1);
list1.PrintList();
list1.MakeEmpty();//清空list1
list1.PrintList();
return0;
}
9.为单链表类模板增加一个复制构造函数和赋值运算符(=)。
在上题基础上,List类增加一个复制构造函数和赋值运算符(=)
【解答】
templateList:
:
List(List&l){
head=newNode(-9999);//现建立头结点
Node*tempP=l.head->link,*tempC;
while(tempP!
=NULL){
tempC=CreatNode(tempP->info);
InsertAfter(tempC);
tempP=tempP->link;
}
}
templateList&List:
:
operator=(List&l){
MakeEmpty();//先释放原来链表的数据结点
Node*tempP=l.head->link,*tempC;
while(tempP!
=NULL){
tempC=CreatNode(tempP->info);
InsertAfter(tempC);
tempP=tempP->link;
}
return*this;
}
intmain(){
Node*P1;
Listlist1,list2;
inta[10]={20,12,0,-5,9,-18,6,11,5,3},i,j;
for(i=0;i<10;i++){
P1=list1.CreatNode(a[i]);
list1.InsertOrder(P1);
}
list1.PrintList();
cout<<"请输入一个要求删除的整数"<cin>>j;
P1=list1.Find(j);
if(P1!
=NULL){
P1=list1.DeleteNode(P1);
deleteP1;
list1.PrintList();
}
elsecout<<"未找到"<cout<<"请输入一个要求插入的整数"<cin>>j;
P1=list1.CreatNode(j);
list1.InsertOrder(P1);
list1.PrintList();
list2=list1;
list2.PrintList();
Listlist3=list1;
list3.PrintList();
cout<<"请输入一个要求删除的整数"<cin>>j;
P1=list1.Find(j);
if(P1!
=NULL){
P1=list1.DeleteNode(P1);
deleteP1;
list1.PrintList();
}
elsecout<<"未找到"<list2=list3=list1;
list2.PrintList();
list3.PrintList();
list1.MakeEmpty();//清空list1
list2.MakeEmpty();//清空list1
list3.MakeEmpty();//清空list1
return0;
}
10.编写一个用来排序的函数模板,对下列数组进行降序的选择法排序。
初始化int类型数组data[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20}
答:
/*
函数功能:
对数组中的某一部分进行选择法降序排序。
函数原形:
voidSelectSort(DataTypea[],intl,intr);
参数:
DataTypea[]:
欲排序的数组;
intl:
有序序列在数组中的起始位置;
intr:
有序序列在数组中的结束位置;
返回值:
无。
*/
#include
usingnamespacestd;
template
voidSelectSort(DataTypea[],intl,intr)
{
inti,j;
DataTypek;
for(i=l;i<=r-1;i++)
for(j=i+1;j<=r;j++)
if(a[i]{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
intmain(void)
{
intdata[]={1,3,5,7,9,11,13,15,17,19,2,4,6,8,10,12,14,16,18,20};
inti;
SelectSort(data,0,19);
for(i=0;i<=19;i++)
{
cout<}
cout<return0;
}