當前位置:首頁 » 編程語言 » 十字鏈表轉置矩陣0c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

十字鏈表轉置矩陣0c語言

發布時間: 2022-09-06 19:16:59

1. c語言課程設計:稀疏矩陣應用 要求:實現三元組,十字鏈表下的稀疏矩陣的加、轉、乘的實現。

我剛寫了一個稀疏矩陣的代碼,如下
#include <iostream>
#include <iomanip>
using namespace std;

template<class T>
//三元組
struct Trituple
{
int row;
int col;
T val;
};
//稀疏矩陣聲明
template<class T>
class SparseMatrix
{
public:
SparseMatrix(int maxt=100);
~SparseMatrix();
bool TransposeTo(SparseMatrix &);
bool AddTo(const SparseMatrix&);
bool TransposeTo_Faster(SparseMatrix&);
void Input();
void Output();
private:
Trituple<T>* data;
int rows,cols,terms;
int maxterms;
};
template<class T>
SparseMatrix<T>::SparseMatrix(int maxt)
{
maxterms=maxt;
data=new Trituple<T>[maxterms];
terms=rows=cols=0;
}
template<class T>
SparseMatrix<T>::~SparseMatrix()
{
if (data!=NULL)
{
delete[] data;
}
}
//普通轉置
template<class T>
bool SparseMatrix<T>::TransposeTo(SparseMatrix &B)
{
if (terms>B.maxterms)
{
return false;
}
B.rows=cols;
B.cols=rows;
B.terms=terms;
if (terms>0)
{
int p=0;
for (int j=1;j<=cols;j++)
{
for (int k=0;k<terms;k++)
{
if (data[k].col==j)
{
B.data[p].row=j;
B.data[p].col=data[k].row;
B.data[p].val=data[k].val;
p++;
}
}
}
}
return true;
}
//快速轉置
template<class T>
bool SparseMatrix<T>::TransposeTo_Faster(SparseMatrix& B)
{
if (terms>B.maxterms)
{
return false;
}
B.rows=cols;
B.cols=rows;
B.terms=terms;
if (terms>0)
{
int *num,*cpot;
num=new int[cols];
cpot=new int[cols];
for (int j=0;j<cols;j++)
{
num[j]=0;
}
for (int k=0;k<terms;k++)
{
num[data[k].col-1]++;
}
//求出B中每一行的起始下標cpot[]
cpot[0]=0;
for (int j=1;j<cols;j++)
{
cpot[j]=cpot[j-1]+num[j-1];
}
//執行轉置操作
for (int p,k=0;k<terms;k++)
{
p=cpot[data[k].col-1]++;
B.data[p].row=data[k].col;
B.data[p].col=data[k].row;
B.data[p].val=data[k].val;
}
delete[] num;
delete[] cpot;
}
return true;
}
template<class T>
void SparseMatrix<T>::Input()
{
cout<<"intput the matrix' row:";
int row1;
cin>>row1;
cout<<"intput the matrix' col:";
int col1;
cin>>col1;
cout<<"input "<<row1<<"*"<<col1<<" matrix"<<endl;
int number;
rows=row1;
cols=col1;
for (int i=0;i<rows;i++)
{
for (int j=0;j<cols;j++)
{
cin>>number;
if (number!=0)
{
data[terms].row=i+1;
data[terms].col=j+1;
data[terms].val=number;
terms++;
}
}
}
}
template<class T> //輸出好看,但是違背了最初的原則
void SparseMatrix<T>::Output()
{
T **tempArray=new T*[rows];
for (int i1=0;i1<rows;i1++)
{
tempArray[i1]=new int[cols];
}
for (int j=0;j<rows;j++)
{
for (int k=0;k<cols;k++)
{
tempArray[j][k]=0;
}
}
for (int i=0;i<terms;i++)
{
tempArray[data[i].row-1][data[i].col-1]=data[i].val;
}
for (int j=0;j<rows;j++)
{
for (int k=0;k<cols;k++)
{
cout<<setw(4)<<tempArray[j][k];
}
cout<<endl;
}
for (int l=0;l<rows;l++)
{
delete[] tempArray[l];
}
delete tempArray;
cout<<endl;
}
template<class T>
bool SparseMatrix<T>::AddTo(const SparseMatrix& B)
{
if (rows!=B.rows||cols!=B.cols)
{
return false;
}
bool flag=false;
int tempTerms=terms;
for (int i=0;i<B.terms;i++)
{
flag=false;
for (int j=0;j<tempTerms;j++)
{
if (data[j].col==B.data[i].col&&data[j].row==B.data[i].row)
{
data[j].val+=B.data[i].val;
flag=true;
}
}
if (flag==false)
{
data[++terms-1].col=B.data[i].col; //數組下標操作注意事項
data[terms-1].row=B.data[i].row;
data[terms-1].val=B.data[i].val;
}
}
return true;
}
int main()
{
cout<<"此程序演示稀疏矩陣的普通轉置和快速轉置操作"<<endl;
SparseMatrix<int> sm(50);
SparseMatrix<int> sm1(50);
SparseMatrix<int> sm2(50);
sm.Input();
cout<<"sm is"<<endl;
sm.Output();
sm.TransposeTo(sm1);
cout<<"Transposition of sm is "<<endl;
sm1.Output();
sm.TransposeTo_Faster(sm2);
cout<<"Transposition of sm is "<<endl;
sm2.Output();
SparseMatrix<int> sm3;
cout<<"input a new matrix"<<endl;
sm3.Input();
cout<<"sm3 is"<<endl;
sm3.Output();
if(sm.AddTo(sm3))
{
cout<<"after adding sm3 ,sm is"<<endl;
sm.Output();
}
else
cout<<"the two matrix can't add"<<endl;
cout<<"Good job!"<<endl;
system("pause");
return 0;
}

