数据结构课后习题.docx
《数据结构课后习题.docx》由会员分享,可在线阅读,更多相关《数据结构课后习题.docx(53页珍藏版)》请在冰豆网上搜索。
数据结构课后习题
第一章
1.15
#include
usingnamespacestd;
voidcompare(int&a,int&b,int&c)//引用,这里的改变可以影响主函数
{
intt;
if(a
t=a;
a=b;
b=t;
}
if(a{t=a;a=c;c=t;}if(b{t=b;b=c;c=t;}//交换后大小排序为a>b>c}intmain(){inta=55,b=64,c=39;cout<<"数据为:"<compare(a,b,c);cout<<"最大的数为:"<cout<<"中间的数为:"<cout<<"最小的数为:"<return0;} 1.17#includeusingnamespacestd;intmain(){int*array,number;cout<<"请输入数组元素的个数:";cin>>number;array=newint[number];cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
a=c;
c=t;
if(b{t=b;b=c;c=t;}//交换后大小排序为a>b>c}intmain(){inta=55,b=64,c=39;cout<<"数据为:"<compare(a,b,c);cout<<"最大的数为:"<cout<<"中间的数为:"<cout<<"最小的数为:"<return0;} 1.17#includeusingnamespacestd;intmain(){int*array,number;cout<<"请输入数组元素的个数:";cin>>number;array=newint[number];cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
{t=b;
b=c;
}//交换后大小排序为a>b>c
intmain()
inta=55,b=64,c=39;
cout<<"数据为:
"<compare(a,b,c);cout<<"最大的数为:"<cout<<"中间的数为:"<cout<<"最小的数为:"<return0;} 1.17#includeusingnamespacestd;intmain(){int*array,number;cout<<"请输入数组元素的个数:";cin>>number;array=newint[number];cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
compare(a,b,c);
cout<<"最大的数为:
"<cout<<"中间的数为:"<cout<<"最小的数为:"<return0;} 1.17#includeusingnamespacestd;intmain(){int*array,number;cout<<"请输入数组元素的个数:";cin>>number;array=newint[number];cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"中间的数为:
"<
cout<<"最小的数为:
"<return0;} 1.17#includeusingnamespacestd;intmain(){int*array,number;cout<<"请输入数组元素的个数:";cin>>number;array=newint[number];cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
return0;
1.17
int*array,number;
cout<<"请输入数组元素的个数:
";
cin>>number;
array=newint[number];
cout<<"---------------------------------------------------------"<cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"请分别输入"<cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"---------------------------------------------------------"<for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
for(inti=0;i{cout<<"请输入第"<"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"请输入第"<
"<cin>>array[i];if(array[i]<0||array[i]>200){cout<<"输入错误";i--;continue;}}//输出原来数据cout<<"原来数据为:";for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cin>>array[i];
if(array[i]<0||array[i]>200)
cout<<"输入错误";
i--;
continue;
//输出原来数据
cout<<"原来数据为:
for(intn=0;n{cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<}cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数for(intk=0;k<5;k++){array1[k]=0;}for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
intarray1[5];//用于存储[0,20],[21,50],[51,80],[81,130],[131,200]的个数
for(intk=0;k<5;k++)
array1[k]=0;
for(intj=0;j{if(array[j]<=20)array1[0]++;elseif(array[j]<=50)array1[1]++;elseif(array[j]<=80)array1[2]++;elseif(array[j]<=130)array1[3]++;elsearray1[4]++;}cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
if(array[j]<=20)array1[0]++;
elseif(array[j]<=50)array1[1]++;
elseif(array[j]<=80)array1[2]++;
elseif(array[j]<=130)array1[3]++;
elsearray1[4]++;
cout<<"[0,20]的个数为"<cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"[21,50]的个数为"<cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"[51,80]的个数为"<cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"[81,130]的个数为"<cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"[131,200]的个数为"<delete[]array;return0;} 第二章2.6#include#includeusingnamespacestd;templateclassList{protected:T*data;//数组intmaxsize;//数组元素个数intlast;//数组下标public:List(intmaxsize1=50);//构造函数~List(){delete[]data;}//析构函数boolisfull(){return(last==maxsize-1)?true:false;}//判断是否已满voidresize();//扩大数组空间voidinput();//存入数据voidprint();//输出数据voidconvert();//原地置换};templateList::List(intmaxsize1)//构造函数{last=-1;maxsize=maxsize1;data=newT[maxsize];if(data==NULL){cerr<<"存储分配出现错误!"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
delete[]array;
第二章
2.6
template
classList
protected:
T*data;//数组
intmaxsize;//数组元素个数
intlast;//数组下标
public:
List(intmaxsize1=50);//构造函数
~List(){delete[]data;}//析构函数
boolisfull(){return(last==maxsize-1)?
true:
false;}//判断是否已满
voidresize();//扩大数组空间
voidinput();//存入数据
voidprint();//输出数据
voidconvert();//原地置换
};
List:
:
List(intmaxsize1)//构造函数
last=-1;
maxsize=maxsize1;
data=newT[maxsize];
if(data==NULL)
cerr<<"存储分配出现错误!
"<exit(1);//非正常运行导致退出程序}}templatevoidList::input()//存入数据{if(isfull()){resize();}cin>>data[++last];}templatevoidList::resize()//扩大数组空间{intnewmaxsize=maxsize*2;T*newarray=newT[newmaxsize];for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
exit
(1);//非正常运行导致退出程序
voidList:
input()//存入数据
if(isfull())
resize();
cin>>data[++last];
resize()//扩大数组空间
intnewmaxsize=maxsize*2;
T*newarray=newT[newmaxsize];
for(inti=0;i{newarray[i]=data[i];}delete[]data;data=newarray;maxsize=newmaxsize;}templatevoidList::print()//输出数据{for(intk=0;k<=last;k++){cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
newarray[i]=data[i];
delete[]data;
data=newarray;
maxsize=newmaxsize;
print()//输出数据
for(intk=0;k<=last;k++)
cout<}cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<}templatevoidList::convert()//原地置换{Tt;for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
convert()//原地置换
Tt;
for(intj=0;j{t=data[j];data[j]=data[last-j];data[last-j]=t;}} voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择{charc;cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
t=data[j];
data[j]=data[last-j];
data[last-j]=t;
voidprocess(intnum0)//属于调用函数,对输入的参数进行不同类型数据的选择
charc;
cout<<"请输入数据(按enter键结束输入)"<if(num0==1){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
if(num0==1)
ListList;
do
List.input();
c=getchar();
}while(c!
='\n');
cout<<"开始数据如下:
"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
List.print();//调用输出函数
List.convert();//调用原地置换函数
cout<<"置换数据如下:
"<List.print();//调用输出函数}elseif(num0==2){ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
elseif(num0==2)
"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
"<List.print();//调用输出函数}else{ListList;do{List.input();c=getchar();}while(c!='\n');cout<<"开始数据如下:"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
else
"<List.print();//调用输出函数List.convert();//调用原地置换函数cout<<"置换数据如下:"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
"<List.print();//调用输出函数}}intmain(){intnum0;//num0表示选择的数据类型cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
intnum0;//num0表示选择的数据类型
cout<<"------------------------------------------------------------------"<cout<<"请选择:1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"请选择:
1.输入字符串2.输入整型数据3.输入双进度型数据"<cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"------------------------------------------------------------------"<cin>>num0;process(num0);//调用process函数return0;} 2.14#includeusingnamespacestd;templateclassNode//结点类{Tdata;public:Node*link;Node(Tdata1)//构造函数,一般用于创建新节点{data=data1;link=NULL;}Node()//构造函数,一般用于创建附加节点{link=NULL;}Tgetdata()//获取数据{returndata;}};templateclassList//用类的组合的方法建单链表{intnum;//记录除附加头结点节点的个数Node*first;//结点类的对象当作List的私有成员,头指针firstpublic:List()//构造函数,建立附加头节点{first=newNode;}voidinput(intnum1);//输入数据函数Node*locate(inti);//定位函数,返回结点地址voidprint();//输出数据函数voidgetmax();//求最大值函数voidnumber();//统计函数voidcreate();//根据一维数组建立一个单链表};templatevoidList::input(intnum1){intdata1;num=num1;for(inti=1;i<=num;i++){cout<<"请输入第"<";cin>>data1;Node*newnode=newNode(data1);if(newnode==NULL){cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cin>>num0;
process(num0);//调用process函数
2.14
classNode//结点类
Tdata;
Node*link;
Node(Tdata1)//构造函数,一般用于创建新节点
data=data1;
link=NULL;
Node()//构造函数,一般用于创建附加节点
Tgetdata()//获取数据
returndata;
classList//用类的组合的方法建单链表
intnum;//记录除附加头结点节点的个数
Node*first;//结点类的对象当作List的私有成员,头指针first
List()//构造函数,建立附加头节点
first=newNode;
voidinput(intnum1);//输入数据函数
Node*locate(inti);//定位函数,返回结点地址
voidprint();//输出数据函数
voidgetmax();//求最大值函数
voidnumber();//统计函数
voidcreate();//根据一维数组建立一个单链表
input(intnum1)
intdata1;
num=num1;
for(inti=1;i<=num;i++)
cin>>data1;
Node*newnode=newNode(data1);
if(newnode==NULL)
cerr<<"分配空间错误"<exit(1);}newnode->link=first->link;//逆序输入,先输入的放后面first->link=newnode;}} templatevoidList::print(){Node*current=first;for(inti=1;i<=num;i++){cout<link->getdata()<<'\t';current=current->link;}cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
(1);
newnode->link=first->link;//逆序输入,先输入的放后面
first->link=newnode;
print()
Node*current=first;
cout<link->getdata()<<'\t';
current=current->link;
cout<}templateNode*List::locate(inti){if(i>num||i<1){cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
Node*List:
locate(inti)
if(i>num||i<1)
cout<<"表中没有这个结点"<returnNULL;}else{Node*current=first->link;for(intk=1;k<=num;k++){if(i==k){returncurrent;}elsecurrent=current->link;}}}templatevoidList::getmax(){intmax,n=1;Node*current=first->link;max=current->getdata();for(inti=1;i<=num;i++){if(maxgetdata()){max=current->getdata();}current=current->link;}current=first->link;for(intj=1;j<=num;j++)//找最大数的结点为第几个{if(max==current->getdata()){break;}current=current->link;n++;}cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
returnNULL;
Node*current=first->link;
for(intk=1;k<=num;k++)
if(i==k)
returncurrent;
elsecurrent=current->link;
getmax()
intmax,n=1;
max=current->getdata();
if(maxgetdata())
current=first->link;
for(intj=1;j<=num;j++)//找最大数的结点为第几个
if(max==current->getdata())
break;
n++;
cout<<"第"<"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
"<} templatevoidList::number(){intnumber11,num11=0;cout<<"请输入要统计的整数:";cin>>number11;Node*current=first->link;for(inti=1;i<=num;i++){if(number11==current->getdata()){num11++;}current=current->link;}if(num11==0){cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
number()
intnumber11,num11=0;
cout<<"请输入要统计的整数:
cin>>number11;
if(number11==current->getdata())
num11++;
if(num11==0)
cout<<"没有"<}else{cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<}}templatevoidList::create(){intn;int*a=newint[n];cout<<"请输入想要定义元素的个数:";cin>>n;for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
create()
intn;
int*a=newint[n];
cout<<"请输入想要定义元素的个数:
cin>>n;
for(inti=0;i{cout<<"请输入第"<";cin>>a[i];}Node*first1=newNode,*last1=first1;//附加头结点,尾指针for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cin>>a[i];
Node*first1=newNode,*last1=first1;//附加头结点,尾指针
for(intj=0;j{Node*newnode=newNode(a[j]);last1->link=newnode;last1=newnode;}cout<<"数组输出情况:";for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
Node*newnode=newNode(a[j]);
last1->link=newnode;
last1=newnode;
cout<<"数组输出情况:
for(intk=0;k{cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<}cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<cout<<"链表输出情况:";Node*current1=first1->link;for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"链表输出情况:
Node*current1=first1->link;
for(intk1=0;k1{cout<getdata()<<'\t';current1=current1->link;}cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<getdata()<<'\t';
current1=current1->link;
cout<} intmain(){ListList1;//建立List1对象intnum1;//记录数据个数cout<<"请输入想要输入整型数据的个数:"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
ListList1;//建立List1对象
intnum1;//记录数据个数
cout<<"请输入想要输入整型数据的个数:
"<cin>>num1;List1.input(num1);List1.print();cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cin>>num1;
List1.input(num1);
List1.print();
cout<<"----------------------测试locate函数----------------------"<intnum2;Node*Node2;cout<<"请输入你想要定位第几个数";cin>>num2;Node2=List1.locate(num2);cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
intnum2;
Node*Node2;
cout<<"请输入你想要定位第几个数";
cin>>num2;
Node2=List1.locate(num2);
cout<<"该表中第"<getdata()<cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"---------------------------------------------------------"<cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"----------------------测试getmax()函数----------------------"<List1.getmax();cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
List1.getmax();
cout<<"---------------------------------------------------------"<cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"----------------------测试number()函数----------------------"<List1.number();cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
List1.number();
cout<<"---------------------------------------------------------"<cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
cout<<"----------------------测试create()函数----------------------"<List1.create();cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
List1.create();
cout<<"---------------------------------------------------------"<return0;} 第三章3.12用栈后进先出的理念解决进制转换问题#includeusingnamespacestd;templateclassstack{T*element;//元素inttop;//指向头指针intmaxsize;//存储的空间public:stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针{top=-1;maxsize=maxsize1;element=newT[maxsize];}voidinput(intN,intB);//输入设置voidprint();}; templatevoidstack::input(intN,intB)//输入设置{while(true){element[++top]=N%B;N=N/B;if(N==0){break;}}}templatevoidstack::print(){intn=top;//保护topwhile(n>-1){if(element[n]<16&&element[n]>=10){switch(element[n]){case10:cout<<"A";break;case11:cout<<"B";break;case12:cout<<"C";break;case13:cout<<"D";break;case14:cout<<"E";break;case15:cout<<"F";break;}}elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
第三章
3.12用栈后进先出的理念解决进制转换问题
classstack
T*element;//元素
inttop;//指向头指针
intmaxsize;//存储的空间
stack(intmaxsize1=100)//构造函数,初始化数组最大空间,栈顶指针
top=-1;
element=newT[maxsize];
voidinput(intN,intB);//输入设置
voidprint();
voidstack:
input(intN,intB)//输入设置
while(true)
element[++top]=N%B;
N=N/B;
if(N==0)
intn=top;//保护top
while(n>-1)
if(element[n]<16&&element[n]>=10)
switch(element[n])
case10:
cout<<"A";break;
case11:
cout<<"B";break;
case12:
cout<<"C";break;
case13:
cout<<"D";break;
case14:
cout<<"E";break;
case15:
cout<<"F";break;
elsecout<n--;}cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
n--;
cout<} intmain(){intN,B;cout<<"请输入要转换的十进制整数:";cin>>N;cout<<"请输入你要转换为几进制数(2到16之间):";cin>>B;stackstack1;stack1.input(N,B);cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
intN,B;
cout<<"请输入要转换的十进制整数:
cin>>N;
cout<<"请输入你要转换为几进制数(2到16之间):
cin>>B;
stackstack1;
stack1.input(N,B);
cout<<"十进制数"<";stack1.print();return0;} 3.16递归算法#includeusingnamespacestd;intackm(intm,intn){if(m==0){returnn+1;}elseif(m!=0&&n==0){returnackm(m-1,1);}else{returnackm(m-1,ackm(m,n
stack1.print();
3.16递归算法
intackm(intm,intn)
if(m==0)
returnn+1;
elseif(m!
=0&&n==0)
returnackm(m-1,1);
returnackm(m-1,ackm(m,n
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1