teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

いろいおr

2016/05/28 06:01

投稿

wake_up_kemeko
wake_up_kemeko

スコア104

answer CHANGED
@@ -74,4 +74,83 @@
74
74
  }
75
75
  printf("\n");
76
76
  }
77
+ ```
78
+
79
+ それともこちら?
80
+
81
+ ```C
82
+ #include<stdio.h>
83
+ #include<stdlib.h>
84
+ #include<string.h>
85
+
86
+ #define STRLEN 10
87
+ struct cell {
88
+ char c;
89
+ struct cell *next;
90
+ };
91
+
92
+ struct cell *stack=NULL, *listhead = NULL;
93
+ char pop();
94
+ void free_stack();
95
+ void push(char ic);
96
+ void print_stack();
97
+
98
+ int main(void) {
99
+
100
+ int i;
101
+ char moji[STRLEN] = {'\0'};
102
+
103
+ while (1) {
104
+ printf("文字列かendを入力してください->");
105
+ scanf_s("%s", moji,STRLEN);//セキュリティ向上 VSさんが言うこと聞かないので
106
+
107
+ if (strcmp(moji, "end") == 0) {
108
+ free_stack();
109
+ return 0;
110
+ }
111
+
112
+
113
+ else {
114
+ if (strlen(moji) >= STRLEN) {
115
+ puts("文字長ながすぎんよ");
116
+ continue;
117
+ }
118
+
119
+ for (i = 0; moji[i]; ++i)push(moji[i]);//文字が入力されてるだけ格納
120
+
121
+ while (listhead != NULL) {//listheadに変更
122
+ printf("%c", pop());
123
+ print_stack();
124
+ }
125
+
126
+ }
127
+ }
128
+ }
129
+
130
+ char pop() {
131
+ char popdata;
132
+ popdata = listhead->c;
133
+ stack = listhead;
134
+ listhead = listhead->next;
135
+ free(stack);
136
+ return popdata;
137
+ }
138
+ void free_stack() {
139
+ if (listhead != NULL)
140
+ free(stack);
141
+ }
142
+ void push(char ic) {//一文字読み込みなら配列じゃなくていい
143
+ stack = (struct cell *)malloc(sizeof(struct cell));
144
+ stack->c = ic;
145
+ stack->next = listhead;
146
+ listhead = stack;
147
+ }
148
+ void print_stack() {
149
+ stack = listhead;
150
+ while (stack != NULL) {
151
+ printf("->%c", stack->c);
152
+ stack = stack->next;
153
+ }
154
+ printf("\n");
155
+ }
77
156
  ```