回答編集履歴

1

いろいおr

2016/05/28 06:01

投稿

wake_up_kemeko
wake_up_kemeko

スコア104

test CHANGED
@@ -151,3 +151,161 @@
151
151
  }
152
152
 
153
153
  ```
154
+
155
+
156
+
157
+ それともこちら?
158
+
159
+
160
+
161
+ ```C
162
+
163
+ #include<stdio.h>
164
+
165
+ #include<stdlib.h>
166
+
167
+ #include<string.h>
168
+
169
+
170
+
171
+ #define STRLEN 10
172
+
173
+ struct cell {
174
+
175
+ char c;
176
+
177
+ struct cell *next;
178
+
179
+ };
180
+
181
+
182
+
183
+ struct cell *stack=NULL, *listhead = NULL;
184
+
185
+ char pop();
186
+
187
+ void free_stack();
188
+
189
+ void push(char ic);
190
+
191
+ void print_stack();
192
+
193
+
194
+
195
+ int main(void) {
196
+
197
+
198
+
199
+ int i;
200
+
201
+ char moji[STRLEN] = {'\0'};
202
+
203
+
204
+
205
+ while (1) {
206
+
207
+ printf("文字列かendを入力してください->");
208
+
209
+ scanf_s("%s", moji,STRLEN);//セキュリティ向上 VSさんが言うこと聞かないので
210
+
211
+
212
+
213
+ if (strcmp(moji, "end") == 0) {
214
+
215
+ free_stack();
216
+
217
+ return 0;
218
+
219
+ }
220
+
221
+
222
+
223
+
224
+
225
+ else {
226
+
227
+ if (strlen(moji) >= STRLEN) {
228
+
229
+ puts("文字長ながすぎんよ");
230
+
231
+ continue;
232
+
233
+ }
234
+
235
+
236
+
237
+ for (i = 0; moji[i]; ++i)push(moji[i]);//文字が入力されてるだけ格納
238
+
239
+
240
+
241
+ while (listhead != NULL) {//listheadに変更
242
+
243
+ printf("%c", pop());
244
+
245
+ print_stack();
246
+
247
+ }
248
+
249
+
250
+
251
+ }
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ char pop() {
260
+
261
+ char popdata;
262
+
263
+ popdata = listhead->c;
264
+
265
+ stack = listhead;
266
+
267
+ listhead = listhead->next;
268
+
269
+ free(stack);
270
+
271
+ return popdata;
272
+
273
+ }
274
+
275
+ void free_stack() {
276
+
277
+ if (listhead != NULL)
278
+
279
+ free(stack);
280
+
281
+ }
282
+
283
+ void push(char ic) {//一文字読み込みなら配列じゃなくていい
284
+
285
+ stack = (struct cell *)malloc(sizeof(struct cell));
286
+
287
+ stack->c = ic;
288
+
289
+ stack->next = listhead;
290
+
291
+ listhead = stack;
292
+
293
+ }
294
+
295
+ void print_stack() {
296
+
297
+ stack = listhead;
298
+
299
+ while (stack != NULL) {
300
+
301
+ printf("->%c", stack->c);
302
+
303
+ stack = stack->next;
304
+
305
+ }
306
+
307
+ printf("\n");
308
+
309
+ }
310
+
311
+ ```