如何使用链接列表在C中表示稀疏矩阵(包括零元素)?
我想要一个代表稀疏矩阵的代码,包括使用链接列表的零元素,我基本上希望输出是这样的:
1 0 0 0
0 6 1 0
我已经有一个执行表示的代码,但仅作为输出作为输出,类似的非零元素:
输入:
2 0 0
0 0 0
0 0 0
输出:
行:1 Col:1 值:2
这是代码:
// C program for Sparse Matrix Representation
// using Linked Lists
#include<stdio.h>
#include<stdlib.h>
// Node to represent sparse matrix
struct Node
{
int value;
int row_position;
int column_postion;
struct Node *next;
};
// Function to create new node
void create_new_node(struct Node** start, int non_zero_element,
int row_index, int column_index )
{
struct Node *temp, *r;
temp = *start;
if (temp == NULL)
{
// Create new node dynamically
temp = (struct Node *) malloc (sizeof(struct Node));
temp->value = non_zero_element;
temp->row_position = row_index;
temp->column_postion = column_index;
temp->next = NULL;
*start = temp;
}
else
{
while (temp->next != NULL)
temp = temp->next;
// Create new node dynamically
r = (struct Node *) malloc (sizeof(struct Node));
r->value = non_zero_element;
r->row_position = row_index;
r->column_postion = column_index;
r->next = NULL;
temp->next = r;
}
}
// This function prints contents of linked list
// starting from start
void PrintList(struct Node* start)
{
struct Node *temp, *r, *s;
temp = r = s = start;
printf("row_position: ");
while(temp != NULL)
{
printf("%d ", temp->row_position);
temp = temp->next;
}
printf("\n");
printf("column_postion: ");
while(r != NULL)
{
printf("%d ", r->column_postion);
r = r->next;
}
printf("\n");
printf("Value: ");
while(s != NULL)
{
printf("%d ", s->value);
s = s->next;
}
printf("\n");
}
I want a code that represents a sparse matrix including zero elements using linked list, I basically want the output to be like this:
1 0 0 0
0 6 1 0
I already have a code that does the representation but it only gives as an output the non-Zero elements like so :
input :
2 0 0
0 0 0
0 0 0
output :
row : 1
col : 1
value : 2
Here is the code :
// C program for Sparse Matrix Representation
// using Linked Lists
#include<stdio.h>
#include<stdlib.h>
// Node to represent sparse matrix
struct Node
{
int value;
int row_position;
int column_postion;
struct Node *next;
};
// Function to create new node
void create_new_node(struct Node** start, int non_zero_element,
int row_index, int column_index )
{
struct Node *temp, *r;
temp = *start;
if (temp == NULL)
{
// Create new node dynamically
temp = (struct Node *) malloc (sizeof(struct Node));
temp->value = non_zero_element;
temp->row_position = row_index;
temp->column_postion = column_index;
temp->next = NULL;
*start = temp;
}
else
{
while (temp->next != NULL)
temp = temp->next;
// Create new node dynamically
r = (struct Node *) malloc (sizeof(struct Node));
r->value = non_zero_element;
r->row_position = row_index;
r->column_postion = column_index;
r->next = NULL;
temp->next = r;
}
}
// This function prints contents of linked list
// starting from start
void PrintList(struct Node* start)
{
struct Node *temp, *r, *s;
temp = r = s = start;
printf("row_position: ");
while(temp != NULL)
{
printf("%d ", temp->row_position);
temp = temp->next;
}
printf("\n");
printf("column_postion: ");
while(r != NULL)
{
printf("%d ", r->column_postion);
r = r->next;
}
printf("\n");
printf("Value: ");
while(s != NULL)
{
printf("%d ", s->value);
s = s->next;
}
printf("\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码不完整。
它可以/应该按行/列排序顺序插入新节点。但是,看来它只是附加到列表的尽头。这使得矩阵的有序打印困难。而且,它不允许[true]随机访问(例如,相同的节点多次更新一次)。
因此,我必须重构代码。
扫描列表并寻找按顺序排序的插入点不再是额外的时间。这是因为插入新节点必须检查已经存在的节点以使稀疏矩阵像真实矩阵一样工作的函数。
我包括了其他一些扫描/匹配功能,并创建了一个矩阵“ Pretty”打印功能。
可以重写打印代码,因为它可以多次调用
nodefind
函数。可以重写该打印,以循环浏览所有节点,然后将其打印在其正确的列中。该代码注释:
这是程序输出:
Your code was incomplete.
It could/should do the insertion of a new node in row/column sorted order. But, it appears it just appended to the end of the list. This makes the ordered printing of the matrix difficult. And, it does not allow for [true] random access (e.g. the same node is updated more than once).
So, I had to refactor the code.
It's no more extra time to scan the list and look for an insertion point that is in sorted order. That's because the function that inserts a new node must check for an already existing node to make the sparse matrix work like a real matrix.
I've included some other scan/match functions and created a matrix "pretty" print function.
The print code could be rewritten as it makes many calls to a
nodefind
function. The print could be rewritten to just loop through all nodes and print them in their correct columns.The code is annotated:
Here is the program output: