0

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;
}

}

2 Answers 2

1

That's how i did it

    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);
            }
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know your "table" implementation but I would recommend you to re-assign CurrB to table2.head before the for loop. And make prod 0 again. What does this code give as output? I searched for Sparse Matrix and multiplication on sparse matrices, and I guess you need more for loops. Computing i-j'th element of the result takes for loops too.

1 Comment

I edit my code. I have tried also with 3 loops but i don't get he results i want!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.