Thread: Sparse matrix and linked list in C

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    Question Sparse matrix and linked list in C

    Hi, I need to represent a sparse matrix using liked list in C:
    The structure definition is the following;

    Code:
    typedef struct matrix{
        struct matrix *right;
        struct matrix *below;
        int rowptr, colptr;
        double val;
    }Matrix;
    I know that right is pointing to the next non-zero element of the row (below points to the non-zero element of the column).
    I need to define this functions:

    1.- Matrix* initiate(void), which uses stdin to read the non-zero element from a .txt file (which only contains the non-zero values).
    2.- void print(Matrix*), it prints the matrix using stdout.
    3.- Matrix* add(Matrix*, Matrix*);
    4.- void set_elemnt(Matrix* A, int i, int j, double x): assigns A(i,j)=x.
    5.- double get_element(Matrix * A, int i, int j); returns A(i,j).
    I don't know how to define the header cells (wich contain nothing), the other cells represents the non-zero values.
    What I have so far:

    Code:
    Matrix* initiate(void){
      Matrix*A=malloc(sizeof(*A));
    
      A->rowptr=rowptr;
      A->colptr=colptr;
    
      A->right=malloc(rowptr *sizeof(*rowptr->right));
      A->below=malloc(n *sizeof(*n->col));
    
      return A;
    }
    For the header (the one containing nothing), I tried to do this, but I have no idea if it is useful:

    Code:
    Matrix* initiate_1(void){
         Matrix*A=malloc(sizeof(*A));
      A->rowptr=-1;
      A->colptr=-1;
      A->val=nan;
      A->right=A;
      A->below=A;
    }
    To add matrix:

    Code:
    Matrix* add(Matrix*,Matrix*){
         int i;
         Matrix*C = initiate();//I don't know what to put here
         for(int i =0;; i++){//don't know what to put in the middle space
                                                             // don't know what to put here
         }
    }
    Also, I think I can create a matrix with zeros only, but I'm not sure on how I can do that and if it is of any help. I would appreciate if you could help me with this, I barely understand data structures. Thank you!
    Last edited by arr_c_93; 12-16-2015 at 07:27 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Consider these structures.
    Code:
    struct Col {
      double value;
      int colNumber; // which column this is
      struct Col *next;
    };
    struct Row {
      int rowNumber;  // which row this is
      struct Col *col;  // column values
      struct Row *next;
    };
    
    struct Matrix {
      int numRows;  // how big the matrix is
      int numCols;
      struct Row *row; // linked list of rows
    };
    A number of utility functions would help you.
    - check whether a Row exists
    - create a Row with a specific row number, and insert it into the matrix row list
    - check whether a Col exists within a given Row
    - create a Col with a specific column number, and insert it into the Row list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sparse matrix as linked lists
    By ioan1ioan in forum C Programming
    Replies: 13
    Last Post: 07-30-2011, 12:46 PM
  2. adjacency matrix and sparse matrix
    By dpp in forum C Programming
    Replies: 3
    Last Post: 07-11-2010, 10:26 AM
  3. Accessor for Sparse Matrix
    By Clark in forum C Programming
    Replies: 1
    Last Post: 04-02-2010, 05:14 PM
  4. Sparse Matrix
    By brb9412 in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 01:12 PM
  5. Sparse matrix
    By milenkom in forum C++ Programming
    Replies: 4
    Last Post: 01-02-2008, 04:17 PM

Tags for this Thread