2. 數據結構課程設計:十字鏈表的應用

交談中請勿輕信匯款、中獎消息,勿輕易撥打陌生電話。

-vE丨 12:40:17
#ifndef Matrix_H

#define Matrix_H

#include "List.h"

class MatNode

{

public:

int data;

int row, col;

union { Node<MatNode> *down; List<MatNode> *downrow; };

MatNode(int value = 0, Node<MatNode> *p = NULL, int i = 0, int j = 0)

: data(value), down(p), row(i), col(j) {}

friend ostream & operator << (ostream & strm, MatNode &mtn)

{

strm << '(' << mtn.row << ',' << mtn.col << ')' << mtn.data;

return strm;

}

};

class Matrix : List<MatNode>

{

public:

Matrix() : row(0), col(0), num(0) {}

Matrix(int row, int col, int num) : row(row), col(col), num(num) {}

~Matrix() { MakeEmpty(); }

void MakeEmpty()

{

3. C++十字鏈表實現兩個矩陣的乘法、加法和轉置

這個建議樓主自己看書,慢慢的編一下

4. 用C語言實現矩陣轉置

//Transpose
#include <stdio.h>
#define MAX 20
int m,n;
void transpose(double a[][MAX],double b[][MAX])
{
int i,j;
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
b[i][j]=a[j][i];
}

void main()
{
int i,j;
double a[MAX][MAX],b[MAX][MAX];
puts("Please input the dimensions of the matrixe:");
puts("(in term of 「2 3」).");
scanf("%d %d",&m,&n);
puts("Enter the matrix:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%lf",&a[i][j]);
transpose(a,b);

puts("The Transpose as follow:");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(b[i][j]-int(b[i][j])!=0)
printf("%lf ",b[i][j]);
else
printf("%d ",int(b[i][j]));
}
puts("");
}
}
//我這個能實現任意大小的,還有提示輸入輸出

5. c語言 矩陣轉置的編寫

#include "stdio.h"
void main()
{
void zhuan(int array[2][3],int b[3][2]);
int array[2][3],b[3][2]; int i,j;
printf("input:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
scanf("%d",&array[i][j]);
}
printf("\n");
zhuan(array,b);
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf("%d",b[i][j]);
printf("\n");
}
}
void zhuan(int array[2][3],int b[3][2]) //轉置函數邏輯錯誤,沒那麼麻煩,直接轉就是了!
{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
b[j][i]=array[i][j];
}
}

