本篇内容主要讲解“c++怎么实现单链表反转然后交错重连和稀疏矩阵”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“c++怎么实现单链表反转然后交错重连和稀疏矩阵”吧!
/******
一leetcode题目
1 2 3 4 n
1 n-1 2 n-2 3 n-3
*****/
#include<iostream>
#include<assert.h>
#include<vector>//容器--类模板
#include<stdlib.h>//利用随机值
#include<time.h>
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<<p->x<<" ";
p=p->next;
}
cout<<endl;
}
void V(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;
}
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<<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;
}
int main()
{
node *head=new node(1);
linkcreat(head);
show(head);
VV(head);
show(head);
}
/**********
#include"wz.h"
template <class T>
struct element
{
int row, col; //行数、列数
T item; //元素值
};
const int MaxTerm=100;
template <class T>
class SparseMatrix
{
public:
SparseMatrix(){};
SparseMatrix(int intmu,int intnu,int inttu,element<T> datatemp[]);//有参构造函数,初始化稀疏矩阵
~SparseMatrix(){}; //析构函数,释放存储空间
element<T> GetMatrix(int intnumber);//输出下标对应的数组元素
void Prt();//显示三元组顺序表
void Trans1(SparseMatrix<T> &B);//直接取、顺序存的矩阵转置算法
void Trans2(SparseMatrix<T> A, SparseMatrix<T> &B);//顺序取、直接存的矩阵转置算法
private:
element<T> data[MaxTerm]; //矩阵非零元素
int mu, nu, tu; //行数、列数、非零元个数
};
#endif
template <class T>
SparseMatrix<T>::SparseMatrix(int intmu,int intnu,int inttu,element<T> datatemp[])
{
if (inttu >MaxTerm ) throw "构造函数的初始化参数不正确";
mu = intmu;nu = intnu;tu = inttu;
for(int i=0;i<inttu;i++)
{
data[i] = datatemp[i];
}
}
template <class T>
element<T> SparseMatrix<T>::GetMatrix(int intnumber)
{
if(intnumber>=tu || intnumber < 0) throw "输入位置不正确";
return data[i];
}
template <class T>
void SparseMatrix<T>::Prt()
{
for(int i=0;i<tu;i++)
{
cout<<data[i].col<<" "<<data[i].row<<" "<<data[i].item<<"\n";
}
}
template <class T>
void SparseMatrix<T>::Trans1(SparseMatrix<T> &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<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 <class T>
void SparseMatrix<T>::Trans2(SparseMatrix<T> A, SparseMatrix<T> &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.nu; i++) //A中每一列非零元素的个数初始化为0
num[i]=0;
for (i=0; i<A.tu; i++)//求矩阵A中每一列非零元素的个数
{
j= A.data[i].col; //取三元组的列号
num[j]++;
}
cpot[0]=0; //A中第0列第一个非零元素在B中的位置为0
for (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]++; //预置同一列的下一个三元组的下标
}
}
}
int main()
{
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;
}
return 0;
}
************/
到此,相信大家对“c++怎么实现单链表反转然后交错重连和稀疏矩阵”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。