如何用C语言将链表存储的稀疏矩阵以表格形式输出?
Solution: Print Sparse Matrix as Full Table with Zeros
Got it, let's adjust your code so it outputs the matrix in a proper table format (including zeros) instead of just listing non-zero elements. Here's the step-by-step fix and modified code:
First, we need two key things:
- Find the maximum column index used in the matrix so we know how many columns to print
- For each row, iterate through every column from 0 to max column, printing the stored value if it exists, otherwise 0
Modified Full Code
#include <stdio.h> #include <stdlib.h> typedef struct node { int column; int value; int row; struct node *next; } element; void Init(element *x[]) { int i; for (i = 0; i < 3; i++) x[i] = NULL; } // Helper function to find the maximum column index in the matrix int getMaxColumn(element *x[]) { int maxCol = -1; for (int i = 0; i < 3; i++) { element *temp = x[i]; while (temp != NULL) { if (temp->column > maxCol) { maxCol = temp->column; } temp = temp->next; } } // If matrix is empty, return 0 to avoid negative column count return maxCol == -1 ? 0 : maxCol; } void Insert(element *x[], int row, int column, int value) { int r = row; element *p; element *new = malloc(sizeof(element)); new->row = row; new->column = column; new->value = value; if (x[r] == NULL) { x[r] = new; new->next = NULL; } else { p = x[r]; if (new->column < p->column) { new->next = p; x[r] = new; } else if (new->column > p->column) { while (p->next != NULL && p->next->column < new->column) { p = p->next; } new->next = p->next; p->next = new; } else printf("An element already exists there!!\n"); } } void Printout(element *x[]) { int maxCol = getMaxColumn(x); int isEmpty = 1; // Check if matrix is empty first for (int i = 0; i < 3; i++) { if (x[i] != NULL) { isEmpty = 0; break; } } if (isEmpty) { printf("This matrix is empty!!\n"); return; } // Print each row as a table line for (int i = 0; i < 3; i++) { element *temp = x[i]; for (int j = 0; j <= maxCol; j++) { // If current column has a value, print it and move to next node if (temp != NULL && temp->column == j) { printf("%d ", temp->value); temp = temp->next; } else { printf("0 "); // Print zero for empty columns } } printf("\n"); } } int main(int argc, const char * argv[]) { int choice, column, row, value, number; element *a[3], *b[3], *sum[3]; Init(a); Init(b); Init(sum); do { printf("\n***\tADDING SPARSE MATRICES\t***\n"); printf("\n 1.) Insert in A"); printf("\n 2.) Insert in B"); printf("\n 3.) Printout both"); printf("\n 0.) EXIT"); printf("\nChoose ---------> "); scanf("%d", &choice); switch (choice) { case 1: /*Insert in A */ do { printf("Enter row -> "); scanf("%d", &row); } while (row < 0 || row > 2); // Fixed: rows are 0-2 (since a[3] is 3 elements) do { printf("Enter column -> "); scanf("%d", &column); } while (column < 0); printf("Enter value -> "); scanf("%d", &value); Insert(a, row, column, value); break; case 2: /*Insert in B */ do { printf("Enter row -> "); scanf("%d", &row); } while (row < 0 || row > 2); do { printf("Enter column -> "); scanf("%d", &column); } while (column < 0); printf("Enter value -> "); scanf("%d", &value); Insert(b, row, column, value); break; case 3: /* Printout A & B */ printf("\n::::::: MATRIX A :> \n\n"); Printout(a); printf("\n::::::: MATRIX B :> \n\n"); Printout(b); break; default: printf("\nWRONG CHOICE"); } } while (choice != 0); return 0; }
Key Changes Explained
getMaxColumnFunction: This scans all nodes in the matrix to find the highest column index used. This tells us how many columns we need to print for each row.- Revised
PrintoutFunction:- First checks if the matrix is empty (same as your original logic)
- For each row, loops through every column from 0 to the max column. For each column:
- If there's a node in the current row with this column index, print its value and move to the next node
- If not, print
0
- Fixed Row Input Validation: In your original code, inserting into matrix A allowed rows up to 3, but your array only has indices 0-2 (since
element *a[3]creates 3 rows). This was a potential crash bug, so I fixed the condition torow > 2.
Example Output
If you insert these elements into Matrix A:
- Row 0, Column 1, Value 3
- Row 1, Column 1, Value 5
- Row 2, Column 0, Value 7; Row 2, Column 1, Value 8; Row 2, Column 2, Value 9
The output will look like:
::::::: MATRIX A :> 0 3 0 0 5 0 7 8 9
内容的提问来源于stack exchange,提问作者RogerSK
相关产品推荐
相关产品推荐