6. 十字鏈表表示稀疏矩陣,並求矩陣的加法,減法,乘法,運算要求用C語言

十字鏈的原理很簡單啊。實現也比較簡單。i,here, give you the defination of the node.and you can build a cross_linkList by yourself or you can take a look at what the above writing.
定義一個表頭結點數據類型,實現的時候定義一個數組即可。
typedef struct node{
int vex;//頂點
struct *node *first;//指向第一個與其有聯系的結點
}ListNode;

然後再定義一個結點的類型,
typedef struct node{
int vexNum;//頂點編號
int vexData;//頂點數據
struct *node *next;//指向其它與表頭結點有聯系的結點
}Node;
矩陣的加法是對應項相加,那麼你只需要把用十字鏈表示的兩個矩陣中,對應項相加即可。具體來說,對每個頂點,在表頭表中查找,然後再查找與其有聯系的結點。指針後移,比較兩個十字鏈表中是否存在兩個相同的結點,有,則相加,將結果保存到其中一個十字鏈表中。否則,不變。依次查找其他的頂點。就可以得到結果。
ListNode head1,head2;
Node *p,*q;
p=head1->frist;
q=head2->first;

while(!p)
{
while(!q)
{
if(p->vexNum==q->vexNum)
{
p->vexData+=q->vexData;//put the result into the first cross_linkList;
break;//
}
q=q->next;
}
p=p->next;
}

the implement of adding is just like what i writing above.
And the others are similar .you can do it by yourself.trust youself.

7. c++ 十字鏈表實現稀疏矩陣的轉置

mplate <class Type>SparseMatrix<Type>
SparseMatrix<Type>::SparseMatrix::FastTranspose()
{
int *RowSize = new int[Cols]; // 統計各列非零元素個數
int *RowStart = new int[Cols]; // 預計轉置後各行存放位置
SparseMatrix b; // 存放轉置結果
b.Rows = cols; b.cols = Rows; b.Terms = Terms;
if (Terms>0) //nonzero matrix
{
for (int i=0;i<Cols;i++) RowSize[i] = 0; //initialize
for (i=0;i<terms;i++) RowSize[smArray[i].col]++;
//RowStart[i]=starting position of row i in b
RowStart[0]=0;
for (i=1;i<Cols;i++) RowStart[i] = RowStart[i-1]+
RowSize[i-1];
for (i=0;i<Terms;i++) //move from a to b
{
int j=Rowstart[smArray[i].col];
b.smArray[j].row=smArray[i].col;
b.smArray[j].col= smArray[i].row;
b.smArray[j].value= smArray[i].value;
RowStart[smArray[i].col]++;
} //end of for
}
delete [ ]Rowsize;
delete [ ]Rowstart;
return b;
}
另外,站長團上有產品團購,便宜有保證

8. 用十字鏈表存儲結稀疏矩陣,並進行矩陣的轉置。 要C的,c++的我看不懂。。。。~

這個吧 功能比你要的還多:http://wenku..com/view/3d744dd950e2524de5187e68.html
建立稀疏矩陣A 的十字鏈表首先輸入的信息是:m(A 的行數),n(A 的列數),r(非零項的數目),緊跟著輸入的是r 個形如(i,j,aij)的三元組。

演算法的設計思想是:首先建立每行(每列)只有頭結點的空鏈表,並建立起這些頭結點拉成的循環鏈表;然後每輸入一個三元組(i,j,aij),則將其結點按其列號的大小插入到第i 個行鏈表中去,同時也按其行號的大小將該結點插入到第j 個列鏈表中去。在演算法中將利用一個輔助數組MNode *hd[s+1]; 其中s=max(m , n) , hd [i]指向第i 行(第i 列)鏈表的頭結點。這樣做可以在建立鏈表時隨機的訪問任何一行(列),為建表帶來方便。

演算法如下:
MLink CreatMLink( ) /* 返回十字鏈表的頭指針*/

