回答編集履歴
4
不要include削除
test
CHANGED
@@ -13,8 +13,6 @@
|
|
13
13
|
```c++
|
14
14
|
|
15
15
|
#include <iostream>
|
16
|
-
|
17
|
-
#include <algorithm>
|
18
16
|
|
19
17
|
#include <cstring>
|
20
18
|
|
3
コード内のコメント変更
test
CHANGED
@@ -46,7 +46,7 @@
|
|
46
46
|
|
47
47
|
if(pNode == NULL) {
|
48
48
|
|
49
|
-
pNode = (BinNode*)malloc(sizeof(BinNode));
|
49
|
+
pNode = (BinNode*)malloc(sizeof(BinNode)); //呼び出し元の変数に代入
|
50
50
|
|
51
51
|
pNode->word = (char*)malloc(strlen(word)+1);
|
52
52
|
|
@@ -58,11 +58,11 @@
|
|
58
58
|
|
59
59
|
else if(strcmp(pNode->word, word) < 0)
|
60
60
|
|
61
|
-
apndNode(pNode->pRight, word); //"pNode->pRight = "は
|
61
|
+
apndNode(pNode->pRight, word); //呼び出し元での"pNode->pRight = "は不要
|
62
62
|
|
63
63
|
else
|
64
64
|
|
65
|
-
apndNode(pNode->pLeft, word); //"pNode->pLeft = "は
|
65
|
+
apndNode(pNode->pLeft, word); //呼び出し元での"pNode->pLeft = "は不要
|
66
66
|
|
67
67
|
}
|
68
68
|
|
2
C++コード追記
test
CHANGED
@@ -3,3 +3,85 @@
|
|
3
3
|
ポインタを渡しても呼び出し元の変数には代入できないので、呼び出し元で変数に代入する必要があります。
|
4
4
|
|
5
5
|
参照渡しできる言語(C++, C#, PHP)ならば、呼び出した先で呼び出し元の変数に代入できるので、呼び出し元での代入を省略できます。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
参照渡しにしたコードを書いてみました。g++でコンパイルして実行してみてください。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```c++
|
14
|
+
|
15
|
+
#include <iostream>
|
16
|
+
|
17
|
+
#include <algorithm>
|
18
|
+
|
19
|
+
#include <cstring>
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
#define MAX 1000000
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
/* ノードの構造体 */
|
28
|
+
|
29
|
+
typedef struct BINNODE {
|
30
|
+
|
31
|
+
char *word;
|
32
|
+
|
33
|
+
struct BINNODE *pLeft;
|
34
|
+
|
35
|
+
struct BINNODE *pRight;
|
36
|
+
|
37
|
+
} BinNode;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
/* pNodeの木にwordをメンバ変数に持つノードを追加する再帰関数 */
|
42
|
+
|
43
|
+
void apndNode(BinNode *&pNode, const char *word) {
|
44
|
+
|
45
|
+
int cond;
|
46
|
+
|
47
|
+
if(pNode == NULL) {
|
48
|
+
|
49
|
+
pNode = (BinNode*)malloc(sizeof(BinNode));
|
50
|
+
|
51
|
+
pNode->word = (char*)malloc(strlen(word)+1);
|
52
|
+
|
53
|
+
strcpy(pNode->word, word);
|
54
|
+
|
55
|
+
pNode->pLeft = pNode->pRight = NULL;
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
else if(strcmp(pNode->word, word) < 0)
|
60
|
+
|
61
|
+
apndNode(pNode->pRight, word); //"pNode->pRight = "はなぜいる?
|
62
|
+
|
63
|
+
else
|
64
|
+
|
65
|
+
apndNode(pNode->pLeft, word); //"pNode->pLeft = "はなぜいる?
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
int main(){
|
72
|
+
|
73
|
+
struct BINNODE *root = NULL;
|
74
|
+
|
75
|
+
apndNode(root, "hello");
|
76
|
+
|
77
|
+
apndNode(root, "world");
|
78
|
+
|
79
|
+
printf("word: %s\n", root->word);
|
80
|
+
|
81
|
+
printf("right: %s\n", root->pRight ? root->pRight->word : "NULL");
|
82
|
+
|
83
|
+
printf("left: %s\n", root->pLeft ? root->pLeft->word: "NULL");
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
```
|
1
文言訂正
test
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
ちょっと難しい回答
|
1
|
+
ちょっと難しい回答になるけど・・・
|
2
2
|
|
3
|
-
ポインタを渡しても変数には代入できないので、変数に代入する必要があります。
|
3
|
+
ポインタを渡しても呼び出し元の変数には代入できないので、呼び出し元で変数に代入する必要があります。
|
4
4
|
|
5
5
|
参照渡しできる言語(C++, C#, PHP)ならば、呼び出した先で呼び出し元の変数に代入できるので、呼び出し元での代入を省略できます。
|