这就是我的结构的样子:
struct Coordinate{
int x;
int y;
int steps;
struct Coordinate *left;
struct Coordinate *right;
struct Coordinate *up;
struct Coordinate *down;
}*root;
typedef struct Coordinate *Coor;
我正在制作这样的许多结构树,并且在某个时候我想检查结构根的数据(x,y)。
我如何获取这个结构根(父)的数据?
* 编辑 *
这是我的所有代码(不是那么长):
//
// main.c
// C-3
//
// Created by Nimrod Shai on 6/21/13.
// Copyright (c) 2013 Nimrod Shai. All rights reserved.
//
#include <stdio.h>
#define length 2
struct Coordinate{
int x;
int y;
int steps;
struct Coordinate *left;
struct Coordinate *right;
struct Coordinate *up;
struct Coordinate *down;
}*root;
typedef struct Coordinate *Coor;
int isValidCoordinate(struct Coordinate *aCoordinate, int x, int y, int map[length][length]){
if ((x >= 0) && (x <= length) && (y >= 0) && (y <= length) && !map[y][x]) {
for (int i = 0; i < aCoordinate -> steps; i++) {
aCoordinate = aCoordinate -> father;
if (aCoordinate->x == x && aCoordinate->y == y) {
return 0;
}
}
return 1;
}else{
return 0;
}
}
Coor insertDataToTree(Coor root, int x, int y, int map[length][length], int steps){
steps++;
if (root == NULL) {
root = (Coor)malloc(sizeof(Coor));
root->x = x;
root->y = y;
root->steps = steps;
root -> left = root -> right = root -> up = root -> down = NULL;
}
//left
if (isValidCoordinate(root,root -> x - 1, root -> y, map)) {
printf("f");
root->left = insertDataToTree(root -> left, x - 1, y, map,steps);
}
//right
if (isValidCoordinate(root,root -> x + 1, root -> y, map)) {
printf("f");
root->right = insertDataToTree(root -> right, x + 1, y, map,steps);
}
//up
if (isValidCoordinate(root,root -> x, root -> y - 1, map)) {
printf("f");
root->up = insertDataToTree(root -> up, x, y - 1, map,steps);
}
//down
if (isValidCoordinate(root,root -> x, root -> y + 1, map)) {
printf("f");
root->down = insertDataToTree(root -> down, x, y + 1, map,steps);
}
Coor ggg = NULL;
return ggg;
}
int main(int argc, const char * argv[])
{
int map[length][length] = {
{0,0},
{0,0}
};
struct Coordinate startPoint;
startPoint.x = 0;
startPoint.y = 0;
startPoint.steps = -1;
insertDataToTree(root, startPoint.x, startPoint.y, map, startPoint.steps);
// insert code here...
printf("Hello, World!\n");
return 0;
}
这段代码的目的是将矩阵内的整个路径从某个起点映射到一棵树中,分支=路径到所有可能的点。
问题是 - 我需要对我添加到这棵树的一个点做的检查之一是它的坐标没有在它的分支中重复(不介意它是否出现在其他分支中)。
但我不知道如何获取某个结构的根的值。
我希望我现在更清楚了,并且有人可以帮助我处理这种糟糕的语法……(我通常在 Objective-C 中编程)。
谢谢!