I'm coding the program that using linked list to store a sparse matrix. First I create a class "Node" contains the index of entry, value of entry and two pointers to next row and next column. Second I find on Google that I need to create the class Matrix like this code below but I don't understand the meaning of `Node **rowList`

and `node** columnList`

. Why they use a pointer to a pointer there and how could I implement a matrix from that? Thank you so much.

```
class Node
{
public:
int iValue, jValue;
float value;
Node *rowPtr;
Node *colPtr;
};
class Matrix
{
Node **rowList; // rowList is the pointer to the array of rows
Node **columnList; // columnList is the pointer to the array of columns
int rows, cols; // number of rows and columns
}
```

It appears to be exactly what the comment says. They are arrays. Presumably `rowList`

will be an array of `rows`

elements, and `columnList`

will be an array of `cols`

elements. The reason it's a `Node**`

is that each item in the array is a `Node*`

. A pointer to an array always has an extra level of indirection (an extra `*`

). That means when you index a single element out of that array you get a value of type `Node*`

again.

The arrays are created like this:

```
rowList = new Node* [rows];
columnList = new Node* [cols];
// Don't forget to initialise the values to NULL! Here's the dull way:
for( int i = 0; i < rows; i++ ) rowList[i] = NULL;
for( int i = 0; i < cols; i++ ) columnList[i] = NULL;
```

When you need to delete them (in the destructor for `Matrix`

):

```
delete [] rowList;
delete [] colList;
```

As for your question on how to implement your matrix from that, that's really up to you. Presumably when you create a node at position `(i, j)`

, you append that node to each of `rowList`

and `columnList`

. *ie*:

```
Node * node = new Node(i, j, 123.0);
rowList[i] = node;
columnList[j] = node;
```

But it's not that simple, because the node obviously must be linked into both a row and column list. At the very basic level, and using the structures you've provided, here's one way:

```
// Inserts newNode at the head of the list and modifies the head pointer.
void insert_row( Node* & r, Node *newNode )
{
newNode->rowPtr = r;
if( r != NULL ) r->rowPtr = newNode;
r = newNode;
}
// Similarly with insert_col()...
```

Now using the above with my original example:

```
Node * node = new Node(i, j, 123.0);
insert_row( rowList[i], node );
insert_col( columnList[j], node );
```

Since you have code already, I will offer my take on it. But you still need to do some work yourself.

I just try to understand the concept but it's so confusing for me.

Let's just clean things up to begin with. It's a class, and you're using C++ so please use your C++ knowledge:

```
class Node
{
public:
Node( int i, int j, int val );
void InsertRowAfter( Node* node );
void InsertColAfter( Node* node );
int iValue, jValue; // Row and column index, 1-based
float value; // Element value
Node *rowPtr; // Next element in this row (sorted by jValue)
Node *colPtr; // Next element in this column (sorted by iValue)
};
Node::Node( int i, int j, int val )
: iValue(i)
, jValue(j)
, value(val)
, rowPtr(NULL)
, colPtr(NULL)
{}
// Inserts the given node to follow this node in the row list
void Node::InsertRowAfter( Node* node )
{
// [go on, you do it]
}
// Inserts the given node to follow this node in the column list
void Node::InsertColAfter( Node* node );
{
// [go on, you do it]
}
```

So, now you need to implement the `Matrix::inputData`

function... Essentially you do what your friend was trying to do, but without the errors and memory leaks. That means you start like this:

```
// Use 'horz' and 'vert' to search through the row and column lists. If a
// node needs to be created, it will be stored in 'node'.
Node *horz = rowList[iValue - 1];
Node *vert = columnList[jValue - 1];
Node *node;
// If there is no row list or smallest jValue, insert at the head.
// Otherwise, search for an insert point.
if( !horz || horz->jValue > jValue )
{
// [go on, you do it]
}
else
{
// Move 'horz' pointer to position at which we will append a node.
Node *next = horz->rowPtr;
while( next && next->jValue <= jValue ) {
horz = next;
next = next->rowPtr;
}
// If replacing an existing value, there's nothing else to do.
if( horz->jValue == jValue ) {
horz->value = value;
return;
}
// Otherwise append a new node.
// [go on, you do it]
}
```

