Search code examples
javamatrixnodesdoubly-linked-list

Matrix of doubly linked nodes


i'm trying to make a 11 x 11 matrix of nodes doubly linked nodes in Java but i have a problem, i linked the nodes to right, left and down node but when i try to link to the up node i just can't and when i try to get node.up for example, i got a null instead of getting an int(which i should get).

Anyway, here is my code hoping someone can help me. I guess the error may be in void linkUp().

public class CazadorPresa {

// node of linked list 
static class Node { 
    int no; 
    Node right; 
    Node down;  
    Node left;
    Node up;

    int xpos,ypos;
    public boolean hunter,prey,crossed,blocked;
}; 

// returns head pointer of linked list 
// constructed from 2D matrix 
static Node construct(int arr[][], int i, int j, int m, int n) { 

    // return if i or j is out of bounds 
    if (i > n - 1 || j > m - 1) 
        return null; 

    // create a new node for current i and j 
    // and recursively allocate its down and 
    // right pointers 
    Node temp = new Node();


    temp.no = arr[i][j]; 

    temp.xpos = j;
    temp.ypos = i;

    temp.blocked = false;
    temp.crossed = false;
    temp.hunter = false;
    temp.prey = false;

    temp.right = construct(arr, i, j + 1, m, n); 
    temp.down = construct(arr, i + 1, j, m, n); 

    return temp;
} 

// utility function for displaying 
// linked list data 
static void display(Node head) { 

    // pointer to move right 
    Node Rp; 

    // pointer to move down 
    Node Dp = head; 

    // loop till node->down is not NULL 
    while (Dp != null) { 
        Rp = Dp; 

        // loop till node->right is not NULL 
        while (Rp != null) { 
            System.out.print(Rp.no + " "); 
            Rp = Rp.right; 
        } 
        System.out.println(); 
        Dp = Dp.down; 
    } 
} 

// link left
static void linkLeft(Node head) { 

    Node Rp; 

    Node Dp = head; 
    Node auxL= head; 

    // loop till node->down is not NULL 
    while (Dp != null) { 
        Rp = Dp; 

        // loop till node->right is not NULL 
        while (Rp != null) { 

            if(Rp==Dp){

            }else{
                Rp.left = auxL;
                auxL = Rp;
            }

            Rp = Rp.right;    
        } 

        Dp = Dp.down; 
    } 
}

// link UP
static void linkUp(Node head) { 
    // pointer to move right 
    Node Rp; 

    // pointer to move down 
    Node Dp = head; 
    Node aux;

    // loop till node->down is not NULL 
    while (Dp != null) { 
        Rp = Dp; 

        // loop till node->right is not NULL 
        while (Rp != null) { 

            aux = Rp.down;

            if(aux==null){

            }else{
              aux.up = Rp;
            }

            Rp = Rp.right; 
        } 

        Dp = Dp.down; 
    }
}

static void hunter(Node head,int x, int y) { 

    Node arr,aba,izq,der;
    boolean out = false;
    // pointer to move right 
    Node Rp; 

    // pointer to move down 
    Node Dp = head; 

    // loop till node->down is not NULL 
    while (Dp != null) { 
        Rp = Dp; 

        // loop till node->right is not NULL 
        while (Rp != null) { 

            if(Rp.xpos==x-1 && Rp.ypos==y-1){

                Rp.hunter = true;

                arr=Rp.up;
                if(arr==null){
                    System.out.println("No link up");
                }else{
                    System.out.println(arr.no);
                }
                aba=Rp.down;
                izq=Rp.left;
                der=Rp.right;

                System.out.println(" "+izq.no+" "+aba.no+" "+der.no);
                out=true;
            }
            if(out==true){
                break;
            }
            Rp = Rp.right; 
        } 
        if(out==true){
            break;
        }
        Dp = Dp.down; 
    } 
}            

// driver program 
public static void main(String args[]) { 
    // 2D matrix 
    int arr[][]= new int[11][11];
    int no=1;
    for(int i=0;i<11;i++){
        for(int j=0;j<11;j++){

          arr[i][j] = no;
          no=no+1;
        }
    }

    int m = 11, n = 11; 

    Node head = construct(arr, 0, 0, m, n); 

    linkUp(head);
    linkLeft(head);

    display(head); 

    System.out.println("I should get: 38 48 60 50 but i get: ");
    hunter(head,5,5);
    }
 }

Solution

  • The problem is in the use of recursion to construct the grid of Nodes. When you do:

    temp.right = construct(arr, i, j + 1, m, n); 
    temp.down = construct(arr, i + 1, j, m, n); 
    

    You're actually creating multiple versions of the grid, each one overwriting right and down linked Nodes that have already been created. For example, it should be the case that after construction, for a given node:

    node.right.down == node.down.right
    

    but given how the grid is constructed this will not be the case, which then causes problems when you come to try to link them up. You can see how bad the problem is by considering that for an 11x11 grid you should be creating 121 Nodes, but I checked and you're actually creating 705,431!

    Fortunately the fix is fairly straightforward. Create a 2d array of Nodes and hook up them up directly:

    public static void main(String args[]) { 
       // 2D matrix 
       Node arr[][]= new Node[11][11];
       int m = 11, n = 11; 
    
       int no=1;
       for(int i=0;i<m;i++){
           for(int j=0;j<n;j++){
    
             arr[i][j] = new Node();
             arr[i][j].no = no;
             arr[i][j].xpos = j;
             arr[i][j].ypos = i;
             no=no+1;
           }
       }
    
       for(int i=0; i<m; i++)
       {
         for(int j=0; j<n; j++)
         {
           arr[i][j].up    = (i>0)   ? arr[i-1][j] : null;
           arr[i][j].left  = (j>0)   ? arr[i][j-1] : null;
           arr[i][j].down  = (i+1<m) ? arr[i+1][j] : null;
           arr[i][j].right = (j+1<n) ? arr[i][j+1] : null;
         }
       }
    
       Node head = arr[0][0];
       display(head); 
    
       hunter(head,5,5);
       }
    }
    

    Which produces:

    38
     48 60 50
    

    Which I believe is the output you were expecting.