teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

修正

2021/09/27 12:27

投稿

MIMY
MIMY

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,15 +1,16 @@
1
- 下記のアルゴズムでリストから index 番目のセルの値を取り出し、存在しない index が指定された場合はエラーメッセージを表示するようにしたいのですが、def get(self, index)が反応しません。
1
+ リストから index 番目のセルの値を取り出し、存在しない index が指定された場合はエラーメッセージを表示するようにしたいのですが、def get(self, index)が反応しません。
2
2
  どう修正すれば良いでしょうか?
3
3
  ```ここに言語を入力
4
- class Node: #Noneデータの入ったNodeクラスオブジェクトを作成
4
+ class Node:
5
- def __init__(self, value): #初期化
5
+ def init(self, value):
6
6
  self.value = value
7
- self.next = None
7
+ self.next = None
8
+
8
- class List: #線形リスト
9
+ class List:
9
- def __init__(self): #初期化
10
+ def init(self):
10
- self.head = Node(None) #リストの先頭にNoneを置く
11
+ self.head = Node(None)
11
12
 
12
- def insert(self, value): #valueを昇順で挿入
13
+ def insert(self, value):
13
14
  previous = self.head
14
15
  current = previous.next
15
16
  while current and value > current.value:

4

編集

2021/09/27 12:27

投稿

MIMY
MIMY

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- Python アルゴリズム 線形リスト
1
+ アルゴリズム リスト
body CHANGED
@@ -18,27 +18,7 @@
18
18
  node = Node(value)
19
19
  node.next = current
20
20
  previous.next = node
21
-
22
- def delete(self, value): #削除するvalueを検索して削除
21
+
23
- previous = self.head
24
- current = previous.next
25
- while current:
26
- if current.value == value:
27
- break
28
- previous = current
29
- current = current.next
30
- if not current: #削除する値が存在しない時
31
- print("[*] Data not found")
32
- return
33
- previous.next = current.next
34
- current = None
35
-
36
- def show(self): #リストの中身を順番に表示
37
- tmp = self.head.next
38
- while tmp:
39
- print(tmp.value)
40
- tmp = tmp.next
41
-
42
22
  def get(self, index):
43
23
  tmp = self.head
44
24
  while tmp:
@@ -48,16 +28,6 @@
48
28
  return "[*] Data not found"
49
29
 
50
30
  l = List()
51
- print('insert 3, 5, 1...')
52
- l.insert(3)
53
- l.insert(5)
54
- l.insert(1)
55
- print('print data')
56
- l.show()
57
- print('delete 3...')
58
- l.delete(3)
59
- print('print data')
60
- l.show()
61
31
  l.get(5)
62
32
  l.get(3)
63
33
  ```

3

意図的に内容を抹消する行為にあたるため

2021/09/27 12:22

投稿

