#include <stdio.h>
#include <stdlib.h>
#define MAX_DEGREE 20
#define MAX(a,b) ((a>b)? a:b)
typedef struct SparseMatrixTerm {
int row;
int col;
int val;
}sparse_term;

typedef struct SparseMatrix {
int NOT;
int row_degree;
int col_degree;
sparse_term item[MAX_DEGREE];
}sparse_matrix;

sparse_matrix* createSparseMatrix(int N, int R, int C, sparse_term item[]) {
sparse_matrix* SM = (sparse_matrix*)malloc(sizeof(sparse_matrix));
if (SM != NULL) {
SM->NOT = N; SM->row_degree = R; SM->col_degree = C;
for (int i = 0; i < SM->NOT; i++) {
SM->item[i].row = item[i].row;
SM->item[i].col = item[i].col;
SM->item[i].val = item[i].val;
}
}

return SM;
}
void printSparseMatrix(sparse_matrix* SM) {
int SM_ind = 0;

printf("================== ");

for (int r = 0; r < SM->row_degree; r++) {
printf("|");
for (int c = 0; c < SM->col_degree; c++) {
if (SM->item[SM_ind].row == r && SM->item[SM_ind].col == c) {
printf("%d|", SM->item[SM_ind].val);
SM_ind++;
}
else
printf("0|");
}
printf(" ");
}
printf("================== ");
}
sparse_matrix* addSparseMatrix(sparse_matrix* sparse1, sparse_matrix* sparse2) {
sparse_matrix* sparse3 = (sparse_matrix*)malloc(sizeof(sparse_matrix));
if (sparse3 != NULL) {
sparse3->NOT = 13; sparse3->row_degree = 8; sparse3->col_degree = 7;
sparse3->item[0].row = 0, sparse3->item[0].col = 2, sparse3->item[0].val = 25;
//
}
return sparse3;
}
int main(void)
{
sparse_term itemA[] = { {0,2,2}, {0,6,12}, {1,4,7}, {2,0,23}, {3,3,31},
{4,1,14}, {4,5,25}, {5,6,6}, {6,0,52}, {7,4,11} };
sparse_matrix* SA = createSparseMatrix(10, 8, 7, itemA); printSparseMatrix(SA);
sparse_term itemB[] = { {0,2,23}, {0,6,52}, {1,4,14}, {2,0,2}, {3,3,31},
{4,1,7}, {4,7,11}, {5,4,26}, {6,0,12}, {6,5,6} };
sparse_matrix* SB = createSparseMatrix(10, 8, 7, itemB); printSparseMatrix(SB);
sparse_matrix* SC = addSparseMatrix(SA, SB); printSparseMatrix(SC);

return 1;
}

희소 행렬을 선형리스트로 출력하고 덧셈한 것도 출력하는 코드인데
덧셈 함수는 아직 작성 안했구 문제는
배열의 크기를 메인 함수에서 처럼 8, 7 로 변경하면 안되고
itemB에서 { 4, 7, 11 }부터 씹혀서 출력이 제대로 안나오는데 좀 알려주세요 ㅜㅜ
4, 7, 11이 왜 출력안되는 지는 알겠는데 뒤부터가 출력이 안돼서요