简书链接:c语言结构体Node数据结构测试
文章字数:145,阅读全文大约需要1分钟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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 = &currentNode;
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;
}

}







}