C
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- まろ
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- @tyottyoworks
- ___shanon
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
259
printf("メニューを選んでください\n\n1=追加\n2=行きがけ順\n3=通りがけ順\n4=帰りがけ順\n5=指定した値の番地\n6=削除\n9=終了\n");
1
2
3
4
/* 木の節の定義 */
5
typedef struct node{
6
int data; /* 探索のキーになるデータ型 */
7
struct node *left; /* 左の子 */
8
struct node *right; /* 右の子 */
9
}NODE;
10
/* メンバright,leftにNULLポインタがセットされているときは該当する子がいないことを表す */
11
12
/* 初期状態では二分探索木は空の状態。
13
グローバル変数rootが二分探索木の根へのポインタを保持しているものとする */
14
/* グローバル変数rootをNULLで初期化 */
15
NODE *root = NULL;
16
17
/* エラーメッセージをプリントしてexitする関数*/
18
/* ポインタ(アドレス)の先のデータを読み取り専用にする */
19
void fatal_error(const char *s)
20
{
21
/* fprintf関数を使ってstderrで標準エラーを出力する*/
22
fprintf(stderr,"%s", s);
23
exit(1); /* 異常終了 */
24
25
}
26
27
/* 二分探索木を探索する関数 */
28
/* パラメータkeyを受け取ってポインタを返す関数として定義
29
key:探索すべきキーの値
30
探索に成功した場合そのデータの節へのポインタを返す */
31
NODE *search(int key)
32
{
33
NODE *p; /* 現在注目している節 */
34
p=root; /* まず根に注目する */
35
/* 進むべき子が存在する限り繰り返す */
36
while (p != NULL) {
37
/* キーと注目している節のデータが等しいか比較 */
38
if (key == p->data)
39
/* もしキーと注目している節のデータとが等しければポインタを関数として返す */
40
printf("探索した値の番地です>>%d\n",p->data);
41
return p;
42
else if (key < p->data) {
43
/* キーの方が小さければ左部分木に進む */
44
p = p->left;
45
else
46
/* キーの方が大きければ右部分木に進む*/
47
p = p->right;
48
}
49
/* ループを抜け出たということは見付からなかったというと
50
NULL返して失敗したことを知らせる */
51
printf("NotExist\n");
52
return NULL;
53
}
54
55
/* 二分探索木から要素を追加する関数*/
56
/* 探索と同じ要領で木を辿り、辿るべき部分木がなければそこに挿入する */
57
/* 挿入した要素が置かれる節へのポインタを返す
58
すでに要素が登録されているのなら、何もしないでNULLを返す
59
key:挿入するデータ */
60
NODE *insert(int key)
61
{
62
NODE **p,*new;
63
/* 変数pが変数rootを指すように初期化する */
64
p=&root;
65
/* 挿入すべき場所が見つかるまで繰り返す */
66
while (*p != NULL) {
67
/* キーと注目している節のデータが等しいか比較 */
68
if (key == (*p)->data)
69
/* すでに登録されている */
70
return NULL;
71
else if (key < (*p)->data) {
72
/* キーの方が小さければ左部分木に進む */
73
p =&(*p)->left;
74
else
75
/* キーの方が大きければ右部分木に進む */
76
p =&(*p)->right;
77
}
78
/* 挿入されるべき場所が見つかったら */
79
/* 挿入される節をつくる */
80
$ gcc prog.c -Wall -Wextra -std=gnu11 "-Werror"
Start
prog.c:2:2: error: stray '\343' in program 2 | #include <stdio.h> | ^~ prog.c:2:5: error: stray '#' in program 2 | #include <stdio.h> | ^ prog.c:2:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token 2 | #include <stdio.h> | ^ In file included from prog.c:3: /usr/include/stdlib.h:865:20: error: unknown type name 'wchar_t' 865 | extern int mbtowc (wchar_t *__restrict __pwc, | ^~~~~~~ /usr/include/stdlib.h:493:1: note: 'wchar_t' is defined in header '<stddef.h>'; did you forget to '#include <stddef.h>'? 492 | # include <alloca.h> +++ |+#include <stddef.h> 493 | #endif /* Use misc. */ /usr/include/stdlib.h:869:31: error: unknown type name 'wchar_t' 869 | extern int wctomb (char *__s, wchar_t __wchar) __THROW; | ^~~~~~~ /usr/include/stdlib.h:869:31: note: 'wchar_t' is defined in header '<stddef.h>'; did you forget to '#include <stddef.h>'? /usr/include/stdlib.h:873:25: error: unknown type name 'wchar_t' 873 | extern size_t mbstowcs (wchar_t *__restrict __pwcs, | ^~~~~~~ /usr/include/stdlib.h:873:25: note: 'wchar_t' is defined in header '<stddef.h>'; did you forget to '#include <stddef.h>'? /usr/include/stdlib.h:877:10: error: unknown type name 'wchar_t' 877 | const wchar_t *__restrict __pwcs, size_t __n) | ^~~~~~~ prog.c: In function 'search': prog.c:42:5: error: 'else' without a previous 'if' 42 | else if (key < p->data) { | ^~~~ prog.c:45:7: error: expected '}' before 'else' 45 | else | ^~~~ prog.c: In function 'insert': prog.c:74:5: error: expected '}' before 'else' 74 | else | ^~~~ prog.c:82:32: error: lvalue required as left operand of assignment 82 | if((new=malloc(sizeof(NODE)))=NULL) | ^ prog.c: In function 'delete': prog.c:123:8: error: implicit declaration of function 'deletemin'; did you mean 'delete'? [-Werror=implicit-function-declaration] 123 | *p=deletemin(&x->right); | ^~~~~~~~~ | delete prog.c:123:7: error: assignment to 'NODE *' {aka 'struct node *'} from 'int' makes pointer from integer without a cast [-Werror=int-conversion] 123 | *p=deletemin(&x->right); | ^ prog.c:124:17: error: lvalue required as left operand of assignment 124 | &(*p)->right=x->right; | ^ prog.c:125:16: error: lvalue required as left operand of assignment 125 | &(*p)->left=x->left; | ^ prog.c: At top level: prog.c:147:7: error: conflicting types for 'deletemin' 147 | NODE *deletemin(NODE **p) | ^~~~~~~~~ prog.c:123:8: note: previous implicit declaration of 'deletemin' was here 123 | *p=deletemin(&x->right); | ^~~~~~~~~ prog.c: In function 'deletemin': prog.c:151:2: error: this 'while' clause does not guard... [-Werror=misleading-indentation] 151 | while ((*p)->left != NULL) | ^~~~~ prog.c:153:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while' 153 | x=*p; | ^ prog.c: At top level: prog.c:159:1: error: return type defaults to 'int' [-Werror=implicit-int] 159 | preorder(NODE *p) | ^~~~~~~~ prog.c: In function 'preorder': prog.c:163:2: error: 'return' with no value, in function returning non-void [-Werror=return-type] 163 | return; | ^~~~~~ prog.c:159:1: note: declared here 159 | preorder(NODE *p) | ^~~~~~~~ prog.c: At top level: prog.c:175:1: error: return type defaults to 'int' [-Werror=implicit-int] 175 | inorder(NODE *p) | ^~~~~~~ prog.c: In function 'inorder': prog.c:179:2: error: 'return' with no value, in function returning non-void [-Werror=return-type] 179 | return; | ^~~~~~ prog.c:175:1: note: declared here 175 | inorder(NODE *p) | ^~~~~~~ prog.c: At top level: prog.c:188:1: error: return type defaults to 'int' [-Werror=implicit-int] 188 | postorder(NODE *p) | ^~~~~~~~~ prog.c: In function 'postorder': prog.c:192:2: error: 'return' with no value, in function returning non-void [-Werror=return-type] 192 | return; | ^~~~~~ prog.c:188:1: note: declared here 188 | postorder(NODE *p) | ^~~~~~~~~ prog.c:194:2: error: implicit declaration of function 'postorderr'; did you mean 'postorder'? [-Werror=implicit-function-declaration] 194 | postorderr(p->left); /* 左ノードへ移動 */ | ^~~~~~~~~~ | postorder prog.c: In function 'main': prog.c:223:2: error: too few arguments to function 'preorder' 223 | preorder(); | ^~~~~~~~ prog.c:159:1: note: declared here 159 | preorder(NODE *p) | ^~~~~~~~ prog.c:228:2: error: too few arguments to function 'inorder' 228 | inorder(); | ^~~~~~~ prog.c:175:1: note: declared here 175 | inorder(NODE *p) | ^~~~~~~ prog.c:233:2: error: too few arguments to function 'postorder' 233 | postorder(); | ^~~~~~~~~ prog.c:188:1: note: declared here 188 | postorder(NODE *p) | ^~~~~~~~~ prog.c:245:4: error: stray '\343' in program 245 | delete(key2); | ^~ prog.c:204:6: error: unused variable 'n' [-Werror=unused-variable] 204 | int n; | ^ prog.c:203:6: error: unused variable 'i' [-Werror=unused-variable] 203 | int i; | ^ cc1: all warnings being treated as errors
1
Finish