我有一个AVL树,如:
(6,25)
/ \
(6,12) (9,25)
我希望将所有元素存储在一个类似的元组中:
array= [(6,12),(6,25),(9,25)]
如何用C语言实现它?
将结构用于异构数据,例如:
struct my_eth_tuple
{
int a;
float b;
char s[32];
};
然后创建元组数组:
struct my_eth_tuple[3];
您也可以通过以下方式初始化它:
struct my_eth_tuple[3] = {{6, 12.0, "tuple 1"},{6, 25, "tuple 2"},{9, 25, "tuple 3"}};
如果数据是同质的,则可以使用简单的数组数组:
typedef int my_hom_tuple[2];
my_hom_tuple[3] = {{6,12},{6,25},{9,25}};
要以编程方式访问这两个使用标准结构或数组的用法,如下例所示:
struct my_eth_tuple
{
int a;
float b;
char s[32];
};
typedef int my_hom_tuple[2];
struct my_eth_tuple[3];
struct my_hom_tuple[3];
void foo(void)
{
for (int i = 0; i < 3; i++)
{
my_eth_tuple[i].a = i + 1;
my_eth_tuple[i].b = (float)i *2.0;
my_eth_tuple[i].s[0] = '\0';
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++)
{
my_hom_tuple[i][j] = i + 1;
my_hom_tuple[i][j] = i * 2;
}
}
}