Thread: help for linked list(double)

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    1

    Unhappy help for linked list(double)

    I want to make a function to add two colum number of one row in a spreadsheet, it need to use two linked list. I have create the .h file.


    #include<stdio.h>
    #include<stdlib.h>

    #define LEN SparseMatrix;
    #define SUCCESS 1;
    #define FAILURE 0;

    typedef struct row_header{
    struct dataNode *rowhead;
    struct row_header *next;
    int rowNum;
    }SparseMatrix;

    typedef SparseMatrix *rowHeaderPtr;

    typedef struct data_node{
    struct dataNode *next;
    float data;
    int colNum;
    }dataNode;

    typedef dataNode *rowhead;
    /* Function photatype */

    float SparseMatrixRowSum(SparseMatrix, int, int, int);
    {
    /* But I have no idea how to define this function, it seems to need a double linked list, which hole a pointer to the list itself and another pointer point to another list head.

    }
    can any body give some example to deal with the problem?
    THanx!
    #include<stdio.h>

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #define LEN SparseMatrix;
    You will need to remove those ; to make this macros useful

    > /* Function photatype */
    Its prototype

    > float SparseMatrixRowSum(SparseMatrix, int, int, int);
    Its difficult to figure out how to call it, and figuring out how to write it (or suggest how) without knowing the names of these parameters

    Eg
    float SparseMatrixRowSum ( SparseMatrix matrix, int row, int foo, int bar);
    If you can add some names, maybe we can start on the next bit

  3. #3
    Unregistered
    Guest

    Unhappy

    Hi, thanx!
    float SparseMatrixRowSum(SparseMatrix *sp, int rowNumber, int column1, int column2);
    {
    /* But I have no idea how to define this function, it seems to need a double linked list, which hole a pointer to the list itself and another pointer point to another list head.

    }

    the parameter *sp is a pointer of sparseMatrix, which is point to the location where we need to compute, and the function sums up th data in the row given by the second argument, through the columns, through the columns specifed by 3rd and 4th arguments, it assumes all the non-existant vales as zeros for the computation. by the way the spreadsheet is something like:
    0 1 2 3 4 5 6 7 8 9 10...
    1 1 4 5
    2 3 6 6 8
    3 4
    4
    5
    like if I want to add row 2 number2 and 5, the answer should be 9; sparseMatrix is a linked list which each node hold the head node of another linked list.
    thanx for help!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A bit like this
    Code:
    float SparseMatrixRowSum(SparseMatrix *sp, int rowNumber, int column1, int column2) {
        float   result = 0.0;
        rowHeaderPtr    row_ptr;
        rowhead         col_ptr;
    
        // find the row
        // this walks the list of rows, looking for the row we want
        // stop when that row is found, or we reach the end of the list
        for ( row_ptr = sp ;
              row_ptr != NULL && row_ptr->rowNum != rowNumber ;
              row_ptr = row_ptr->next );
    
        // did we find the row?
        if ( row_ptr != NULL ) {
            // sum values on this row of data
            for ( col_ptr = row_ptr->rowhead ;
                  col_ptr != NULL ;
                  col_ptr = col_ptr->next ) {
                if ( col_ptr->colNum >= column1 && col_ptr->colNum <= column2 ) {
                    // this node is in the range of required colums, so add
                    result += col_ptr->data;
                }
            }
        }
    
        return result;
    }

  5. #5
    Ausandy
    Guest

    Smile

    I see, thanks very much for your help!!!

Popular pages Recent additions subscribe to a feed