c语言三元组存储转制,数据结构三元组转置C语言实现(普通 快速).pdf

  • Post author:
  • Post category:其他


普通转置 #include #include #include #define MAXSIZE 100 typedef struct{ int i,j; int e; }Triple; typedef struct{ Triple data[MAXSIZE+1]; int mu,nu,tu; }TSMatrix; void main() {TSMatrix *TransposeSMatrix(TSMatrix *M,TSMatrix *T); TSMatrix *M,*T; int i,j,e,a; M=(TSMatrix*)malloc(sizeof(TSMatrix)); T=(TSMatrix*)malloc(sizeof(TSMatrix)); printf(“请输入非零元个数\n”); scanf(“%d”, printf(“请输入行数\n”); scanf(“%d”, printf(“请输入列数\n”); scanf(“%d”, printf(“请依次输入元素下标(先行后列),元素值\n”); for(a=1;atu;a++) { scanf(“%d”, scanf(“%d”, scanf(“%d”, M->data[a].i=i; M->data[a].j=j; M->data[a].e=e; } printf(“三元组为\n”); for(a=1;atu;a++) { printf(“%d%d%d\n”,M->data[a].i,M->data[a].j,M->data[a].e); } printf(“数组为\n”); a=1; for(i=1;imu;i++) {for(j=1;jnu;j++) { if((i==M->data[a].i) a++; } else printf(“0”); } printf(“\n”); } M=TransposeSMatrix(M,T); printf(“转置(普通)后三元组为\n”); for(a=1;atu;a++) { printf(“%d%d%d\n”,M->data[a].i,M->data[a].j,M->data[a].e); } printf(“转置(普通)数组为\n”); a=1; for(i=1;imu;i++) {for(j=1;jnu;j++) { if((i==M->data[a].i) a++; } else printf(“0”); } printf(“\n”); } } TSMatrix *TransposeSMatrix(TSMatrix *M, TSMatrix *T) { // 求稀疏矩阵 M 的转置矩阵 T(普通) int p, q, col; T->mu = M->nu;T->nu = M->mu;T->tu = M->tu; if (T->tu) { q = 1; for (col=1; colnu; ++col) for (p=1; ptu; ++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; } } return T; } 快速转置 #include #include #include #define MAXSIZE 100 typedef struct{ int i,j; int e; }Triple; typedef struct{ Triple data[MAXSIZE+1]; int mu,nu,tu; }TSMatrix; void main() { TSMatrix *FastTransposeSMatrix(TSMatrix *M, TSMatrix *T); TSMatrix *M,*T; int i,j,e,a; M=(TSMatrix*)malloc(sizeof(TSMatrix)); T=(TSMatrix*)malloc(sizeof(TSMatrix)); printf(“请输入非零元个数\n”); scanf(“%d”, printf(“请输入行数\n”); scanf(“%d”, printf(“请输入列数\n”); scanf(“%d”, printf(“请依次输入元素下标(先行后列),元素值\n”); for(a=1;atu;a++) { scanf(“%d”, scanf(“%d”, scanf(“%d”, M->data[a].i=i; M->data[a].j=j; M->data[a].e=e; } printf(“三元组为\n”); for(a=1;atu;a++) { printf(“%d%d%d\n”,M->data[a].i,M->data[a].j,M->data[a].e); } printf(“数组为\n”); a=1; for(i=1;imu;i++) {for(j=1;jnu;j++) { if((i==M->data[a].i) a++; } else printf(“0”); } printf(“\n”); } M=FastTransposeSMatrix(M, T); printf(“转置(快速)后三元组为\n”); for(a=1;atu;a++) { printf(“%d%d%d\n”,M->data[a].i,M->data[a].j,M->data[a].e); } printf(“转置(快速)数组为\n”); a=1; for(i=1;imu;i++) {for(j=1;jnu;j++) { if((i==M->data[a].i) a++; } else printf(“0”); } printf(“\n”); } } TSMatrix *FastTransposeSMatrix(TSMatrix *M, TSMatrix *T) { // 求稀疏矩阵 M 的转置矩阵 T(快速) int col, t, p, q; int num[20], cpot[20]; T->mu=M->nu;T->nu=M->mu;T->tu=M->tu; if (T->tu) { for (col=1; colnu; ++col) num[col]=0; for (t=1; ttu; ++t) ++num[M->data[t].j]; cpot[1] = 1; for (col=2; colnu; ++col) cpot[col] = cpot[col-1]+num[col-1]; for (p=1; ptu; ++p) { col = M->data[p].j;q = cpot[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;++cpot[col]; } } return T; }

展开阅读全文