I have two link list with non-zero number and i try to multiply the two link list and the result store it in a third link list. Not convert the link list to an array. The two list i create them in another function. Also on the lists i’ve created all the row and columns are like: if the table has 2 rows and 2 column and the first node is in the place 1 row and 0 col, the next node cant be on 0 row and 1 col. I know that the loop is wrong. Any help because i really have stuck to this.
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;
}
}