C语言中使用链表的稀疏矩阵乘法
我有两个非零数的链接列表,我尝试将两个链接列表相乘,结果存储在第三个链接列表中。不将链接列表转换为数组。我在另一个函数中创建的两个列表。在我创建的列表中,所有的行和列都是这样的:如果表有两行两列,并且第一个节点位于1行0列的位置,那么下一个节点不能位于0行1列。我知道循环是错误的。任何帮助,因为我真的坚持这一点C语言中使用链表的稀疏矩阵乘法,c,C,我有两个非零数的链接列表,我尝试将两个链接列表相乘,结果存储在第三个链接列表中。不将链接列表转换为数组。我在另一个函数中创建的两个列表。在我创建的列表中,所有的行和列都是这样的:如果表有两行两列,并且第一个节点位于1行0列的位置,那么下一个节点不能位于0行1列。我知道循环是错误的。任何帮助,因为我真的坚持这一点 typedef struct node { int row; int column;
typedef struct node
{
int row;
int column;
float value;
struct node * next;
} node;
typedef struct table
{
node * head;
int number_of_rows;
int number_of_columns;
} table;
void create_new_node( node* start, table * input, float non_zero_element,
int row_index, int column_index ) {
node *temp, *r;
start = input->head;
temp = start;
if (temp == NULL) {
temp = (node *) malloc(sizeof(node));
temp->value = non_zero_element;
temp->row = row_index;
temp->column = column_index;
temp->next = NULL;
input->head = temp;
} else {
while (temp->next != NULL) {
temp = temp->next;
}
r = (node *) malloc(sizeof(node));
r->value = non_zero_element;
r->row = row_index;
r->column = column_index;
r->next = NULL;
temp->next = r;
}
}
void Multables(table table1, table table2, table * table3) {
node *currA = table1.head;
node *currB;
table3->number_of_rows = table1.number_of_rows;
table3->number_of_columns = table2.number_of_columns;
table3->head = NULL;
node *currC = table3->head;
if (table1.number_of_columns != table2.number_of_rows) {
printf("the multiplication can't be done!!!\n");
return;
}
float prod;
for (int i = 0; i < table3.number_of_rows; i++) {
prod = 0;
for (int j = 0; j < table3.number_of_columns; j++) {
currB=table2.head;
for (int k = 0; k < table3.number_of_rows; k++) {
printf("currA=%f ",currA->value);
printf("currB=%f ",currB->value);
if (currA->row == i && (currA->column == k && currB->row == k)) {
prod += currA->value * currB->value;
printf(" prod=%f ",prod);
}
currB = currB->next;
}
if (prod != 0) {
create_new_node(currC, table3, prod, i, j);
}
}
currA = currA->next;
}
typedef结构节点
{
int行;
int列;
浮动值;
结构节点*下一步;
}节点;
typedef结构表
{
节点*头;
整数行数;
整列的整数;
}表;
void create_new_节点(节点*开始,表*输入,浮点非零元素,
int行索引,int列索引){
节点*temp,*r;
开始=输入->头部;
温度=启动;
if(temp==NULL){
temp=(node*)malloc(sizeof(node));
温度->值=非零元素;
临时->行=行索引;
临时->列=列索引;
temp->next=NULL;
输入->压头=温度;
}否则{
while(临时->下一步!=NULL){
温度=温度->下一步;
}
r=(节点*)malloc(sizeof(节点));
r->value=非零元素;
r->row=行索引;
r->column=column\u索引;
r->next=NULL;
温度->下一步=r;
}
}
表格(表1、表2、表*表3){
节点*currA=表1.head;
节点*currB;
表3->行数=表1.行数;
表3->列数=表2.列数;
表3->head=NULL;
节点*currC=table3->head;
if(表1.列数!=表2.行数){
printf(“无法进行乘法!!!\n”);
返回;
}
浮子;
对于(int i=0;ivalue);
printf(“currB=%f”,currB->value);
如果(currA->row==i&&(currA->column==k&&currB->row==k)){
prod+=currA->value*currB->value;
printf(“prod=%f”,prod);
}
currB=currB->next;
}
如果(prod!=0){
创建新节点(currC,表3,prod,i,j);
}
}
currA=currA->next;
}
}我不知道您的“table”实现,但我建议您在for循环之前将CurrB重新分配给table2.head。并再次使prod为0。
这段代码的输出是什么?
我搜索了稀疏矩阵和稀疏矩阵上的乘法,我想你需要更多的循环。计算结果的i-j'元素也需要循环。我就是这样做的
float search(node* start, table input, int row_index, int column_index) {
start = input.head;
float zero = 0;
while(start != NULL) {
if(start->row == row_index && start->column == column_index) {
return start->value;
}
start = start->next;
}
return zero;
}
void Multables(table table1, table table2, table * table3) {
if (table1.number_of_columns != table2.number_of_rows) {
printf("the multiplication can'τ be done!!!\n");
return;
}
node *currA = table1.head;
node *currB = table2.head;
table3->number_of_rows = table1.number_of_rows;
table3->number_of_columns = table2.number_of_columns;
table3->head = NULL;
node *currC = table3->head;
float prod = 0, t1 = 0, t2 = 0;
for (int i = 0; i < table1.number_of_rows; i++) {
for (int j = 0; j < table2.number_of_columns; j++) {
prod = 0;
for (int k = 0; k < table1.number_of_columns; k++) {
t1 = search(currA,table1,i,k);
t2 = search(currB,table2,k,j);
prod += t1 * t2;
}
if (prod != 0) {
create_new_node(currC, table3, prod, i, j);
}
}
}
}
float搜索(节点*开始,表输入,int行索引,int列索引){
开始=输入.head;
浮点零=0;
while(开始!=NULL){
如果(开始->行==行索引和开始->列==列索引){
返回开始->值;
}
开始=开始->下一步;
}
返回零;
}
表格(表1、表2、表*表3){
if(表1.列数!=表2.行数){
printf(“乘法可以完成!!!\n”);
返回;
}
节点*currA=表1.head;
节点*currB=table2.head;
表3->行数=表1.行数;
表3->列数=表2.列数;
表3->head=NULL;
节点*currC=table3->head;
浮点数=0,t1=0,t2=0;
对于(int i=0;i
我编辑我的代码。我也尝试了3个循环,但我没有得到我想要的结果!