質問編集履歴
1
質問内容など先ほど不足していた部分を明確にいたしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,11 +1,15 @@
|
|
1
|
-
###前提
|
1
|
+
###前提
|
2
2
|
|
3
3
|
線形リストの実装の課題です。問題文は以下の通りです。
|
4
4
|
|
5
|
-
|
5
|
+
```
|
6
6
|
|
7
7
|
LIST-INSERT(L,x)を用いるなどして、1~10までの整数をランダム(手動で可)に並べた線形リストを作成し、先頭から順に要素を書き出して、正しく線形リストができていることを示してください。
|
8
8
|
|
9
|
+
```
|
10
|
+
|
11
|
+
|
12
|
+
|
9
13
|
|
10
14
|
|
11
15
|
###課題のヒントで与えられた疑似コード
|
@@ -56,6 +60,310 @@
|
|
56
60
|
|
57
61
|
```
|
58
62
|
|
63
|
+
###試したこと
|
64
|
+
|
65
|
+
色々なサイトを参考にして実装しました。
|
66
|
+
|
67
|
+
```Python3.5.1
|
68
|
+
|
69
|
+
class Linear_List(object):
|
70
|
+
|
71
|
+
def __init__(self, init_elems=[]):
|
72
|
+
|
73
|
+
self.head = ListElement()
|
74
|
+
|
75
|
+
self.head.next = None
|
76
|
+
|
77
|
+
for ie in init_elems:
|
78
|
+
|
79
|
+
self.append(ie)
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
def __contains__(self, value):
|
84
|
+
|
85
|
+
sp = self.head
|
86
|
+
|
87
|
+
while sp.next is not None:
|
88
|
+
|
89
|
+
sp = sp.next
|
90
|
+
|
91
|
+
if sp.value == value:
|
92
|
+
|
93
|
+
return True
|
94
|
+
|
95
|
+
else:
|
96
|
+
|
97
|
+
return False
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
def List_Search(self, index):
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
if not (isinstance(index, int)):
|
106
|
+
|
107
|
+
raise TypeError
|
108
|
+
|
109
|
+
if not (-1 * len(self) <= index < len(self)):
|
110
|
+
|
111
|
+
raise IndexError
|
112
|
+
|
113
|
+
if index < 0:
|
114
|
+
|
115
|
+
index += len(self)
|
116
|
+
|
117
|
+
sp = self.head
|
118
|
+
|
119
|
+
for i in range(index):
|
120
|
+
|
121
|
+
sp = sp.next
|
122
|
+
|
123
|
+
return sp.next
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
def List_Insert(self, index, value):
|
128
|
+
|
129
|
+
if not (isinstance(index, int)):
|
130
|
+
|
131
|
+
raise TypeError
|
132
|
+
|
133
|
+
if not (-1 * len(self) <= index < len(self)):
|
134
|
+
|
135
|
+
raise IndexError
|
136
|
+
|
137
|
+
if index < 0:
|
138
|
+
|
139
|
+
index += len(self)
|
140
|
+
|
141
|
+
sp = self.head
|
142
|
+
|
143
|
+
for i in range(index):
|
144
|
+
|
145
|
+
sp = sp.next
|
146
|
+
|
147
|
+
sp.next.value = value
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
def List_Delete(self, index):
|
152
|
+
|
153
|
+
if not (isinstance(index, int)):
|
154
|
+
|
155
|
+
raise TypeError
|
156
|
+
|
157
|
+
if not (-1 * len(self) <= index < len(self)):
|
158
|
+
|
159
|
+
raise IndexError
|
160
|
+
|
161
|
+
if index < 0:
|
162
|
+
|
163
|
+
index += len(self)
|
164
|
+
|
165
|
+
sp = self.head
|
166
|
+
|
167
|
+
for i in range(index):
|
168
|
+
|
169
|
+
sp = sp.next
|
170
|
+
|
171
|
+
n = sp.next
|
172
|
+
|
173
|
+
sp.next = sp.next.next
|
174
|
+
|
175
|
+
del n
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
def __len__(self):
|
180
|
+
|
181
|
+
sp = self.head
|
182
|
+
|
183
|
+
count = 0
|
184
|
+
|
185
|
+
while sp.next is not None:
|
186
|
+
|
187
|
+
sp = sp.next
|
188
|
+
|
189
|
+
count += 1
|
190
|
+
|
191
|
+
return count
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
def __iter__(self):
|
196
|
+
|
197
|
+
return Linear_List_Iterator(self.head)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
def __str__(self):
|
202
|
+
|
203
|
+
sp = self.head
|
204
|
+
|
205
|
+
array = []
|
206
|
+
|
207
|
+
while sp.next is not None:
|
208
|
+
|
209
|
+
sp = sp.next
|
210
|
+
|
211
|
+
array.append(sp.value)
|
212
|
+
|
213
|
+
return 'Linear_List: ' + '[' + ', '.join(map(str, array)) + ']'
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
def __repr__(self):
|
218
|
+
|
219
|
+
sp = self.head
|
220
|
+
|
221
|
+
array = []
|
222
|
+
|
223
|
+
while sp.next is not None:
|
224
|
+
|
225
|
+
sp = sp.next
|
226
|
+
|
227
|
+
array.append(sp.value)
|
228
|
+
|
229
|
+
return repr(array)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
def append(self, value):
|
234
|
+
|
235
|
+
self.insert(len(self), value)
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
def insert(self, index, value):
|
240
|
+
|
241
|
+
if not (isinstance(index, int)):
|
242
|
+
|
243
|
+
raise TypeError
|
244
|
+
|
245
|
+
if not (-1 * len(self) <= index <= len(self)):
|
246
|
+
|
247
|
+
raise IndexError
|
248
|
+
|
249
|
+
if index < 0:
|
250
|
+
|
251
|
+
index += len(self)
|
252
|
+
|
253
|
+
sp = self.head
|
254
|
+
|
255
|
+
for i in range(index):
|
256
|
+
|
257
|
+
sp = sp.next
|
258
|
+
|
259
|
+
n = ListElement(value=value)
|
260
|
+
|
261
|
+
n.next = sp.next
|
262
|
+
|
263
|
+
sp.next = n
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
class ListElement(object):
|
268
|
+
|
269
|
+
def __init__(self, value=None):
|
270
|
+
|
271
|
+
self.value = value
|
272
|
+
|
273
|
+
self.next = None
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
def __str__(self):
|
278
|
+
|
279
|
+
return str(self.value)
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def __repr__(self):
|
284
|
+
|
285
|
+
return repr(self.value)
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
class Linear_List_Iterator(object):
|
290
|
+
|
291
|
+
def __init__(self, head):
|
292
|
+
|
293
|
+
self.head = head
|
294
|
+
|
295
|
+
self.node = self.head.next
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
def next(self):
|
300
|
+
|
301
|
+
if self.node is None:
|
302
|
+
|
303
|
+
raise StopIteration
|
304
|
+
|
305
|
+
else:
|
306
|
+
|
307
|
+
val = self.node.value
|
308
|
+
|
309
|
+
self.node = self.node.next
|
310
|
+
|
311
|
+
return val
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
if __name__ == '__main__':
|
316
|
+
|
317
|
+
import doctest
|
318
|
+
|
319
|
+
doctest.testmod()
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
"""
|
324
|
+
|
325
|
+
↓ランダムに数字を入れたリストを作成
|
326
|
+
|
327
|
+
"""
|
328
|
+
|
329
|
+
a = Linear_List()
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
a.append(3)
|
334
|
+
|
335
|
+
a.append(7)
|
336
|
+
|
337
|
+
a.append(2)
|
338
|
+
|
339
|
+
a.append(4)
|
340
|
+
|
341
|
+
a.append(5)
|
342
|
+
|
343
|
+
a.append(10)
|
344
|
+
|
345
|
+
a.append(9)
|
346
|
+
|
347
|
+
a.append(6)
|
348
|
+
|
349
|
+
a.append(1)
|
350
|
+
|
351
|
+
a.append(8)
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
"""
|
356
|
+
|
357
|
+
↓リストの要素を順に表示
|
358
|
+
|
359
|
+
"""
|
360
|
+
|
361
|
+
for i in range(len(a)):
|
362
|
+
|
363
|
+
print(a[i])
|
364
|
+
|
365
|
+
```
|
366
|
+
|
59
367
|
|
60
368
|
|
61
369
|
###発生している問題・エラーメッセージ
|
@@ -66,307 +374,13 @@
|
|
66
374
|
|
67
375
|
```
|
68
376
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
###
|
377
|
+
###困っていること
|
74
|
-
|
75
|
-
|
378
|
+
|
76
|
-
|
77
|
-
```Python3.5.1
|
78
|
-
|
79
|
-
class Linear_List(object):
|
80
|
-
|
81
|
-
def __init__(self, init_elems=[]):
|
82
|
-
|
83
|
-
self.head = ListElement()
|
84
|
-
|
85
|
-
self.head.next = None
|
86
|
-
|
87
|
-
for ie in init_elems:
|
88
|
-
|
89
|
-
self.append(ie)
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
def __contains__(self, value):
|
94
|
-
|
95
|
-
sp = self.head
|
96
|
-
|
97
|
-
while sp.next is not None:
|
98
|
-
|
99
|
-
sp = sp.next
|
100
|
-
|
101
|
-
if sp.value == value:
|
102
|
-
|
103
|
-
return True
|
104
|
-
|
105
|
-
else:
|
106
|
-
|
107
|
-
return False
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
def List_Search(self, index):
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
if not (isinstance(index, int)):
|
116
|
-
|
117
|
-
raise TypeError
|
118
|
-
|
119
|
-
if not (-1 * len(self) <= index < len(self)):
|
120
|
-
|
121
|
-
raise IndexError
|
122
|
-
|
123
|
-
if index < 0:
|
124
|
-
|
125
|
-
index += len(self)
|
126
|
-
|
127
|
-
sp = self.head
|
128
|
-
|
129
|
-
for i in range(index):
|
130
|
-
|
131
|
-
sp = sp.next
|
132
|
-
|
133
|
-
return sp.next
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
def List_Insert(self, index, value):
|
138
|
-
|
139
|
-
if not (isinstance(index, int)):
|
140
|
-
|
141
|
-
raise TypeError
|
142
|
-
|
143
|
-
if not (-1 * len(self) <= index < len(self)):
|
144
|
-
|
145
|
-
raise IndexError
|
146
|
-
|
147
|
-
if index < 0:
|
148
|
-
|
149
|
-
index += len(self)
|
150
|
-
|
151
|
-
sp = self.head
|
152
|
-
|
153
|
-
for i in range(index):
|
154
|
-
|
155
|
-
sp = sp.next
|
156
|
-
|
157
|
-
sp.next.value = value
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
def List_Delete(self, index):
|
162
|
-
|
163
|
-
if not (isinstance(index, int)):
|
164
|
-
|
165
|
-
raise TypeError
|
166
|
-
|
167
|
-
if not (-1 * len(self) <= index < len(self)):
|
168
|
-
|
169
|
-
raise IndexError
|
170
|
-
|
171
|
-
if index < 0:
|
172
|
-
|
173
|
-
index += len(self)
|
174
|
-
|
175
|
-
sp = self.head
|
176
|
-
|
177
|
-
for i in range(index):
|
178
|
-
|
179
|
-
sp = sp.next
|
180
|
-
|
181
|
-
n = sp.next
|
182
|
-
|
183
|
-
sp.next = sp.next.next
|
184
|
-
|
185
|
-
del n
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
def __len__(self):
|
190
|
-
|
191
|
-
sp = self.head
|
192
|
-
|
193
|
-
count = 0
|
194
|
-
|
195
|
-
while sp.next is not None:
|
196
|
-
|
197
|
-
sp = sp.next
|
198
|
-
|
199
|
-
count += 1
|
200
|
-
|
201
|
-
return count
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
def __iter__(self):
|
206
|
-
|
207
|
-
return Linear_List_Iterator(self.head)
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
def __str__(self):
|
212
|
-
|
213
|
-
sp = self.head
|
214
|
-
|
215
|
-
array = []
|
216
|
-
|
217
|
-
while sp.next is not None:
|
218
|
-
|
219
|
-
sp = sp.next
|
220
|
-
|
221
|
-
array.append(sp.value)
|
222
|
-
|
223
|
-
return 'Linear_List: ' + '[' + ', '.join(map(str, array)) + ']'
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
def __repr__(self):
|
228
|
-
|
229
|
-
sp = self.head
|
230
|
-
|
231
|
-
array = []
|
232
|
-
|
233
|
-
while sp.next is not None:
|
234
|
-
|
235
|
-
sp = sp.next
|
236
|
-
|
237
|
-
array.append(sp.value)
|
238
|
-
|
239
|
-
return repr(array)
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
def append(self, value):
|
244
|
-
|
245
|
-
self.insert(len(self), value)
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
379
|
+
最後の行で、リストの要素をひとつずつ順に表示させたいのですが、
|
250
|
-
|
251
|
-
|
380
|
+
|
252
|
-
|
253
|
-
raise TypeError
|
254
|
-
|
255
|
-
|
381
|
+
エラーになってしまって困っています。まず、なにが原因でエラーが出てしまうのかを教えていただきたいです。その解決方法も教えてくださると助かります。お願いします。
|
256
|
-
|
257
|
-
|
382
|
+
|
258
|
-
|
259
|
-
|
383
|
+
|
260
|
-
|
261
|
-
index += len(self)
|
262
|
-
|
263
|
-
sp = self.head
|
264
|
-
|
265
|
-
for i in range(index):
|
266
|
-
|
267
|
-
sp = sp.next
|
268
|
-
|
269
|
-
n = ListElement(value=value)
|
270
|
-
|
271
|
-
n.next = sp.next
|
272
|
-
|
273
|
-
sp.next = n
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
class ListElement(object):
|
278
|
-
|
279
|
-
def __init__(self, value=None):
|
280
|
-
|
281
|
-
self.value = value
|
282
|
-
|
283
|
-
self.next = None
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
def __str__(self):
|
288
|
-
|
289
|
-
return str(self.value)
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
def __repr__(self):
|
294
|
-
|
295
|
-
return repr(self.value)
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
class Linear_List_Iterator(object):
|
300
|
-
|
301
|
-
def __init__(self, head):
|
302
|
-
|
303
|
-
self.head = head
|
304
|
-
|
305
|
-
self.node = self.head.next
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
def next(self):
|
310
|
-
|
311
|
-
if self.node is None:
|
312
|
-
|
313
|
-
raise StopIteration
|
314
|
-
|
315
|
-
else:
|
316
|
-
|
317
|
-
val = self.node.value
|
318
|
-
|
319
|
-
self.node = self.node.next
|
320
|
-
|
321
|
-
return val
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
if __name__ == '__main__':
|
326
|
-
|
327
|
-
import doctest
|
328
|
-
|
329
|
-
doctest.testmod()
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
a = Linear_List()
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
a.append(3)
|
338
|
-
|
339
|
-
a.append(7)
|
340
|
-
|
341
|
-
a.append(2)
|
342
|
-
|
343
|
-
a.append(4)
|
344
|
-
|
345
|
-
a.append(5)
|
346
|
-
|
347
|
-
a.append(10)
|
348
|
-
|
349
|
-
a.append(9)
|
350
|
-
|
351
|
-
a.append(6)
|
352
|
-
|
353
|
-
a.append(1)
|
354
|
-
|
355
|
-
a.append(8)
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
print(a)
|
360
|
-
|
361
|
-
print(a[1])
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
for i in range(len(a)):
|
366
|
-
|
367
|
-
print(a[i])
|
368
|
-
|
369
|
-
```
|
370
384
|
|
371
385
|
###実行結果
|
372
386
|
|
@@ -391,3 +405,7 @@
|
|
391
405
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
392
406
|
|
393
407
|
使用ツール:Python3.5.1
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
至らない点が多々あり、申し訳ございませんでした。
|