SM Array Implementation #include #define MAX 100 struct SMatrix{ int r; int c; int val; }; int i,j; int createSM(int mat[MAX][MAX], int r, int c, struct SMatrix sm[MAX]) { int k=0,smr=0; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { if (mat[i][j] != 0) { sm[k].r = i; sm[k].c = j; sm[k].val = mat[i][j]; k++; smr++; } } } return(smr); } /*void scanmat(int mat[][], int r,int c) { printf("Enter the elements of the matrix: "); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { scanf("%d", &mat[i][j]); } } }*/ void printSM(struct SMatrix sm[MAX], int r) { for ( i = 0; i < r; i++) { printf("%d\t%d\t%d ", sm[i].r,sm[i].c,sm[i].val); } } void printmat(int mat[MAX][MAX], int r, int c) { for ( i = 0; i < r; i++) { for ( j = 0; j < c; j++) { printf("%d\t", mat[i][j]); } printf(" "); } } int main() { int r=6, c=10,smr; struct SMatrix sm[MAX]; int mat[MAX][MAX]={{0,1,0,0,0,2,0,0,0,0},{0,1,0,3,0,2,0,8,0,0},{0,1,0,0,4,2,0,7,0,0}, {3,6,0,6,0,2,0,0,9,0},{4,0,3,0,2,0,7,0,1,0},{0,9,0,0,4,2,0,7,5,0}}; //scanmat(mat,r,c); printmat(mat,r,c); smr=createSM(mat, r, c, sm); printf(" Sparse Matrix Representation: "); printSM(sm,smr); return 0; }
@rightclick807 күн бұрын
SM Linked List Implementation #include #define MAX 100 struct node { int c,v; struct node * link; }; struct node *X[MAX], *t, *p; int i,j,smr; void createSM(int mat[MAX][MAX], int r, int c) { for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { if (mat[i][j]!= 0) { p= malloc(sizeof(struct node)); p->c = j; p->v = mat[i][j]; p-> link = NULL; if(X[i] == NULL) X[i] = p; else { t = X[i]; while(t->link!= NULL){ t=t->link; } t-> link =p; } } } } } /*void scanmat(int mat[][], int r,int c) { printf("Enter the elements of the matrix: "); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { scanf("%d", &mat[i][j]); } } } */ void printSM(int r) { for ( i = 0; i < r; i++) { t = X[i]; // printf("Row\tColumn\tValue"); while(t!= NULL){ printf("%d\t%d\t%d ----> ", i, t->c, t->v); t=t->link; }printf(" "); } } void printmat(int mat[MAX][MAX], int r, int c) { for ( i = 0; i < r; i++) { for ( j = 0; j < c; j++) { printf("%d ", mat[i][j]); } printf(" "); } } int main() { int r=6, c=10; //struct SMatrix sm[MAX]; int mat[MAX][MAX]={{0,1,0,0,0,2,0,0,0,0},{0,1,0,3,0,2,0,8,0,0},{0,1,0,0,4,2,0,7,0,0},{3,6,0,6,0,2,0,0,9,0},{4,0,3,0,2,0,7,0,1,0},{0,9,0,0,4,2,0,7,5,0}}; //scanmat(mat,r,c); printmat(mat,r,c); createSM(mat, r, c); printf(" Sparse Matrix LL Representation: "); printSM(r); return 0; }