so using a singly-linked approach, I'm trying to create a two dimensional linked list(matrix). The matrix will be constructed with specified dimension(ex 3x4 matrix). Below is my code.
class MLink{
public MLink nextCol;
public MLink nextRow;
public long data;
//----------------------------------
MLink(long data){
this.data = data;
nextCol = null;
nextRow = null;
}
//----------------------------------
public void displayLink(){
System.out.print("{"+data+"} ");
}
//----------------------------------
}// end class MList
class MLinkList{
private MLink first;
private MLink current;
private int rows;
private int cols;
//----------------------------------
MLinkList(int rows, int cols){
this.rows = rows;
this.cols = cols;
MLink newLink = new MLink(0);
first = newLink;
current = first;
for(int i=0;i<rows;i++){
current.nextRow = new MLink(0);
for(int j=0;j<cols;j++){
current.nextCol = new MLink(0);
}
}
}
//----------------------------------
public boolean isEmpty(){
return (first==null);
}
//----------------------------------
}//end class MLinkList
public class MatrixListApp {
public static void main(String[] args) {
MLinkList q = new MLinkList(3,4);
}
}
I have to admit that I haven't made much progress because I got stuck at initializing the matrix in the MLinkList constructor. What I wanna do is I want to fill up the matrix with MLinks with 0 and then work my way up from there. I assume that I would have to move my current along the matrix to fill the matrix but how can I do so?
Any help is appreciated.