これは私の構造体がどのように見えるかです:
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;
}
このコードの目的は、マトリックス内のパス全体を、特定の開始点からすべての可能なポイントへの branch=paths を持つツリーにマッピングすることです。
問題は - このツリーに追加するポイントで行う必要があるチェックの 1 つは、座標がそのブランチで繰り返されていないことです (他のブランチに表示されても気にしないでください)。
しかし、特定の構造体のルートの値を取得する方法がわかりません。
私は今、私がより明確になり、誰かがこのひどい構文で私を助けてくれることを願っています... (私は通常、Objective-C でプログラムしています)。
ありがとう!