资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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;   //行数、列数、非零元个数
};



#endif

template 
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; colnu; col++)  //依次考察每一列
			for (pa=0; patu; 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++怎么实现单链表反转然后交错重连和稀疏矩阵
标题路径:http://cdkjz.cn/article/pipppg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220