MIMY
MIMY

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- 削除依頼中です。。。。
1
+ Python アルゴリズム 線形リスト
body CHANGED
@@ -1,5 +1,63 @@
1
+ 下記のアルゴリズムでリストから index 番目のセルの値を取り出し、存在しない index が指定された場合はエラーメッセージを表示するようにしたいのですが、def get(self, index)が反応しません。
1
- 削除依頼中で。。。。。
2
+ どう修正れば良いでしょうか?
2
-
3
3
  ```ここに言語を入力
4
-
4
+ class Node: #Noneデータの入ったNodeクラスオブジェクトを作成
5
+ def __init__(self, value): #初期化
6
+ self.value = value
7
+ self.next = None
8
+ class List: #線形リスト
9
+ def __init__(self): #初期化
10
+ self.head = Node(None) #リストの先頭にNoneを置く
11
+
12
+ def insert(self, value): #valueを昇順で挿入
13
+ previous = self.head
14
+ current = previous.next
15
+ while current and value > current.value:
16
+ previous = current
17
+ current = current.next
18
+ node = Node(value)
19
+ node.next = current
20
+ previous.next = node
21
+
22
+ def delete(self, value): #削除するvalueを検索して削除
23
+ previous = self.head
24
+ current = previous.next
25
+ while current:
26
+ if current.value == value:
27
+ break
28
+ previous = current
29
+ current = current.next
30
+ if not current: #削除する値が存在しない時
31
+ print("[*] Data not found")
32
+ return
33
+ previous.next = current.next
34
+ current = None
35
+
36
+ def show(self): #リストの中身を順番に表示
37
+ tmp = self.head.next
38
+ while tmp:
39
+ print(tmp.value)
40
+ tmp = tmp.next
41
+
42
+ def get(self, index):
43
+ tmp = self.head
44
+ while tmp:
45
+ if tmp.value == index:
46
+ return index
47
+ tmp = tmp.next
48
+ return "[*] Data not found"
49
+
50
+ l = List()
51
+ print('insert 3, 5, 1...')
52
+ l.insert(3)
53
+ l.insert(5)
54
+ l.insert(1)
55
+ print('print data')
56
+ l.show()
57
+ print('delete 3...')
58
+ l.delete(3)
59
+ print('print data')
60
+ l.show()
61
+ l.get(5)
62
+ l.get(3)
5
63
  ```

2

削除依頼中

2021/09/27 02:08

投稿

退会済みユーザー
title CHANGED
@@ -1,1 +1,1 @@
1
- Python アルゴリズム 線形リスト
1
+ 削除依頼中です。。。。
body CHANGED
@@ -1,4 +1,4 @@
1
- 下記のアルゴリズムリストから index 番目のセルの値を取り出し
1
+ 削除依頼中す。。。。。
2
2
 
3
3
  ```ここに言語を入力
4
4
 

1

修正

2021/09/26 14:40

投稿

MIMY
MIMY

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,65 +1,5 @@
1
- 下記のアルゴリズムでリストから index 番目のセルの値を取り出し、存在しない index が指定された場合はエラーメッセージを表示するようにしたいのですが、def get(self, index)が反応しません。
1
+ 下記のアルゴリズムでリストから index 番目のセルの値を取り出し
2
- どう修正すれば良いでしょうか?
3
2
 
4
3
  ```ここに言語を入力
5
- class Node: #Noneデータの入ったNodeクラスオブジェクトを作成
6
- def __init__(self, value): #初期化
7
- self.value = value
8
- self.next = None
9
4
 
10
- class List: #線形リスト
11
- def __init__(self): #初期化
12
- self.head = Node(None) #リストの先頭にNoneを置く
13
-
14
- def insert(self, value): #valueを昇順で挿入
15
- previous = self.head
16
- current = previous.next
17
- while current and value > current.value:
18
- previous = current
19
- current = current.next
20
- node = Node(value)
21
- node.next = current
22
- previous.next = node
23
-
24
- def delete(self, value): #削除するvalueを検索して削除
25
- previous = self.head
26
- current = previous.next
27
- while current:
28
- if current.value == value:
29
- break
30
- previous = current
31
- current = current.next
32
- if not current: #削除する値が存在しない時
33
- print("[*] Data not found")
34
- return
35
- previous.next = current.next
36
- current = None
37
-
38
- def show(self): #リストの中身を順番に表示
39
- tmp = self.head.next
40
- while tmp:
41
- print(tmp.value)
42
- tmp = tmp.next
43
-
44
- def get(self, index):
45
- tmp = self.head
46
- while tmp:
47
- if tmp.value == index:
48
- return index
49
- tmp = tmp.next
50
- return "[*] Data not found"
51
-
52
- l = List()
53
- print('insert 3, 5, 1...')
54
- l.insert(3)
55
- l.insert(5)
56
- l.insert(1)
57
- print('print data')
58
- l.show()
59
- print('delete 3...')
60
- l.delete(3)
61
- print('print data')
62
- l.show()
63
- l.get(5)
64
- l.get(3)
65
5
  ```