本文主要是介绍稀疏矩阵的三元组顺序表的C语言实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下面的程序中,用三元组顺便表实现两个相加矩阵的有bug,想了半天没想出解决办法。o(╯□╰)o
#include <stdio.h>
#include <stdlib.h>#ifndef OK
#define OK 1
#endif
#ifndef ERROR
#define ERROR 0
#endif#define MAXSIZE 12500typedef int Status;
typedef int ElemType;
typedef struct{int i, j;ElemType e;
}Triple;
typedef struct{Triple data[MAXSIZE + 1];int mu, nu, tu;
}TSMatrix;TSMatrix *CreateMatrix(TSMatrix *M); //create TSMatrix
Status InsertElem(TSMatrix *M, int row, int col, ElemType e);
Status PrintSMatrix(TSMatrix M); //output the element of M
TSMatrix *CopySMatrix(TSMatrix M, TSMatrix *T);//copy TSMatrix M to TSMatrix T
TSMatrix *AddSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M+N
TSMatrix *TransposeSMatrix(TSMatrix M, TSMatrix *T);
#if 0
void DestroyMatrix(TSMatrix *M); //destroy TSMatrix
Status SubSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M-N
Status MultSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q);//Q=M*N
#endifTSMatrix *CreateMatrix(TSMatrix *M)
{M = (TSMatrix *)malloc(sizeof(TSMatrix));if(!M){printf("alloc memory failed.\n");return ERROR;}printf("please input the column number:");scanf("%d", &M->mu);printf("please input the line number:");scanf("%d", &M->nu);M->tu = 0;return M;
}Status InsertElem(TSMatrix *M, int row, int col, ElemType e)
{int p, i, t;//check out the segmentif(M->tu >= MAXSIZE) return ERROR;if(row > M->nu || col > M->mu) return ERROR;p = 1;if(M->tu == 0){M->data[p].i = row; M->data[p].j = col; M->data[p].e = e;M->tu++;return OK;}p = M->tu+1;for(t = M->tu; t >= 1; t--){if((row <= M->data[t].i)&&(col <= M->data[t].j))p--;}if((row == M->data[p].i && col == M->data[p].j)){M->data[p].e = e;return OK; }for(i = M->tu; i >= p; i--){M->data[i+1].i = M->data[i].i; M->data[i+1].j = M->data[i].j; M->data[i+1].e = M->data[i].e; }M->data[p].i = row;M->data[p].j = col;M->data[p].e = e;M->tu++;return OK;
}//copy TSMatrix M to TSMatrix T
TSMatrix *CopySMatrix(TSMatrix M, TSMatrix *T)
{int i;T = (TSMatrix *)malloc(sizeof(TSMatrix));if(!T){printf("alloc memory failed.\n"); return ERROR;}T->mu = M.mu;T->nu = M.nu;T->tu = M.tu;for(i = 1; i <= M.tu; i++){T->data[i].i = M.data[i].i;T->data[i].j = M.data[i].j;T->data[i].e = M.data[i].e;}return T;
}TSMatrix *AddSMatrix(TSMatrix M, TSMatrix N, TSMatrix *Q)
{int i, j, p; int row, col, data;Q = (TSMatrix *)malloc(sizeof(TSMatrix));if(!Q){printf("alloc memory for Q failed.\n"); return ERROR;}Q->mu = M.mu;Q->nu = M.nu;Q->tu = 0;p = 1;for(i = 1; i <= M.tu; i++){Q->data[p].i = M.data[i].i;Q->data[p].j = M.data[i].j; Q->data[p].e = M.data[i].e; Q->tu++;for(j = 1; j <= N.tu; j++){if((M.data[i].i == N.data[j].i)&&(M.data[i].j == N.data[j].j)){Q->data[p].e += N.data[j].e; } }p++;}return Q;
}TSMatrix *TransposeSMatrix(TSMatrix M, TSMatrix *T)
{int q, col, p;T = (TSMatrix *)malloc(sizeof(TSMatrix));if(!T){printf("alloc memory failed.\n"); return ERROR;}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;}}return T;
}Status PrintSMatrix(TSMatrix M)
{int i = 1;while(i <= M.tu){printf("%d ", M.data[i].e);i++;}putchar('\n');return OK;
}int
main(void)
{TSMatrix *M, *T, *Q, *X;M = CreateMatrix(M);InsertElem(M, 2, 3, 4);InsertElem(M, 3, 4, 2);InsertElem(M, 3, 3, 1);InsertElem(M, 3, 2, 1);InsertElem(M, 4, 4, 3);printf("M->mu = %d.\n", M->mu);printf("M->nu = %d.\n", M->nu);printf("M->tu = %d.\n", M->tu);PrintSMatrix(*M);T = CopySMatrix(*M, T);PrintSMatrix(*T);InsertElem(T, 4, 3, 3);Q = AddSMatrix(*M, *T, Q);PrintSMatrix(*Q);X = TransposeSMatrix(*M, X);PrintSMatrix(*X);return 0;
}
这篇关于稀疏矩阵的三元组顺序表的C语言实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!