質問編集履歴
1
解答を参考に書き直したため
title
CHANGED
File without changes
|
body
CHANGED
@@ -140,4 +140,126 @@
|
|
140
140
|
return(0);
|
141
141
|
}
|
142
142
|
|
143
|
+
```
|
144
|
+
|
145
|
+
(追記ソースコード)
|
146
|
+
```
|
147
|
+
#include <stdio.h>
|
148
|
+
#include <stdlib.h>
|
149
|
+
#include<string.h>
|
150
|
+
|
151
|
+
/* 構造体 cell の宣言 */
|
152
|
+
struct cell {
|
153
|
+
int num;
|
154
|
+
struct cell *next;
|
155
|
+
};
|
156
|
+
typedef struct cell cell;
|
157
|
+
|
158
|
+
/**********************/
|
159
|
+
/* enq 操作を行う関数 */
|
160
|
+
/**********************/
|
161
|
+
void enq(struct cell **head, struct cell **tail, int val)
|
162
|
+
{
|
163
|
+
cell *p;
|
164
|
+
p=(cell*)malloc(sizeof( cell ));
|
165
|
+
p->num = val;
|
166
|
+
p->next = NULL;
|
167
|
+
if(head == NULL){
|
168
|
+
head = &p;
|
169
|
+
tail = &p;
|
170
|
+
}
|
171
|
+
else{
|
172
|
+
(*tail)->next = &p;
|
173
|
+
tail = &p;
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
/**********************/
|
178
|
+
/* deq 操作を行う関数 */
|
179
|
+
/**********************/
|
180
|
+
int deq(struct cell **head){
|
181
|
+
cell *p;
|
182
|
+
int deqdata;
|
183
|
+
deqdata = (*head)->num;
|
184
|
+
p = head;
|
185
|
+
*head = (*head)->next;
|
186
|
+
free(p);
|
187
|
+
return deqdata;
|
188
|
+
}
|
189
|
+
|
190
|
+
/******************************/
|
191
|
+
/* キューの中身を表示する関数 */
|
192
|
+
/******************************/
|
193
|
+
void print_queue(struct cell *head)
|
194
|
+
{
|
195
|
+
cell *p = head;
|
196
|
+
while(p != NULL){
|
197
|
+
printf(" -> %d" , p->num);
|
198
|
+
p=p->next;
|
199
|
+
}
|
200
|
+
printf("\n");
|
201
|
+
|
202
|
+
}
|
203
|
+
|
204
|
+
/******************************/
|
205
|
+
/* キューの中身を空にする関数 */
|
206
|
+
/******************************/
|
207
|
+
void free_queue(struct cell **head)
|
208
|
+
{
|
209
|
+
cell **q;
|
210
|
+
cell **p;
|
211
|
+
&p = head;
|
212
|
+
while(p != NULL){
|
213
|
+
q=(*p)->next;
|
214
|
+
free(p);
|
215
|
+
p=q;
|
216
|
+
}
|
217
|
+
printf("プログラムを終了します\n");
|
218
|
+
}
|
219
|
+
|
220
|
+
int main(void)
|
221
|
+
{
|
222
|
+
int indata;
|
223
|
+
char in[100];
|
224
|
+
struct cell *listhead=NULL; /* リストの先頭を指すポインタ.データが無い場合はNULLとする. */
|
225
|
+
struct cell *listtail; /* リストの末端を指すポインタ */
|
226
|
+
while(1){
|
227
|
+
printf("操作を入力してください(enq/deq/end) ==>");
|
228
|
+
scanf("%s" , in);
|
229
|
+
//end入力時
|
230
|
+
if(strcmp(in , "end") == 0){
|
231
|
+
free(&listhead);
|
232
|
+
}
|
233
|
+
|
234
|
+
//enq入力時
|
235
|
+
if(strcmp(in , "enq") == 0){
|
236
|
+
printf("追加する値を入力して下さい ==>");
|
237
|
+
scanf("%d" , &indata);
|
238
|
+
enq(&listhead , &listtail , indata);
|
239
|
+
print_queue(listhead);
|
240
|
+
if(listhead == NULL){
|
241
|
+
listtail = NULL;
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
//deq入力時
|
246
|
+
if(strcmp(in , "deq") == 0){
|
247
|
+
if(listhead == NULL){
|
248
|
+
printf("キューにデータはありません\n");
|
249
|
+
free_queue(&listhead);
|
250
|
+
break;
|
251
|
+
}
|
252
|
+
else{
|
253
|
+
printf("%dはキューから消去されました\n" , deq(&listhead));
|
254
|
+
print_queue(listhead);
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
return(0);
|
263
|
+
}
|
264
|
+
|
143
265
|
```
|