c++怎么实现单链表反转然后交错重连和稀疏矩阵
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容主要讲解"c++怎么实现单链表反转然后交错重连和稀疏矩阵",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"c++怎么实现单链表反转然后交错重连和稀疏矩
千家信息网最后更新 2026年11月03日c++怎么实现单链表反转然后交错重连和稀疏矩阵
本篇内容主要讲解"c++怎么实现单链表反转然后交错重连和稀疏矩阵",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"c++怎么实现单链表反转然后交错重连和稀疏矩阵"吧!
/******一leetcode题目 1 2 3 4 n 1 n-1 2 n-2 3 n-3 *****/ #include#include #include //容器--类模板#include //利用随机值#include using namespace std; #define N 1000 #define K 100 typedef struct node{ int x; node *next;public: node():x(1) ,next(NULL){} node(int a):x(a), next(NULL){}}node;int xx[]={0,7,6,5,4,3,2,1,11,12,0};int i=0; void linkcreat(node*head){if(head==NULL){ head=new node;}head->x=xx[i++];while(i<10){node *add=new node(xx[i++]);add->next=head->next;head->next=add;}}void show(node *head){node *p=head; while(p){cout< x<<" ";p=p->next;}cout< next!=NULL) { q=p; p=p->next; q->next=t; t=q; } head=p; p->next=q; }void V(node *&head,int k) { 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; } void VV(node *&head) {//快慢指针找到中间结点 node *p=head; node *q=head; while(p->next->next!=NULL) { p=p->next->next; q=q->next; } cout< x< 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; } int main(){ node *head=new node(1); linkcreat(head); show(head); VV(head); show(head);}/**********#include"wz.h"template struct element{ int row, col; //行数、列数 T item; //元素值};const int MaxTerm=100;template class SparseMatrix{ public: SparseMatrix(){}; SparseMatrix(int intmu,int intnu,int inttu,element datatemp[]);//有参构造函数,初始化稀疏矩阵 ~SparseMatrix(){}; //析构函数,释放存储空间 element GetMatrix(int intnumber);//输出下标对应的数组元素 void Prt();//显示三元组顺序表 void Trans1(SparseMatrix &B);//直接取、顺序存的矩阵转置算法 void Trans2(SparseMatrix A, SparseMatrix &B);//顺序取、直接存的矩阵转置算法 private: element data[MaxTerm]; //矩阵非零元素 int mu, nu, tu; //行数、列数、非零元个数};#endiftemplate SparseMatrix ::SparseMatrix(int intmu,int intnu,int inttu,element datatemp[]){ if (inttu >MaxTerm ) throw "构造函数的初始化参数不正确"; mu = intmu;nu = intnu;tu = inttu; for(int i=0;i element SparseMatrix ::GetMatrix(int intnumber){ if(intnumber>=tu || intnumber < 0) throw "输入位置不正确"; return data[i];} template void SparseMatrix ::Prt(){ for(int i=0;i void SparseMatrix ::Trans1(SparseMatrix &B){ int pb,pa; B.mu=this->nu; B.nu=this->mu; B.tu=this->tu;//设置行数、列数、非零元素个数 if (B.tu>0) //有非零元素则转换 { pb = 0; for (int col=0; col nu; col++) //依次考察每一列 for (pa=0; pa 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 void SparseMatrix ::Trans2(SparseMatrix A, SparseMatrix &B){ int i,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) element 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 A[3];A[0] = elementtemp;A[1] = elementtemp2;A[2] = elementtemp3; SparseMatrix sparsematrixB;//构造三元组顺序表来存储转置后的三元组顺序表 SparseMatrix 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< 到此,相信大家对"c++怎么实现单链表反转然后交错重连和稀疏矩阵"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
三元
顺序
元素
矩阵
个数
稀疏
下标
算法
c++
交错
函数
位置
内容
数组
存储
学习
实用
更深
兴趣
参数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技网
河南云网互联网络科技有限公司
苏州找大状互联网科技有限公司
科技和互联网公司
科技互联网政务服务
科技信息互联网
与互联网科技公司
智网互联网科技
亿账通互联网科技
互联网 新科技
上海亿帐通互联网科技
互联网科技有限公司 翻译
昭通互联网科技选哪家
互联网科技研发水平
中和天地互联网科技有限公司
烟台灵鸽互联网科技有限公司
互联网公司高科技产品
领航科技大厦互联网公司
互联网科技数字货币
吉林省码上互联网科技电话
喔噻互联网科技
上海挚品互联网科技
腾讯科技 互联网女皇
玖二柒互联网科技公司待遇
宁波青枫互联网科技有限公司
科技互联网海报
从那以后互联网科技有限公司
福州德业互联网科技
互联网科技博主张洲
河南商务圈互联网科技有限公司