稀疏矩阵的压缩存储(十字链表)

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 
  5 typedef struct node
  6 {
  7     int row;
  8     int col;
  9     int value;
 10     struct node* right;
 11     struct node* down;
 12 }Node;
 13 
 14 typedef struct
 15 {
 16     int rows;
 17     int cols;
 18     int nums;
 19     Node** rowhead;//二重指针指向一个指针数组(由若干个 Node 结构体类型的指针组成的指针数组)
 20     Node** colhead;
 21 }CrossList;
 22 
 23 void InitNode(Node* A);
 24 void InitCrossList(CrossList*  A);
 25 void CreateCrossList(CrossList* A);
 26 void PrintCrossList(CrossList A);
 27 
 28 int main(void)
 29 {
 30     CrossList A;
 31 
 32     InitCrossList(&A);
 33 
 34     CreateCrossList(&A);
 35 
 36     PrintCrossList(A);
 37 
 38     system("pause");
 39     return 0;
 40 }
 41 
 42 void InitNode(Node* A)
 43 {
 44     A->row = 0;
 45     A->col = 0;
 46     A->value = 0;
 47     A->right = NULL;
 48     A->down = NULL;
 49 }
 50 
 51 void InitCrossList(CrossList* A)
 52 {
 53     A->rows = 0;
 54     A->cols = 0;
 55     A->nums = 0;
 56     A->rowhead = NULL;
 57     A->colhead = NULL;
 58 }
 59 
 60 void CreateCrossList(CrossList* A)
 61 {
 62     int i;
 63     int j;
 64     int value = 0;
 65     Node* P = NULL;//插入结点
 66     Node* Q = NULL;// Q 是结点 P 的前置结点
 67 
 68     printf("请输入矩阵总行数、总列数(空格分隔)\n");
 69     scanf(" %d%*c%d", &A->rows, &A->cols);
 70 
 71     A->rowhead = (Node**)calloc(A->rows + 1, sizeof(Node*));//为二重指针分配指针数组的空间
 72     A->colhead = (Node**)calloc(A->cols + 1, sizeof(Node*));
 73 
 74     printf("请输入矩阵(空格分隔)\n");
 75     for (i = 1; i <= A->rows; i++)
 76     {
 77         for (j = 1; j <= A->cols; j++)
 78         {
 79             scanf(" %d", &value);// %d前有一空格读走缓存区的空白字符
 80             if (value)
 81             {//非零元素存入十字链表
 82                 P = (Node*)calloc(1, sizeof(Node));
 83                 InitNode(P);
 84                 P->row = i;
 85                 P->col = j;
 86                 P->value = value;
 87 
 88                 if (A->rowhead[i])
 89                 {//插入结点所在行非空
 90                     Q = A->rowhead[i];
 91                     while (Q->right != NULL && Q->col < j)
 92                     {
 93                         Q = Q->right;
 94                     }
 95                     P->right = Q->right;
 96                     Q->right = P;
 97                 }
 98                 else
 99                 {
100                     A->rowhead[i] = P;
101                 }
102 
103                 if (A->colhead[j])
104                 {//插入结点所在列非空
105                     Q = A->colhead[j];
106                     while (Q->down != NULL && Q->row < i)
107                     {
108                         Q = Q->down;
109                     }
110                     P->down = Q->down;
111                     Q->down = P;
112                 }
113                 else
114                 {
115                     A->colhead[j] = P;
116                 }
117 
118                 A->nums++;
119             }//非零元素
120         }//每列
121     }//每行
122 }
123 
124 void PrintCrossList(CrossList A)
125 {
126     int i;
127     int j;
128     for (i = 1; i <= A.rows; i++)
129     {
130         for (j = 1; j <= A.cols; j++)
131         {
132             if (A.rowhead[i] && A.rowhead[i]->col == j)
133             {//该行有非零元素
134                 printf("%2d", A.rowhead[i]->value);
135                 A.rowhead[i] = A.rowhead[i]->right;
136             }
137             else
138             {
139                 printf("%2d", 0);
140             }
141 
142             if (j != A.cols)
143             {
144                 printf("\t");
145             }
146         }//每列
147         if (i != A.rows)
148         {
149             printf("\n");
150         }
151     }//每行
152     printf("\n");
153 }

 

posted @ 2021-12-31 20:11  吕辉  阅读(183)  评论(0)    收藏  举报