c++怎么实现单链表反转然后交错重连和稀疏矩阵
发表于:2025-02-19 作者:千家信息网编辑
千家信息网最后更新 2025年02月19日,本篇内容主要讲解"c++怎么实现单链表反转然后交错重连和稀疏矩阵",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"c++怎么实现单链表反转然后交错重连和稀疏矩
千家信息网最后更新 2025年02月19日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安全错误
数据库的锁怎样保障安全
应对网络安全威胁措施
国企网络安全员
社区网络安全签名活动
网络技术手段助力信访举报
6.0cydia数据库
宜春高性价比服务器需要多少钱
软件开发减税申请
dnf怀旧服租服务器
天涯海南棋牌软件开发
渝中区咨询软件开发服务特点
校园网络安全拓扑图设计
互联网科技英语演讲
我国三大全文数据库
网络安全无小事从你我身边做起
前置服务器名词解释
网络安全看动漫插件
智慧医疗 网络安全
电脑中找到管理服务器
数据库2005密钥
五一节网络安全检查
影之刃角色查询服务器
美国网络安全技术趋势
自己公司弄个服务器安全么
服务器怎么添加任务管理器
mysql数据库建模方式
计算机网络技术的工资吗
数据库sql语句函数名
oracel数据库登录不上
如何建立数据库中建立视图
黄石软件开发10大企业