c++怎么实现单链表反转然后交错重连和稀疏矩阵

c++怎么实现单链表反转然后交错重连和稀疏矩阵

本篇内容主要讲解“c++怎么实现单链表反转然后交错重连和稀疏矩阵”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“c++怎么实现单链表反转然后交错重连和稀疏矩阵”吧!

/******一leetcode题目1234n1n-12n-23n-3*****/#include<iostream>#include<assert.h>#include<vector>//容器--类模板#include<stdlib.h>//利用随机值#include<time.h>usingnamespacestd;#defineN1000#defineK100typedefstructnode{intx;node*next;public:node():x(1),next(NULL){}node(inta):x(a),next(NULL){}}node;intxx[]={0,7,6,5,4,3,2,1,11,12,0};inti=0;voidlinkcreat(node*head){if(head==NULL){head=newnode;}head->x=xx[i++];while(i<10){node*add=newnode(xx[i++]);add->next=head->next;head->next=add;}}voidshow(node*head){node*p=head;while(p){cout<<p->x<<"";p=p->next;}cout<<endl;}voidV(node*&head){node*newhead=head;node*p=head;node*q=head;node*t=NULL;while(p->next!=NULL){q=p;p=p->next;q->next=t;t=q;}head=p;p->next=q;}voidV(node*&head,intk){node*newhead=head;node*p=head;node*q=head;node*t=NULL;while(k--){q=p;p=p->next;q->next=t;t=q;}head=q;newhead->next=p;}voidVV(node*&head){//快慢指针找到中间结点node*p=head;node*q=head;while(p->next->next!=NULL){p=p->next->next;q=q->next;}cout<<q->x<<endl;//反转后面的结点p=q->next;node*qq=q->next;node*t=NULL;while(p->next!=NULL){qq=p;p=p->next;qq->next=t;t=qq;}p->next=qq;q->next=p;//从新链接合并node*pp=head;q->next=NULL;while(p->next!=NULL){t=p;p=p->next;t->next=pp->next;pp->next=t;pp=pp->next->next;}q->next=p;}intmain(){node*head=newnode(1);linkcreat(head);show(head);VV(head);show(head);}/**********#include"wz.h"template<classT>structelement{introw,col;//行数、列数Titem;//元素值};constintMaxTerm=100;template<classT>classSparseMatrix{public:SparseMatrix(){};SparseMatrix(intintmu,intintnu,intinttu,element<T>datatemp[]);//有参构造函数,初始化稀疏矩阵~SparseMatrix(){};//析构函数,释放存储空间element<T>GetMatrix(intintnumber);//输出下标对应的数组元素voidPrt();//显示三元组顺序表voidTrans1(SparseMatrix<T>&B);//直接取、顺序存的矩阵转置算法voidTrans2(SparseMatrix<T>A,SparseMatrix<T>&B);//顺序取、直接存的矩阵转置算法private:element<T>data[MaxTerm];//矩阵非零元素intmu,nu,tu;//行数、列数、非零元个数};#endiftemplate<classT>SparseMatrix<T>::SparseMatrix(intintmu,intintnu,intinttu,element<T>datatemp[]){if(inttu>MaxTerm)throw"构造函数的初始化参数不正确";mu=intmu;nu=intnu;tu=inttu;for(inti=0;i<inttu;i++){data[i]=datatemp[i];}}template<classT>element<T>SparseMatrix<T>::GetMatrix(intintnumber){if(intnumber>=tu||intnumber<0)throw"输入位置不正确";returndata[i];}template<classT>voidSparseMatrix<T>::Prt(){for(inti=0;i<tu;i++){cout<<data[i].col<<""<<data[i].row<<""<<data[i].item<<"\n";}}template<classT>voidSparseMatrix<T>::Trans1(SparseMatrix<T>&B){intpb,pa;B.mu=this->nu;B.nu=this->mu;B.tu=this->tu;//设置行数、列数、非零元素个数 if(B.tu>0)//有非零元素则转换{pb=0;for(intcol=0;col<this->nu;col++)//依次考察每一列for(pa=0;pa<this->tu;pa++)//在A中扫描整个三元组表if(this->data[pa].col==col)//处理col列元素{B.data[pb].row=this->data[pa].col;B.data[pb].col=this->data[pa].row;B.data[pb].item=this->data[pa].item;pb++;}}}template<classT>voidSparseMatrix<T>::Trans2(SparseMatrix<T>A,SparseMatrix<T>&B){inti,j,k,num[MaxTerm],cpot[MaxTerm];B.mu=A.nu;B.nu=A.mu;B.tu=A.tu;//设置行数、列数、元素个数if(B.tu>0)//有非零元素则转换{for(i=0;i<A.nu;i++)//A中每一列非零元素的个数初始化为0num[i]=0;for(i=0;i<A.tu;i++)//求矩阵A中每一列非零元素的个数{j=A.data[i].col;//取三元组的列号num[j]++;}cpot[0]=0;//A中第0列第一个非零元素在B中的位置为0for(i=1;i<A.nu;i++)//求A中每一列第一个非零元素在B中的下标cpot[i]=cpot[i-1]+num[i-1];for(i=0;i<A.tu;i++)//扫描三元组表A{j=A.data[i].col;//当前三元组的列号k=cpot[j];//当前三元组在B中的下标B.data[k].row=A.data[i].col;B.data[k].col=A.data[i].row;B.data[k].item=A.data[i].item;cpot[j]++;//预置同一列的下一个三元组的下标}}}intmain(){try{//建立一个element<int>类型的数组(A)element<int>elementtemp,elementtemp3,elementtemp2;elementtemp.col=0;elementtemp.row=0;elementtemp.item=15;elementtemp2.col=1;elementtemp2.row=2;elementtemp2.item=16;elementtemp3.col=1;elementtemp3.row=0;elementtemp3.item=17;element<int>A[3];A[0]=elementtemp;A[1]=elementtemp2;A[2]=elementtemp3;SparseMatrix<int>sparsematrixB;//构造三元组顺序表来存储转置后的三元组顺序表SparseMatrix<int>sparsematrixA(3,3,3,A);//构造三元组顺序表cout<<"源三元组顺序表如下:"<<"\n";sparsematrixA.Prt();//显示三元组顺序表sparsematrixA.Trans1(sparsematrixB);cout<<"使用直接取、顺序存转置算法转置后的三元组顺序表如下:"<<"\n";sparsematrixB.Prt();//显示三元组顺序表sparsematrixA.Trans2(sparsematrixA,sparsematrixB);cout<<"使用顺序取、直接存转置算法转置后的三元组顺序表如下:"<<"\n";sparsematrixB.Prt();//显示三元组顺序表}catch(char*e){cout<<e;}return0;}************/

到此,相信大家对“c++怎么实现单链表反转然后交错重连和稀疏矩阵”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

c++怎么实现单链表反转然后交错重连和稀疏矩阵

发布于 2022-01-10 23:46:22
收藏
分享
海报
0 条评论
45
上一篇:零基础入门web前端的学习方法是什么 下一篇:c语言插入法排序怎么实现
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码