質問編集履歴
1
修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
これは何が原因なのでしょうか。分かる方いましたら、回答をお願いします。
|
10
10
|
|
11
|
+
このプログラム自体は単方向リストを用いて、要素の中でかぶっていないものを昇順に表示するものとなっています。
|
12
|
+
|
11
13
|
|
12
14
|
|
13
15
|
### 表示結果
|
@@ -28,7 +30,133 @@
|
|
28
30
|
|
29
31
|
```c
|
30
32
|
|
33
|
+
#include<stdio.h>
|
34
|
+
|
35
|
+
#include<stdlib.h>
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
#define MAX 256
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
typedef struct LISTNODE
|
44
|
+
|
45
|
+
{
|
46
|
+
|
47
|
+
int data;
|
48
|
+
|
49
|
+
struct LISTNODE *pNext;
|
50
|
+
|
51
|
+
struct LISTNODE *pPrev;
|
52
|
+
|
53
|
+
}ListNode;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
ListNode head;
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
ListNode* getNode(int num){
|
62
|
+
|
63
|
+
int i;
|
64
|
+
|
65
|
+
ListNode *retNode=&head;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
for(i=0;i<num;i++){
|
70
|
+
|
71
|
+
retNode=retNode->pNext;
|
72
|
+
|
73
|
+
if(retNode==NULL){
|
74
|
+
|
75
|
+
return 0;
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
return retNode;
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
int checkNode(int num1){
|
88
|
+
|
89
|
+
ListNode *currentNode, *pCheckNode=getNode(num1);
|
90
|
+
|
91
|
+
currentNode=head.pNext;
|
92
|
+
|
93
|
+
while(currentNode!=pCheckNode){
|
94
|
+
|
95
|
+
if(currentNode->data>pCheckNode->data)
|
96
|
+
|
97
|
+
return 1;
|
98
|
+
|
99
|
+
currentNode=currentNode->pNext;
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
return 0;
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
int swapNode(int num1, int num2){
|
110
|
+
|
111
|
+
ListNode *node1=getNode(num1), *node2=getNode(num2);
|
112
|
+
|
113
|
+
ListNode *tmpNext, *tmpPrev;
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
if(node1->pNext!=node2){
|
118
|
+
|
119
|
+
tmpNext=node1->pNext;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
node1->pNext=node2->pNext;
|
124
|
+
|
125
|
+
node1->pPrev->pNext=node2;
|
126
|
+
|
127
|
+
node2->pPrev->pNext=node1;
|
128
|
+
|
129
|
+
node2->pNext=tmpNext;
|
130
|
+
|
131
|
+
}else{
|
132
|
+
|
133
|
+
node1->pPrev->pNext=node2;
|
134
|
+
|
135
|
+
node1->pNext=node2->pNext;
|
136
|
+
|
137
|
+
node2->pNext=node1;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
return 1;
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
int addNode(){
|
148
|
+
|
149
|
+
int k,c;
|
150
|
+
|
151
|
+
ListNode *currentNode=head.pNext;
|
152
|
+
|
153
|
+
ListNode *newNode, *pCheckNode, *pNextNode;
|
154
|
+
|
155
|
+
newNode=(ListNode*)malloc(sizeof(ListNode));
|
156
|
+
|
157
|
+
|
158
|
+
|
31
|
-
FILE *fp=fopen("test1.txt","r");
|
159
|
+
FILE *fp=fopen("test1.txt","r"); //問題の部分
|
32
160
|
|
33
161
|
k=fgetc(fp);
|
34
162
|
|
@@ -64,6 +192,78 @@
|
|
64
192
|
|
65
193
|
}
|
66
194
|
|
195
|
+
|
196
|
+
|
197
|
+
fclose(fp);
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
int nodeNum;
|
202
|
+
|
203
|
+
ListNode *pEndNode=getNode(c);
|
204
|
+
|
205
|
+
while(head.pNext!=pEndNode){
|
206
|
+
|
207
|
+
pCheckNode=head.pNext;
|
208
|
+
|
209
|
+
nodeNum=0;
|
210
|
+
|
211
|
+
while(pCheckNode->pNext!=NULL){
|
212
|
+
|
213
|
+
pNextNode=pCheckNode->pNext;
|
214
|
+
|
215
|
+
nodeNum++;
|
216
|
+
|
217
|
+
if(pCheckNode->data>pNextNode->data)
|
218
|
+
|
219
|
+
swapNode(nodeNum, nodeNum+1);
|
220
|
+
|
221
|
+
else
|
222
|
+
|
223
|
+
pCheckNode=pCheckNode->pNext;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
pEndNode=pEndNode->pPrev;
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
printf("<");
|
234
|
+
|
235
|
+
for(;currentNode!=NULL;){
|
236
|
+
|
237
|
+
if(currentNode->pNext==NULL)
|
238
|
+
|
239
|
+
printf("%d>",currentNode->data);
|
240
|
+
|
241
|
+
else
|
242
|
+
|
243
|
+
printf("%d, ",currentNode->data);
|
244
|
+
|
245
|
+
currentNode=currentNode->pNext;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
printf("\nNumber of elements: %d",c);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
return 1;
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
int main(void){
|
260
|
+
|
261
|
+
addNode();
|
262
|
+
|
263
|
+
return 0;
|
264
|
+
|
265
|
+
}
|
266
|
+
|
67
267
|
```
|
68
268
|
|
69
269
|
###test1.txt
|