当前位置:首页 » 编程语言 » 十字链表转置矩阵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;
}