双向链接节点矩阵

Matrix of doubly linked nodes

我正在尝试制作一个 11 x 11 的节点矩阵 linked 节点 Java 但我有一个问题,我 linked 节点到右边,左节点和下节点,但是当我尝试 link 到上节点时,我就是做不到,例如,当我尝试获取 node.up 时,我得到了一个空值而不是一个整数(我应该得到)。

无论如何,这是我的代码希望有人能帮助我。我猜错误可能在 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);
    }
 }

问题在于使用递归构造Nodes的网格。当你这样做时:

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

您实际上是在创建多个版本的网格,每个版本都会覆盖已创建的 rightdown linked Nodes。例如,构造后,对于给定的 node:

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

但鉴于网格的构造方式,情况并非如此,当您尝试 link 时,这会导致问题。考虑到对于 11x11 网格,您应该创建 121 Nodes,您可以看到问题有多严重,但我检查了一下,您实际上创建了 705,431!

幸运的是,修复相当简单。创建一个 Nodes 的二维数组并直接连接它们:

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);
   }
}

产生:

38
 48 60 50

我相信这就是您所期望的输出。