質問編集履歴
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
木の構築を行い、木の探索をします。
|
4
4
|
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
@@ -11,7 +11,7 @@
|
|
11
11
|
### 試したこと
|
12
12
|
|
13
13
|
木の構築自体は出来ているみたいです。
|
14
|
-
|
14
|
+
|
15
15
|
|
16
16
|
### 補足情報(FW/ツールのバージョンなど)
|
17
17
|
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,113 +6,7 @@
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
7
|
文字を入力するとsegmentation違反となってしまう
|
8
8
|
|
9
|
-
### 該当のソースコード
|
10
9
|
|
11
|
-
```C
|
12
|
-
#include <stdio.h>
|
13
|
-
#include <string.h>
|
14
|
-
#include <stdlib.h>
|
15
|
-
|
16
|
-
struct node
|
17
|
-
{
|
18
|
-
char animal[20];
|
19
|
-
struct node *left;
|
20
|
-
struct node *right;
|
21
|
-
};
|
22
|
-
|
23
|
-
void tree_structure(struct node *root, char *name)
|
24
|
-
{
|
25
|
-
struct node *p, *q;
|
26
|
-
q = root;
|
27
|
-
if (strcmp(q->animal, name) > 0)
|
28
|
-
{
|
29
|
-
if (q->left == NULL)
|
30
|
-
{
|
31
|
-
p = (struct node *)malloc(sizeof(struct node));
|
32
|
-
q->left = p;
|
33
|
-
p->right = p->left = NULL;
|
34
|
-
strcpy(q->left->animal, name);
|
35
|
-
}
|
36
|
-
else
|
37
|
-
{
|
38
|
-
tree_structure(q->left, name);
|
39
|
-
}
|
40
|
-
}
|
41
|
-
else if (strcmp(q->animal, name) <= 0)
|
42
|
-
{
|
43
|
-
if (q->right == NULL)
|
44
|
-
{
|
45
|
-
p = (struct node *)malloc(sizeof(struct node));
|
46
|
-
q->right = p;
|
47
|
-
p->right = p->left = NULL;
|
48
|
-
strcpy(q->right->animal, name);
|
49
|
-
}
|
50
|
-
else
|
51
|
-
{
|
52
|
-
tree_structure(q->right, name);
|
53
|
-
}
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
int member_tree(char *search, struct node *root)
|
58
|
-
{
|
59
|
-
struct node *p, *q;
|
60
|
-
q = root;
|
61
|
-
if (q == NULL)
|
62
|
-
return 0;
|
63
|
-
else if (strcmp(q->animal, search) > 0)
|
64
|
-
{
|
65
|
-
member_tree(search, q->left);
|
66
|
-
}
|
67
|
-
else if (strcmp(q->animal, search) < 0)
|
68
|
-
{
|
69
|
-
member_tree(search, q->right);
|
70
|
-
}
|
71
|
-
else
|
72
|
-
{
|
73
|
-
return 1;
|
74
|
-
}
|
75
|
-
}
|
76
|
-
|
77
|
-
int main(void)
|
78
|
-
{
|
79
|
-
struct node *root = NULL;
|
80
|
-
struct node *p;
|
81
|
-
int i, k, j;
|
82
|
-
char search[20];
|
83
|
-
char animals[][20] = {"elephant", "cat", "dog", "rat", "horse", "bat", "hamster"};
|
84
|
-
k = sizeof(animals) / sizeof(animals[0]);
|
85
|
-
if (root == NULL)
|
86
|
-
{
|
87
|
-
p = (struct node *)malloc(sizeof(struct node));
|
88
|
-
root = p;
|
89
|
-
strcpy(root->animal, animals[0]);
|
90
|
-
p->left = p->right = NULL;
|
91
|
-
}
|
92
|
-
for (i = 1; i < k; i++)
|
93
|
-
{
|
94
|
-
tree_structure(root, animals[i]);
|
95
|
-
}
|
96
|
-
|
97
|
-
while (1)
|
98
|
-
{
|
99
|
-
printf("動物名は英語で入力してください(00で終了) -->");
|
100
|
-
scanf("%s", search);
|
101
|
-
printf("%s", search);
|
102
|
-
j = member_tree(search, root);
|
103
|
-
if (j == 1)
|
104
|
-
printf("%sは存在します.\n", search);
|
105
|
-
else if (strcmp(search, "00") == 0)
|
106
|
-
{
|
107
|
-
printf("終了します.\n");
|
108
|
-
break;
|
109
|
-
}
|
110
|
-
else if (j == 0)
|
111
|
-
printf("%sは存在しません.\n", search);
|
112
|
-
}
|
113
|
-
return 0;
|
114
|
-
}
|
115
|
-
```
|
116
10
|
|
117
11
|
### 試したこと
|
118
12
|
|