回答編集履歴
3
説明を追加
test
CHANGED
@@ -347,3 +347,33 @@
|
|
347
347
|
}
|
348
348
|
|
349
349
|
```
|
350
|
+
|
351
|
+
**追記2**
|
352
|
+
|
353
|
+
> 1つ質問ですが、変数 i は2進数として扱っているのですか?
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
```text
|
358
|
+
|
359
|
+
{10, 20, 30} {1, 1, 1} 7
|
360
|
+
|
361
|
+
{10, 20 } {1, 1, 0} 6
|
362
|
+
|
363
|
+
{10, 30} {1, 0, 1} 5
|
364
|
+
|
365
|
+
{10 } {1, 0, 0} 4
|
366
|
+
|
367
|
+
{ 20, 30} {0, 1, 1} 3
|
368
|
+
|
369
|
+
{ 20 } {0, 1, 0} 2
|
370
|
+
|
371
|
+
{ 30} {0, 0, 1} 1
|
372
|
+
|
373
|
+
{ } {0, 0, 0} 0
|
374
|
+
|
375
|
+
```
|
376
|
+
|
377
|
+
表示するものを 1、表示しないものを 0 とすると、
|
378
|
+
|
379
|
+
それは 2進数とみなして、7~0 の値になります。
|
2
全ソースを追加
test
CHANGED
@@ -107,3 +107,243 @@
|
|
107
107
|
return 1;
|
108
108
|
|
109
109
|
```
|
110
|
+
|
111
|
+
**追記**
|
112
|
+
|
113
|
+
```C
|
114
|
+
|
115
|
+
#include<stdio.h>
|
116
|
+
|
117
|
+
#include<stdlib.h>
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
typedef struct LISTNODE
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
int data;
|
126
|
+
|
127
|
+
int hantei;
|
128
|
+
|
129
|
+
struct LISTNODE *pNext;
|
130
|
+
|
131
|
+
}ListNode;
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
ListNode head;
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
ListNode* getNode(int num){
|
140
|
+
|
141
|
+
int i;
|
142
|
+
|
143
|
+
ListNode *retNode=&head;
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
for(i=0;i<num;i++){
|
148
|
+
|
149
|
+
retNode=retNode->pNext;
|
150
|
+
|
151
|
+
if(retNode==NULL){
|
152
|
+
|
153
|
+
return 0;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
return retNode;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
int checkNode(int num){
|
166
|
+
|
167
|
+
ListNode *currentNode, *pCheckNode=getNode(num);
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
currentNode=head.pNext;
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
while(currentNode!=pCheckNode){
|
176
|
+
|
177
|
+
if(currentNode->data==pCheckNode->data)
|
178
|
+
|
179
|
+
return 1;
|
180
|
+
|
181
|
+
currentNode=currentNode->pNext;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
return 0;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
void deleteNode(int num){
|
192
|
+
|
193
|
+
ListNode *targetNode=getNode(num);
|
194
|
+
|
195
|
+
ListNode *pPrevNode=getNode(num-1);
|
196
|
+
|
197
|
+
pPrevNode -> pNext =targetNode -> pNext;
|
198
|
+
|
199
|
+
free(targetNode);
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
int swapNode(int num1, int num2){
|
206
|
+
|
207
|
+
ListNode *node1=getNode(num1), *node2=getNode(num2);
|
208
|
+
|
209
|
+
ListNode *Prenode1=getNode(num1-1), *Prenode2=getNode(num2-1);
|
210
|
+
|
211
|
+
ListNode *tmpNext;
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if(node1->pNext!=node2){
|
216
|
+
|
217
|
+
tmpNext=node1->pNext;
|
218
|
+
|
219
|
+
node1->pNext=node2->pNext;
|
220
|
+
|
221
|
+
node2->pNext=tmpNext;
|
222
|
+
|
223
|
+
Prenode1->pNext=node2;
|
224
|
+
|
225
|
+
Prenode2->pNext=node1;
|
226
|
+
|
227
|
+
}else{
|
228
|
+
|
229
|
+
node1->pNext=node2->pNext;
|
230
|
+
|
231
|
+
node2->pNext=node1;
|
232
|
+
|
233
|
+
Prenode1->pNext=node2;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
return 1;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
void printNode(ListNode *currentNode){
|
244
|
+
|
245
|
+
if(currentNode!=NULL){
|
246
|
+
|
247
|
+
if(currentNode->hantei!=1){
|
248
|
+
|
249
|
+
printf("%d, ",currentNode->data);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
printNode(currentNode->pNext);
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
int addNode(){
|
262
|
+
|
263
|
+
ListNode *newNode, *pCheckNode, *pNextNode;
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
FILE *fp=fopen("test3.txt","r");
|
268
|
+
|
269
|
+
if(!fp) return 0;
|
270
|
+
|
271
|
+
int k,c=0;
|
272
|
+
|
273
|
+
if (fscanf(fp, "%d:", &k) != 1) return 0;
|
274
|
+
|
275
|
+
for (int i = 1; i <= k; i++) {
|
276
|
+
|
277
|
+
//printf("a[%d] = %d\n", i, a[i]);
|
278
|
+
|
279
|
+
ListNode *pPrevNode=getNode(c);
|
280
|
+
|
281
|
+
newNode=(ListNode*)malloc(sizeof(ListNode));
|
282
|
+
|
283
|
+
if (fscanf(fp, "%d", &newNode->data) != 1) return 0;
|
284
|
+
|
285
|
+
newNode->pNext=pPrevNode->pNext;
|
286
|
+
|
287
|
+
pPrevNode->pNext=newNode;
|
288
|
+
|
289
|
+
//printf("%d\n",checkNode(c+1));
|
290
|
+
|
291
|
+
if(checkNode(c+1)==1){
|
292
|
+
|
293
|
+
deleteNode(c+1);
|
294
|
+
|
295
|
+
continue;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
c++;
|
300
|
+
|
301
|
+
//printf("c=%d\n",c);
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
fclose(fp);
|
308
|
+
|
309
|
+
for (int i = 1 << c; --i >= 0; ) {
|
310
|
+
|
311
|
+
pCheckNode = head.pNext;
|
312
|
+
|
313
|
+
printf("{");
|
314
|
+
|
315
|
+
const char *sep = "";
|
316
|
+
|
317
|
+
for (int j = c; --j >= 0; ) {
|
318
|
+
|
319
|
+
if (i >> j & 1) {
|
320
|
+
|
321
|
+
printf("%s%d", sep, pCheckNode->data);
|
322
|
+
|
323
|
+
sep = " , ";
|
324
|
+
|
325
|
+
}
|
326
|
+
|
327
|
+
pCheckNode = pCheckNode->pNext;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
printf("}\n");
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
return 1;
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
int main(void){
|
342
|
+
|
343
|
+
addNode();
|
344
|
+
|
345
|
+
return 0;
|
346
|
+
|
347
|
+
}
|
348
|
+
|
349
|
+
```
|
1
修正
test
CHANGED
@@ -16,15 +16,9 @@
|
|
16
16
|
|
17
17
|
- newNode->data=a[i];
|
18
18
|
|
19
|
-
+ if (fscanf(fp, "
|
19
|
+
+ if (fscanf(fp, "%d", &newNode->data) != 1) return 0;
|
20
20
|
|
21
21
|
newNode->pNext=pPrevNode->pNext;
|
22
|
-
|
23
|
-
}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
fclose(fp);
|
28
22
|
|
29
23
|
|
30
24
|
|
@@ -110,8 +104,6 @@
|
|
110
104
|
|
111
105
|
+ }
|
112
106
|
|
113
|
-
+
|
114
|
-
|
115
107
|
return 1;
|
116
108
|
|
117
109
|
```
|