双链接节点矩阵

时间:2018-10-13 22:07:04

标签: java matrix nodes doubly-linked-list

我正在尝试使节点的11 x 11矩阵在Java中成为双链接节点,但是我遇到了问题,我将节点链接到右,左和下节点,但是当我尝试链接到上节点时,我只是不能,例如当我尝试获取node.up时,我得到的是null而不是int(我应该得到)。

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

1 个答案:

答案 0 :(得分:0)

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

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

您实际上正在创建网格的多个版本,每个版本都覆盖已经创建的rightdown链接的Nodes。例如,对于给定的node,应该是在构造之后:

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

,但是考虑到网格的构造方式,情况并非如此,当您尝试将它们链接起来时,就会引起问题。考虑到对于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

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