资源描述:
《数据结构三元组转置C语言实现(普通 快速)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、普通转置#include#include#include#defineMAXSIZE100typedefstruct{inti,j;inte;}Triple;typedefstruct{Tripledata[MAXSIZE+1];intmu,nu,tu;}TSMatrix;voidmain(){TSMatrix*TransposeSMatrix(TSMatrix*M,TSMatrix*T);TSMatrix*M,*T;inti,j,e,a;M=(TSMatrix*)malloc(sizeof(TSMatrix));T=(TSMatrix
2、*)malloc(sizeof(TSMatrix));printf("请输入非零元个数");scanf("%d",&M->tu);printf("请输入行数");scanf("%d",&M->mu);printf("请输入列数");scanf("%d",&M->nu);printf("请依次输入元素下标(先行后列),元素值");for(a=1;a<=M->tu;a++){scanf("%d",&i);scanf("%d",&j);scanf("%d",&e);M->data[a].i=i;M->data[a].j=j;M->data[a].e=e;}printf("三元组为
3、n");for(a=1;a<=M->tu;a++){printf("%d%d%d",M->data[a].i,M->data[a].j,M->data[a].e);}printf("数组为");a=1;for(i=1;i<=M->mu;i++){for(j=1;j<=M->nu;j++){if((i==M->data[a].i)&&(j==M->data[a].j)){printf("%d",M->data[a].e);a++;}elseprintf("0");}printf("");}M=TransposeSMatrix(M,T);printf("转置(普通)后三元组为")
4、;for(a=1;a<=M->tu;a++){printf("%d%d%d",M->data[a].i,M->data[a].j,M->data[a].e);}printf("转置(普通)数组为");a=1;for(i=1;i<=M->mu;i++){for(j=1;j<=M->nu;j++){if((i==M->data[a].i)&&(j==M->data[a].j)){printf("%d",M->data[a].e);a++;}elseprintf("0");}printf("");}}TSMatrix*TransposeSMatrix(TSMatrix*M,TSMatr
5、ix*T){//求稀疏矩阵M的转置矩阵T(普通)intp,q,col;T->mu=M->nu;T->nu=M->mu;T->tu=M->tu;if(T->tu){q=1;for(col=1;col<=M->nu;++col)for(p=1;p<=M->tu;++p)if(M->data[p].j==col){T->data[q].i=M->data[p].j;T->data[q].j=M->data[p].i;T->data[q].e=M->data[p].e;++q;}}returnT;}快速转置#include#include#include6、io.h>#defineMAXSIZE100typedefstruct{inti,j;inte;}Triple;typedefstruct{Tripledata[MAXSIZE+1];intmu,nu,tu;}TSMatrix;voidmain(){TSMatrix*FastTransposeSMatrix(TSMatrix*M,TSMatrix*T);TSMatrix*M,*T;inti,j,e,a;M=(TSMatrix*)malloc(sizeof(TSMatrix));T=(TSMatrix*)malloc(sizeof(TSMatrix));printf("请输入非零元个数")
7、;scanf("%d",&M->tu);printf("请输入行数");scanf("%d",&M->mu);printf("请输入列数");scanf("%d",&M->nu);printf("请依次输入元素下标(先行后列),元素值");for(a=1;a<=M->tu;a++){scanf("%d",&i);scanf("%d",&j);scanf("%d",&e);M->data[a].i=