c++怎么實(shí)現(xiàn)單鏈表反轉(zhuǎn)然后交錯重連和稀疏矩陣

本篇內(nèi)容主要講解“c++怎么實(shí)現(xiàn)單鏈表反轉(zhuǎn)然后交錯重連和稀疏矩陣”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“c++怎么實(shí)現(xiàn)單鏈表反轉(zhuǎn)然后交錯重連和稀疏矩陣”吧!

專注于為中小企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)本溪免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

/******
一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>//利用隨機(jī)值
#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)  
 {//快慢指針找到中間結(jié)點(diǎn)
     node *p=head;
     node *q=head;
      while(p->next->next!=NULL)
     {
         p=p->next->next;
         q=q->next;
     }
     cout<<q->x<<endl;
 //反轉(zhuǎn)后面的結(jié)點(diǎn)
 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; //行數(shù)、列數(shù)
  T item; //元素值
};
const int MaxTerm=100;
template <class T>
class SparseMatrix
{
	public:
		SparseMatrix(){};
		SparseMatrix(int intmu,int intnu,int inttu,element<T> datatemp[]);//有參構(gòu)造函數(shù),初始化稀疏矩陣
		~SparseMatrix(){}; //析構(gòu)函數(shù),釋放存儲空間
		element<T> GetMatrix(int intnumber);//輸出下標(biāo)對應(yīng)的數(shù)組元素
		void Prt();//顯示三元組順序表
		void Trans1(SparseMatrix<T> &B);//直接取、順序存的矩陣轉(zhuǎn)置算法
		void Trans2(SparseMatrix<T> A, SparseMatrix<T> &B);//順序取、直接存的矩陣轉(zhuǎn)置算法
	private:
		element<T> data[MaxTerm]; //矩陣非零元素
		int mu, nu, tu;   //行數(shù)、列數(shù)、非零元個數(shù)
};



#endif

template <class T>
SparseMatrix<T>::SparseMatrix(int intmu,int intnu,int inttu,element<T> datatemp[])
{
	if (inttu >MaxTerm ) throw "構(gòu)造函數(shù)的初始化參數(shù)不正確";
	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;//設(shè)置行數(shù)、列數(shù)、非零元素個數(shù) 

	if (B.tu>0) //有非零元素則轉(zhuǎn)換
	{
		
		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;//設(shè)置行數(shù)、列數(shù)、元素個數(shù)
	if (B.tu>0)  //有非零元素則轉(zhuǎn)換
	{
	for (i=0; i<A.nu; i++)    //A中每一列非零元素的個數(shù)初始化為0
		num[i]=0;
  for (i=0; i<A.tu; i++)//求矩陣A中每一列非零元素的個數(shù)
  {
		j= A.data[i].col;     //取三元組的列號
    num[j]++;
  }  
	cpot[0]=0;     //A中第0列第一個非零元素在B中的位置為0
	for (i=1; i<A.nu; i++)  //求A中每一列第一個非零元素在B中的下標(biāo)
	cpot[i]= cpot[i-1]+num[i-1];
	for (i=0; i<A.tu; i++)//掃描三元組表A
  {
		j=A.data[i].col;      //當(dāng)前三元組的列號
    k=cpot[j]; //當(dāng)前三元組在B中的下標(biāo)
    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]++;             //預(yù)置同一列的下一個三元組的下標(biāo)
	}  
	}
}
 
int main()
{
	try
	{
		//建立一個element<int>類型的數(shù)組(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;//構(gòu)造三元組順序表來存儲轉(zhuǎn)置后的三元組順序表
		SparseMatrix<int> sparsematrixA(3,3,3,A);//構(gòu)造三元組順序表
		cout<<"源三元組順序表如下:"<<"\n";
		sparsematrixA.Prt();//顯示三元組順序表
		sparsematrixA.Trans1(sparsematrixB);
		cout<<"使用直接取、順序存轉(zhuǎn)置算法轉(zhuǎn)置后的三元組順序表如下:"<<"\n";
		sparsematrixB.Prt();//顯示三元組順序表
		sparsematrixA.Trans2(sparsematrixA,sparsematrixB);
		cout<<"使用順序取、直接存轉(zhuǎn)置算法轉(zhuǎn)置后的三元組順序表如下:"<<"\n";
		sparsematrixB.Prt();//顯示三元組順序表
	}
	catch(char* e)
	{
		cout<<e;
	}


  return 0;
}
************/

到此,相信大家對“c++怎么實(shí)現(xiàn)單鏈表反轉(zhuǎn)然后交錯重連和稀疏矩陣”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)站名稱:c++怎么實(shí)現(xiàn)單鏈表反轉(zhuǎn)然后交錯重連和稀疏矩陣
標(biāo)題URL:http://muchs.cn/article36/pipppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT網(wǎng)站營銷、品牌網(wǎng)站制作商城網(wǎng)站、網(wǎng)站維護(hù)動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化