struct NodeX {
int childcount;
struct NodeX *preNode;
struct NodeX *nextNode;
// struct NodeX nextNodeN; //rror: field 'nextNodeN' has incomplete type
struct NodeX *parentNode;
int deep;
char *name;
struct NodeX (*pChildNode[]);
// struct NodeX (*childNode)[];
// struct NodeX childNode[2];
};
void createChildParentNode(char parentName[], struct NodeX *currentNode, int aspectDeep) {
if (((struct NodeX) *currentNode).deep < aspectDeep) {
struct NodeX childNode = createNode();
// initNodeValue(childNode);
char buff[100];
sprintf(buff, "%s-deep-%d", parentName, ((struct NodeX) *currentNode).deep + 1);
childNode.name = buff;
childNode.deep = ((struct NodeX) *currentNode).deep + 1;
childNode.parentNode = ¤tNode;
printf("create node [%s],current deep= %d \n", buff, childNode.deep);
struct NodeX childNodes[] = {childNode};
((struct NodeX) *currentNode).childcount - 5;
((struct NodeX) *currentNode).pChildNode[0] = &childNodes[0];
createChildParentNode(buff, &childNode, aspectDeep);
}
}
struct NodeX createNode() {
struct NodeX x;
x.nextNode = NULL;
x.preNode = NULL;
x.name = NULL;
x.pChildNode[0] = NULL; //: error: invalid use of flexible array member
x.deep = 0;
x.childcount = 0;
return x;
}
void testStruct() {
struct Books book1;
strcpy(book1.title, "newbook");
book1.count = 1;
book1.price = 20.5;
book1.totalprice = 150.5555555;
strcpy(book1.author, "情随事迁");
printf("title:%s address %p\n", book1.title, book1.title);
printf("author:%s author %p\n", book1.title, book1.author);
printf("count:%d address %p size:%d\n", book1.count, &book1.count, sizeof(&book1.count));
printf("price:%f address %p\n", book1.price, &book1.price);
printf("totalprice:%lf address %p\n", book1.totalprice, &book1.totalprice);
struct NodeX parentNode = createNode();
// initNodeValue(parentNode);
parentNode.name = "parentNode";
parentNode.deep = 0;
struct NodeX childNode1 = createNode();
// initNodeValue(childNode1);
childNode1.name = "child1";
struct NodeX childNode2 = createNode();
// initNodeValue(childNode2);
childNode2.name = "child2";
childNode2.preNode = &childNode1;
childNode1.nextNode = &childNode2;
childNode1.parentNode = &parentNode;
childNode2.parentNode = &parentNode;
childNode2.deep = 1;
childNode1.deep = 1;
parentNode.pChildNode[0] = &childNode1;
parentNode.pChildNode[1] = &childNode2;
parentNode.pChildNode[2] = &childNode2;
parentNode.childcount = 3;
createChildParentNode("from-child1-", &childNode1, 10);
createChildParentNode("from-child2-", &childNode2, 5);
printf("[[[[[[[[[[[[[[[[[[[[[start-loop]]]]]]]]]]]]]]]]]]] \n");
#define TRUE 1
int count = 0;
struct NodeX *p_CurrentParentNode = &parentNode;
while (TRUE) {
if (p_CurrentParentNode == NULL) {
printf("pointer is empty exit");
break;
}
/* if((*p_CurrentParentNode)==NULL){//4: error: invalid operands to binary == (have 'struct NodeX' and 'void *')
printf("value empty exit");
break;
}*/
printf("will loop address %#x sizeof %d\n", p_CurrentParentNode,
sizeof(p_CurrentParentNode));
printf("[nodename]%s\n", ((struct NodeX) *p_CurrentParentNode).name);
printf("[nodedeep]%d\n", ((struct NodeX) *p_CurrentParentNode).deep);
if (p_CurrentParentNode->pChildNode[0] != NULL) {
printf("read child node\n");
p_CurrentParentNode = &p_CurrentParentNode->pChildNode[0];
} else if (((struct NodeX) *p_CurrentParentNode).nextNode != NULL) {
printf("will loop \n");
printf("address %#x \n", &p_CurrentParentNode->nextNode);
// p_CurrentParentNode=&(((struct NodeX)*p_CurrentParentNode)->nextNode;
p_CurrentParentNode = &p_CurrentParentNode->nextNode;
// }else if(((struct NodeX)*p_CurrentParentNode).pChildNode!=NULL){ //error: invalid operands to binary != (have 'struct NodeX *[]' and 'void *')
} else {
printf("not child node!\n");
break;
}
printf("loop node count %d \n", count);
count++;
if (count > 500) {
printf("error exit \n");
break;
}
}
}
c语言结构体Node数据结构测试
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 阅读须知: 结构体部分内容,涉及结构体定义,结构体变量,结构体指针,结构体数组,更多内容敬请关注。 结构体是由若干...
- 在 Objective-C 代码中,有时会遇到定义里不含 * 的变量,他们可能会使用 “栈空间” (stack s...