Now, you finish the function off, and don't forget to do the column indexing...

Similar Questions

I have a 5000 *5000 sparse matrix with 4 different values. I want to visualise the nonzero elements with 4 different colors such that I can recognise the ratio of this values and the relationships bet

I want to keep a linked list in sorted order when inserting elements (about 200000 elements in the list), which algorithm can you recommend? I made a simple implementation using insertion sort, but it

I'm looking for any standard C program that uses OpenMP APIs for a sparse matrix-vector or matrix-matrix multiplications. Can anyone let me know if there are any such programs.

type(A) <class 'scipy.sparse.csc.csc_matrix'> A.shape (8529, 60877) print A[0,:] (0, 25) 1.0 (0, 7422) 1.0 (0, 26062) 1.0 (0, 31804) 1.0 (0, 41602) 1.0 (0, 43791) 1.0 print A[1,:] (0, 7044) 1.0

I'm using numpy and scipy. I have a large sparse matrix and I want to find the largest eigenvalue of the sparse matrix. How can I do that?

When representing graphs in memory in a language like Java, either an adjacency matrix is used (for dense graphs) or an adjacency list for sparse graphs. So say we represent the latter like Map<Int

I am having a hard time working with Linked List/Nodes, I basically have to create my own Linked List in Java using only nodes. This is what I have to make: ----- ----- ----- head -->| B |------

cuSparse only has a function api for multiplying a sparse matrix with a dense matrix. How to do multiply operation for two sparse matrices using cuSparse or any other cuda liberary?

I am having a problem with my linked list. I'm pretty sure it is my pointers being off, or I didn't pass a pointer in the correct way as I am new to c. Structs are also new to me, and c++ is the langu

This is a very simple question - what is the best practice to work with triangle matrixes and to work with sparse matrices in C++? For triangle matrix I suggest a data format as easy as double* myMa

This question was asked to me in an interview: There are two header of two linked lists. There is a merged linked list in c where in the second linked list is merged into the first one at some point.

I am trying to bubble-sort a singly linked list using pointer manipulation in C. I've looked at some other implementations of bubble-sort on the website, but I feel like the logic of my code here shou

