回答編集履歴
1
追記
answer
CHANGED
@@ -37,4 +37,66 @@
|
|
37
37
|
}
|
38
38
|
return 0;
|
39
39
|
}
|
40
|
+
```
|
41
|
+
|
42
|
+
[おまけ] 分割したやつ:
|
43
|
+
|
44
|
+
```C
|
45
|
+
// stack.h
|
46
|
+
#ifndef STACK_H__
|
47
|
+
#define STACK_H__
|
48
|
+
|
49
|
+
#include <stdbool.h>
|
50
|
+
|
51
|
+
bool put(char* putdata);
|
52
|
+
bool get(char** buffer);
|
53
|
+
|
54
|
+
#endif
|
55
|
+
```
|
56
|
+
|
57
|
+
```C
|
58
|
+
// stack.c
|
59
|
+
#include "stack.h"
|
60
|
+
|
61
|
+
#define MAX_STACK 256 /* スッタクサイズの上限 */
|
62
|
+
char* stack[MAX_STACK]; /* スタックの実体である配列 */
|
63
|
+
size_t size = 0; /* スタック内の要素数 */
|
64
|
+
|
65
|
+
bool put(char* putdata) {
|
66
|
+
if ( size < MAX_STACK ) {
|
67
|
+
// スタックにpush
|
68
|
+
stack[size++] = putdata;
|
69
|
+
return true;
|
70
|
+
}
|
71
|
+
return false;
|
72
|
+
}
|
73
|
+
|
74
|
+
bool get(char** buffer) {
|
75
|
+
if ( size > 0 ) {
|
76
|
+
// スタックからpop
|
77
|
+
*buffer = stack[--size];
|
78
|
+
return true;
|
79
|
+
}
|
80
|
+
return false;
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
```C
|
85
|
+
// main.c
|
86
|
+
#include <string.h>
|
87
|
+
#include <stdio.h>
|
88
|
+
#include <stdlib.h>
|
89
|
+
#include "stack.h"
|
90
|
+
|
91
|
+
int main() {
|
92
|
+
put(strdup("abc"));
|
93
|
+
put(strdup("def"));
|
94
|
+
put(strdup("ghi"));
|
95
|
+
char* str;
|
96
|
+
while ( get(&str) ) {
|
97
|
+
printf("[%s]\n", str);
|
98
|
+
free(str);
|
99
|
+
}
|
100
|
+
return 0;
|
101
|
+
}
|
40
102
|
```
|