2013-06-21 25 views
0

這是我的結構看起來像根/父:如何調用結構

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中編程)。

謝謝!

+0

您不需要在C程序中投射'malloc'的返回值。 –

+0

如果給定一個'Coor *',那麼就沒有辦法從'Coor'結構中的數據中找到它的父節點。如果你需要的話,你必須在結構中添加'struct Coordinate * parent',並且適當地初始化它。 –

+0

我曾考慮過這樣做,但我不知道在哪裏正確初始化父變量 –

回答

0

root->xroot->y,假設你已經在其他代碼中正確地分配了它,但是你沒有向我們展示。