【发布时间】:2015-01-17 14:03:36
【问题描述】:
我有一个任务是创建两个稀疏矩阵(A 和 B),然后将它们添加到第三个稀疏矩阵(C)中。我已经用向量完成了它(接收两个稀疏向量并将它们添加到第三个)所以我决定使用一系列向量来表示矩阵会更容易。将来,我可能需要在没有向量但使用实际矩阵的情况下执行此操作(我什至有一个将数字插入稀疏矩阵的函数的伪代码),所以请理解 为什么 我'我问这一切,即使下面的代码有点工作。
因为它是一个矩阵,所以它有两个指针数组,分别是行数组和列数组。数组的每个单元都指向各自的行/列。如下图:
作为一个稀疏矩阵,其中有零,您会看到这些箭头,因为那些带有零的节点不存在。
为了简化问题,我更喜欢只处理行数组,如图所示,这些行指向每个新行的头部。假设每一行都是一个向量C(就像我上面说的,我想用一系列向量来表示矩阵)。
这是我构建的所有链表函数的代码:
typedef struct List_node
{
int key;
int i,j;
struct List_node *next;
}List_node;
typedef struct List
{
List_node *head;
}List;
List *create_list()
{
List *list = (List*)malloc(sizeof(List));
list->head = NULL;
return list;
}
void insert_first(List_node *x, List *list)
{
x->next = list->head;
list->head = x;
}
void insert(List_node *x, List_node *y)
{
x->next = y->next;
y->next = x;
}
List_node *mk_node(int data, int row, int col)
{
List_node *node = (List_node *)malloc(sizeof(List_node));
node->key = data;
node->i = row + 1;
node->j = col + 1;
return node;
}
void delete_list(List *list)
{
List_node *node, *temp;
node = list->head;
while (node != NULL)
{
temp = node->next;
free(node);
node = temp;
}
free(list);
}
void print_list(List *list, char name)
{
List_node *p;
p = list->head;
printf("\nThe linked list %c consists of: ",name);
while (p != NULL)
{
printf("%d(i = %d) (j = %d) ", p->key, p->i, p->j);
p = p->next;
}
printf("\n");
}
这是创建向量的代码:
List *Create_vector (List *A, List *B, int i, int m)
{
int l,flag = 0;
List *C;
List_node *node=NULL, *next, *Pointer_A, *Pointer_B;
C = create_list();
Pointer_A = A->head;
Pointer_B = B->head;
for (l = 0; l<m; l++)
{
if ((Pointer_A->key + Pointer_B->key) != 0)
{
if (flag == 0)
{
node = mk_node(Pointer_A->key + Pointer_B->key, i,l);
insert_first(node, C);
flag = 1;
}
else
{
next = mk_node(Pointer_A->key + Pointer_B->key, i,l);
insert(next, node);
node = next;
}
}
Pointer_A = Pointer_A->next;
Pointer_B = Pointer_B->next;
}
return C;
}
这是主程序:
void main()
{
List_node *Row_A, *Row_B, *Row_C;
List *A, *B, *C;
List_node *node, *next;
int data, m,n,l;
printf("Enter list row\n");
scanf("%d", &m);
Row_A = (List_node*)malloc(sizeof(int)*(m));
Row_B = (List_node*)malloc(sizeof(int)*(m));
Row_C = (List_node*)malloc(sizeof(int)*(m));
printf("Enter list columns\n");
scanf("%d", &n);
for (int i=0; i<m; i++)
{
A = create_list();
B = create_list();
printf("\nInsert first number into A\n");
scanf("%d", &data);
node = mk_node(data, i,0);
insert_first(node, A);
for (l = 1; l<n; l++)
{
printf("Now insert the rest of the numbers\n");
scanf("%d", &data);
next = mk_node(data, i,l);
insert(next, node);
node = next;
}
print_list(A,'A');
printf("\nInsert first number into B\n");
scanf("%d", &data);
node = mk_node(data,i,0);
insert_first(node, B);
for (l = 1; l<n; l++)
{
printf("Now insert the rest of the numbers\n");
scanf("%d", &data);
next = mk_node(data, i,l);
insert(next, node);
node = next;
}
print_list(B,'B');
C = Create_vector(A,B,i,n);
}
getchar(); getchar();
}
希望你能走到这一步。所以我的问题是:
我不确定如何创建我需要的指针数组,它们是 Row_A、Row_B、Row_C。意思是,如果每个单元格都指向向量的头部 - 我如何定义数组?作为列表?列出节点?如何使数组的每个单元格都指向每个向量的头部?
每次执行 for 循环时都会覆盖列表 C。我看到可以保留每个向量 C 并将其放入矩阵的唯一方法是使每个数组单元指向每个向量的头部,这使我们回到问题 #1。真的吗?我该怎么做?
非常感谢。
【问题讨论】:
标签: c sparse-matrix