我正在尝试在 Java 中创建一个 11 x 11 的节点矩阵双向链接节点,但我有一个问题,我将节点链接到右、左和下节点,但是当我尝试链接到上节点时,我就是不能例如,当我尝试获取 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;
}
}
收到一只叮咚
相关分类