I am working on a simple text editor in C. I am having troubles with inserting an element in a linked list. Here is my structure: struct node { struct node *previous; int c; int x; int y; struct node

I am implementing a min-heap/priority-queue in C++ using this code. I have created a .h file that creates a linked list (it works fine, gives no errors when I test it out by itself). I have created a

I really couldn't google it. How to transform sparse matrix to ndarray? Assume, I have sparse matrix t of zeros. Then g = t.todense() g[:10] matrix([[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]])

I want to test some of the newer sparse linear solvers and I want to know if there is a fast way of filling in the matrix. The format I'm interested is CSR (http://goo.gl/hLXYd). Let's say the matrix,

I'm new to c and I'm trying to build a doubly linked list. I have a small concern which I would like your help please. I need to input a string that looks like this: (word)_#_(year)_#_(english synony

I have a code, which seems to be working but I am failing to get the values stored in the linked list between the first and the last node, the pointers in between are skipped? And dereferencing these

I am trying to print the reverse linked list. But I am getting only one value. Where am I going wrong? Please bear with me as I am new to C. #include<stdio.h> #include<stdlib.h> struct ite

I got a problem when using octave sparse matrix. max(speye(65536)(:)) will result in a 0x0 variable. However, speye(65535) and speye(65537) works. How that happens? My octave version is 3.2.4 in Fedo

I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The method below is the function i use to access the elements of a matrix, if not possible thr

I have a 1034_by_1034 sparse matrix (scipy.sparse.csr.csr_matrix), which basically represents the adjacency matrix of a graph. I want to check if some elements are ones or not. But I found this to be

I have some difficulties to implement one iterative function for sorted insert in linked list. I have previously done that with recursion which is much easier when the insert() function is called, but

I have a linked list of objects where each object has a linked list of char. I declared three iterators: top = someList.begin(); middle = top++; bottom = middle++; When I print the list of each of th

I am having trouble with my display_matrix function in my program and I am also recieving a segmentation fault when I run my program, I don't know if that has to do with me not having a destroy_memory

Is there a simpler way of generating sparse matrix other than this? for (i = 0; i < 1000; i++) { if (rand() % 3 == 0) { array[i] = rand() % 3; } else { array[i] = ((rand() % 3) - 1); } } Thanks. I

Is there a way to create linked list in objective c. I'm a newbie and so far I've researched in apple developer guides, there isn't any function predefined for linked list. Is doubly linked list same

I am trying to implement a map using a template-doubly linked list. I am trying to write a get function: valueType get (keyType key, bool & success) const; Returns the value associated with the

I understand the concept of LinkedList, and single/doubly linked list. I, however don't understand how to implement it to my code? I have to convert this singly list to a doubly linked list: public c

I know how to implement graph using linked list or Matrix. But i want to know when to use Linked List & when to use matrix for graph representation?

I'm having trouble with what should be a simple program. I've written a single linked list implementation in C using void* pointers. However, I have a problem, as there is a possible memory leak somew

I am fairly new to c (and this site) and I am having a lot of issues with segmentation faults. I am writing a program that creates a linked list of numbers and inserts values in ascending order. void

I have a scipy.sparse.csr matrix and would like to dump it to a CSV file. Is there a way to preserve the sparsity of the matrix and write it to a CSV?

I'm currently working with sparse matrices, and I have to compare the computation time of sparse matrix-matrix multiplication with full matrix-matrix multiplication. The issue is that sparse matrix co

Does anyone have experience with Sparse? I seem unable to find any documentation, so the warnings and errors it produces are unclear to me. I tried checking the mailing list and man page but there rea

I have a generic linked list that works with various types of data including objects and pointers to objects, etc, but i'm having trouble working with the list when I insert objects from a class that

While solving a machine learning problem using scikit (python) I need to do scaling of scipy.sparse matrix before doing the training using SVM in order to achieve higher accuracy. But its clearly ment

Is it possible to traverse (IE: Find an element) in a linked list using Lambda? My assumption is that it isn't?

I have a large sparse matrix (lil) implemented in Python with scipy, which comprises of Users on one axis, and songs they played on the other. So each row is a linked list of the songs that user has p

What is the best way to efficiently remove columns from a sparse matrix that only contain zeros. I have a matrix which I have created and filled with data: matrix = sp.sparse.lil_matrix((100, 100)) I

I did a sparse qr decomposition with Matrix package in R like a <- Matrix(runif(20), nrow = 5, sparse = T) a[3:5,] <- 0 #now **a** is a 5X4 matrix b <- qr.R(qr(a), complete = T) #but now **

I read through this page on assigning a sparse matrix. Unfortunately, I do not understand it. Can anyone help me out with an example? For instance, how should I assign the following 10 by 8 sparse mat

I am trying to create a generic doubly linked list , and I am having trouble getting my head around it. Does anyone have an example of a very simple implementation of Doubly Linked List using C#? Than

here is code to implement linked list i hope you understand main purpose of this code such kind of code is written in java and i am trying to implement in c++ #include <iostream> using namespace

I would like to know the best way to check if a scipy sparse matrix, if CSC or CSR. Right now I'm using. rows, cols = X.shape() indptr = X.indptr() if len(indptr) == cols + 1: print csc else: print

I am met a segfault during implementing my own linked_list in C++. I spent hours on it but still cannot figure out the bug. Any help will be appreciated. Thank you in advance. I think the error is in

I have a very large (about 91 million non-zero entries) sparseMatrix() in R that looks like: > myMatrix a b c a . 1 2 b 1 . . c 2 . . I would like to convert it to a triangular matrix (upper or lo

I am new to Eigen and have limited experience in C++. I have a file which is represented in sparse format (like in LIBSVM) and I want to load the data into a matrix using Eigen. Can someone tell me ho

Hey everyone, I am new to C and I am working on an XOR linked list for a project. I have most of the code done, but I can't seem to get the delete function of the list to work properly. It seems able

I need a command to check for zero sparse matrix, isempty(..) does not work. Is there some sparse version of isempty(..)? >> mlf2=sparse([],[],[],2^31+1,1) mlf2 = All zero sparse: 2147483649-by-