###前提
線形リストの実装の課題です。問題文は以下の通りです。
LIST-INSERT(L,x)を用いるなどして、1~10までの整数をランダム(手動で可)に並べた線形リストを作成し、先頭から順に要素を書き出して、正しく線形リストができていることを示してください。
###課題のヒントで与えられた疑似コード
連結リストの疑似コードなので線形リストとは少し異なると思います。 LIST-SEARCH(????,????) ???? ← ????.ℎ???????????? while ???? ≠ NIL かつ ????.???????????? ≠ ???? ???? = ????.???????????????? return ???? LIST-INSERT(????,????) ????.???????????????? = ????.ℎ???????????? if ????.ℎ???????????? ≠ NIL ????.ℎ????????????.???????????????? = ???? ????.ℎ???????????? = ???? ????.????????????????=NIL LIST-DELETE(????,????) if ????.???????????????? ≠ NIL ????.????????????????.???????????????? = ????.???????????????? else ????.ℎ???????????? = ????.???????????????? if ????.???????????????? ≠ NIL ????.????????????????.???????????????? = ????.????????????????
###試したこと
色々なサイトを参考にして実装しました。
Python3.5.1
1class Linear_List(object): 2 def __init__(self, init_elems=[]): 3 self.head = ListElement() 4 self.head.next = None 5 for ie in init_elems: 6 self.append(ie) 7 8 def __contains__(self, value): 9 sp = self.head 10 while sp.next is not None: 11 sp = sp.next 12 if sp.value == value: 13 return True 14 else: 15 return False 16 17 def List_Search(self, index): 18 19 if not (isinstance(index, int)): 20 raise TypeError 21 if not (-1 * len(self) <= index < len(self)): 22 raise IndexError 23 if index < 0: 24 index += len(self) 25 sp = self.head 26 for i in range(index): 27 sp = sp.next 28 return sp.next 29 30 def List_Insert(self, index, value): 31 if not (isinstance(index, int)): 32 raise TypeError 33 if not (-1 * len(self) <= index < len(self)): 34 raise IndexError 35 if index < 0: 36 index += len(self) 37 sp = self.head 38 for i in range(index): 39 sp = sp.next 40 sp.next.value = value 41 42 def List_Delete(self, index): 43 if not (isinstance(index, int)): 44 raise TypeError 45 if not (-1 * len(self) <= index < len(self)): 46 raise IndexError 47 if index < 0: 48 index += len(self) 49 sp = self.head 50 for i in range(index): 51 sp = sp.next 52 n = sp.next 53 sp.next = sp.next.next 54 del n 55 56 def __len__(self): 57 sp = self.head 58 count = 0 59 while sp.next is not None: 60 sp = sp.next 61 count += 1 62 return count 63 64 def __iter__(self): 65 return Linear_List_Iterator(self.head) 66 67 def __str__(self): 68 sp = self.head 69 array = [] 70 while sp.next is not None: 71 sp = sp.next 72 array.append(sp.value) 73 return 'Linear_List: ' + '[' + ', '.join(map(str, array)) + ']' 74 75 def __repr__(self): 76 sp = self.head 77 array = [] 78 while sp.next is not None: 79 sp = sp.next 80 array.append(sp.value) 81 return repr(array) 82 83 def append(self, value): 84 self.insert(len(self), value) 85 86 def insert(self, index, value): 87 if not (isinstance(index, int)): 88 raise TypeError 89 if not (-1 * len(self) <= index <= len(self)): 90 raise IndexError 91 if index < 0: 92 index += len(self) 93 sp = self.head 94 for i in range(index): 95 sp = sp.next 96 n = ListElement(value=value) 97 n.next = sp.next 98 sp.next = n 99 100class ListElement(object): 101 def __init__(self, value=None): 102 self.value = value 103 self.next = None 104 105 def __str__(self): 106 return str(self.value) 107 108 def __repr__(self): 109 return repr(self.value) 110 111class Linear_List_Iterator(object): 112 def __init__(self, head): 113 self.head = head 114 self.node = self.head.next 115 116 def next(self): 117 if self.node is None: 118 raise StopIteration 119 else: 120 val = self.node.value 121 self.node = self.node.next 122 return val 123 124if __name__ == '__main__': 125 import doctest 126 doctest.testmod() 127 128""" 129↓ランダムに数字を入れたリストを作成 130""" 131a = Linear_List() 132 133a.append(3) 134a.append(7) 135a.append(2) 136a.append(4) 137a.append(5) 138a.append(10) 139a.append(9) 140a.append(6) 141a.append(1) 142a.append(8) 143 144""" 145↓リストの要素を順に表示 146""" 147for i in range(len(a)): 148 print(a[i])
###発生している問題・エラーメッセージ
TypeError: 'Linear_List' object does not support indexing
###困っていること
最後の行で、リストの要素をひとつずつ順に表示させたいのですが、
エラーになってしまって困っています。まず、なにが原因でエラーが出てしまうのかを教えていただきたいです。その解決方法も教えてくださると助かります。お願いします。
###実行結果
Linear_List: [3, 7, 2, 4, 5, 10, 9, 6, 1, 8] Traceback (most recent call last): File "C:\Users\List_Insert.py", line 143, in <module> print(a[1]) TypeError: 'Linear_List' object does not support indexing >>>
###補足情報(言語/FW/ツール等のバージョンなど)
使用ツール:Python3.5.1
至らない点が多々あり、申し訳ございませんでした。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/17 11:28