MLink H;
Mnode *p,*q,*hd[s+1];
int i,j,m,n,t;
datatype v;
scanf(「%d,%,%d」,&m,&n,&t);
H=malloc(sizeof(MNode)); /*申請總頭結點*/
H->row=m; H->col=n;
hd[0]=H;
for(i=1; i<S; i++)
{ p=malloc(sizeof(MNode)); /*申請第i 個頭結點*/
p->row=0; p->col=0;
p->right=p; p->down=p;
hd[i]=p;
hd[i-1]->v_next.next=p;
}
hd[S]->v_next.next=H; /*將頭結點們形成循環鏈表*/
for (k=1;k<=t;k++)
{ scanf (「%d,%d,%d」,&i,&j,&v); /*輸入一個三元組,設值為int*/
p=malloc(sizeof(MNode));
p->row=i ; p->col=j; p->v_next.v=v
/*以下是將*p 插入到第i 行鏈表中去,且按列號有序*/
q=hd[i];
while ( q->right!=hd[i] && (q->right->col)<j ) /*按列號找位置*/
q=q->right;
p->right=q->right; /*插入*/
q->right=p;
/*以下是將*p 插入到第j 行鏈表中去,且按行號有序*/
q=hd[i];
while ( q->down!=hd[j] && (q->down->row)<i ) /*按行號找位置*/
q=q->down;
p-> down =q-> down; /*插入*/
q-> down =p;
} /*for k*/
return H;
} /* CreatMLink */

9. C語言 ,求轉置矩陣

根據數學定義可以知道,對於矩陣P,其第m行n列上的元素,是其轉置矩陣的n行m列元素。

從此可以得出程序如下:

#defineM10
#defineN5
//以10行5列為例,可以任意修改。
voidconvert(inta[M][N],intb[N][M])//求a的轉置矩陣,結果存於b中。
{
inti,j;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
b[j][i]=a[i][j];//轉置運算。
}

10. 用C語言編寫一個矩陣轉置的函數,矩陣的行數和列數在程序中由用戶輸入,請問怎麼寫,非常感謝

我的代碼邏輯是:

矩陣行指針初值指向每行首地址,迭代依次取所有行指針指向值組成新行,所有行指針自增。最終組合新的矩陣。

#include<stdio.h>
#include<malloc.h>
int**getList(introw,intclo);//獲取矩陣地址空間
voidsetNum(int**nList,intn);//填寫數值
voidprtList(int**nList,introw,intclo);//列印矩陣
int**zz(int**nList,introw,intclo);//轉置函數

intmain()
{
introw,clo,**nList=NULL,**nListSave=NULL;

printf("輸入矩陣行列數:");
scanf("%d%d",&row,&clo);
nList=getList(row,clo);
setNum(nList,row*clo);

printf("輸入的矩陣為: ");
prtList(nList,row,clo);

printf("轉置後的矩陣為: ");
nListSave=zz(nList,row,clo);
free(nList);
nList=nListSave;
prtList(nList,clo,row);
return0;
}

int**zz(int**nList,introw,intclo)
{
int*nSave=NULL,**listSave=NULL,**listp=nList,*p=NULL,i,j;
nSave=(int*)malloc(sizeof(int)*row*clo);
listSave=(int**)malloc(sizeof(int*)*clo);//倒置後的矩陣
p=nSave;

for(j=0;j<clo;j++)
{
for(i=0;i<row;i++)
{
*p++=*listp[i];
listp[i]=listp[i]+1;
}
}
for(i=0;i<clo;i++)
listSave[i]=&nSave[i*row];

for(i=0;i<row;i++)
free(nList[i]);//釋放原矩陣行空間
returnlistSave;
}
voidprtList(int**nList,introw,intclo)
{
inti,j;
for(i=0;i<row;i++)
{
for(j=0;j<clo;j++)
printf("%d",nList[i][j]);
printf(" ");
}
}
voidsetNum(int**nList,intn)
{
int*p=nList[0];
printf("填寫矩陣中%d個數值: ",n);
while(n-->0)
scanf("%d",p++);
}
int**getList(introw,intclo)
{
int*nums,**nList,i;
nums=(int*)malloc(sizeof(int)*row*clo);
nList=(int**)malloc(sizeof(int*)*row);
for(i=0;i<row;i++)
nList[i]=&nums[i*clo];
returnnList;
}