質問編集履歴
2
問題点とソースコードを更新した。
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,135 +9,60 @@
|
|
9
9
|
```
|
10
10
|
kotlinc-jvm.bat ".\BidirectionalLinkedList.kt" -include-runtime -d ".\BidirectionalLinkedList.jar"
|
11
11
|
```
|
12
|
-
上記でコンパイル
|
12
|
+
一旦、上記でコンパイルが通るよう修正しました。
|
13
|
-
コンパイルエラーがまだまだ多いため、
|
14
|
-
|
13
|
+
新たな問題点は3点。(編集前の4点は保留)
|
14
|
+
(5)(4)とほぼ同義だが、型引数Tの指定方法がよくわからない。とりあえずAny?に変更した。
|
15
|
+
(6)NodeクラスもBidirectionalLinkedListIteratorクラスもBidirectionalLinkedListにだけ見えるようにしたいが、単純に内部クラスにしてしまうと、~IteratorクラスからNodeクラスが見えない。
|
16
|
+
(7)IteratorとかIterableの継承方法がよくわからない。
|
15
17
|
|
16
|
-
```
|
17
|
-
BidirectionalLinkedList.kt:186:20: error: expecting 'in'
|
18
|
-
BidirectionalLinkedList.kt:187:4: error: 'return' is not allowed here
|
19
|
-
など
|
20
|
-
→(1)GetEnumerator周辺の実装方法がわからない
|
21
|
-
|
22
|
-
BidirectionalLinkedList.kt:5:8: error: unresolved reference: System
|
23
|
-
→(2)System.Collections.Genericのインポート方法がわからない
|
24
|
-
|
25
|
-
BidirectionalLinkedList.kt:13:43: error: unresolved reference: IEnumerable
|
26
|
-
→(3)上のインポートができれば解決するはず。。。
|
27
|
-
|
28
|
-
BidirectionalLinkedList.kt:24:21: error: unresolved reference: T
|
29
|
-
など
|
30
|
-
→(4)全般的に型引数Tが参照できない
|
31
|
-
```
|
32
|
-
|
33
18
|
### 該当のソースコード
|
34
19
|
|
35
|
-
注意点として、私の好みで下記2点を変更しています。
|
36
|
-
・countを追加・削除時に計算に変更。
|
37
|
-
・Insert系関数の関数名と引数の順番を変更。
|
38
20
|
```Kotlin
|
21
|
+
//https://ufcpp.net/study/algorithm/col_blist.html
|
22
|
+
|
39
23
|
package Collections;
|
40
24
|
|
41
|
-
import System.Collections.Generic;
|
42
|
-
//import java.util.Iterator;
|
43
|
-
//import java.lang.Iterable;
|
44
25
|
|
45
26
|
///<summary>
|
46
27
|
/// 双方向連結リスト。
|
47
28
|
///</summary>
|
48
29
|
///<typeparam name = "T">要素の型</typeparam>
|
49
|
-
public class BidirectionalLinkedList
|
30
|
+
public class BidirectionalLinkedList {
|
50
|
-
//public class BidirectionalLinkedList<T> : Iterator<T> {
|
51
31
|
|
52
|
-
//#region 内部クラス
|
53
|
-
|
54
|
-
///<summary>
|
55
|
-
/// 連結リストのノード。
|
56
|
-
///</summary>
|
57
|
-
public class Node {
|
58
|
-
//#region フィールド
|
59
|
-
|
60
|
-
public var value: T;
|
61
|
-
private var prev: Node;
|
62
|
-
private var next: Node;
|
63
|
-
|
64
|
-
//#endregion
|
65
|
-
//#region 初期化
|
66
|
-
|
67
|
-
internal constructor(_value: T, _prev: Node, _next: Node) {
|
68
|
-
this.value = _value;
|
69
|
-
this.prev = _prev;
|
70
|
-
this.next = _next;
|
71
|
-
}
|
72
|
-
|
73
|
-
//#endregion
|
74
|
-
//#region プロパティ
|
75
|
-
|
76
|
-
public var Value: T
|
77
|
-
get() = this.value;
|
78
|
-
set(value) {
|
79
|
-
this.value = value;
|
80
|
-
}
|
81
|
-
|
82
|
-
/// <summary>
|
83
|
-
/// 前のノード。
|
84
|
-
/// </summary>
|
85
|
-
var Previous: Node
|
86
|
-
public get() = this.prev;
|
87
|
-
internal set(value) {
|
88
|
-
this.prev = value;
|
89
|
-
}
|
90
|
-
|
91
|
-
/// <summary>
|
92
|
-
/// 次のノード。
|
93
|
-
/// </summary>
|
94
|
-
var Next: Node
|
95
|
-
public get() = this.next;
|
96
|
-
internal set(value) {
|
97
|
-
this.next = value;
|
98
|
-
}
|
99
|
-
|
100
|
-
//#endregion
|
101
|
-
}
|
102
|
-
|
103
|
-
//#endregion
|
104
32
|
//#region フィールド
|
105
33
|
|
106
34
|
public val dummy: Node;
|
107
35
|
public var count: Int;
|
108
36
|
|
109
37
|
//#endregion
|
38
|
+
|
110
39
|
//#region 初期化
|
111
40
|
|
112
41
|
public constructor() {
|
113
|
-
this.dummy = Node(
|
42
|
+
this.dummy = Node(null, null, null);
|
114
43
|
this.dummy.Next = this.dummy;
|
115
44
|
this.dummy.Previous = this.dummy;
|
116
45
|
this.count = 0;
|
117
46
|
}
|
118
47
|
|
119
48
|
//#endregion
|
49
|
+
|
120
50
|
//#region プロパティ
|
121
51
|
|
122
|
-
///<summary>
|
123
|
-
/// リストの先頭ノード。
|
124
|
-
///</summary>
|
125
|
-
public val First: Node
|
52
|
+
public val First: Node?
|
126
53
|
get() = this.dummy.Next;
|
127
54
|
|
128
|
-
///<summary>
|
129
|
-
/// リストの末尾ノード。
|
130
|
-
///</summary>
|
131
|
-
public val Last: Node
|
55
|
+
public val Last: Node?
|
132
|
-
|
56
|
+
get() = this.dummy.Previous;
|
133
57
|
|
134
58
|
///<summary>
|
135
59
|
/// リストの終端(末尾よりも後ろの番兵に当たるノード)。
|
136
60
|
///</summary>
|
137
|
-
public val End: Node
|
61
|
+
public val End: Node?
|
138
|
-
|
62
|
+
get() = this.dummy;
|
139
63
|
|
140
64
|
//#endregion
|
65
|
+
|
141
66
|
//#region 挿入・削除
|
142
67
|
|
143
68
|
///<summary>
|
@@ -146,10 +71,10 @@
|
|
146
71
|
///<param name = "e">新しい要素</param>
|
147
72
|
///<param name = "n">要素の挿入位置</param>
|
148
73
|
///<returns>新しく挿入されたノード</returns>
|
149
|
-
public fun InsertEAfterN(e:
|
74
|
+
public fun InsertEAfterN(e: Any?, n: Node?): Node? {
|
150
|
-
val m: Node = Node(e, n, n.Next);
|
75
|
+
val m: Node? = Node(e, n, n?.Next);
|
151
|
-
n.Next.Previous = m;
|
76
|
+
n?.Next?.Previous = m;
|
152
|
-
n.Next = m;
|
77
|
+
n?.Next = m;
|
153
78
|
this.count++;
|
154
79
|
return m;
|
155
80
|
}
|
@@ -160,10 +85,10 @@
|
|
160
85
|
///<param name = "e">新しい要素</param>
|
161
86
|
///<param name = "n">要素の挿入位置</param>
|
162
87
|
///<returns>新しく挿入されたノード</returns>
|
163
|
-
public fun InsertEBeforeN(e:
|
88
|
+
public fun InsertEBeforeN(e: Any?, n: Node?): Node? {
|
164
|
-
val m: Node = Node(e, n.Previous, n);
|
89
|
+
val m: Node? = Node(e, n?.Previous, n);
|
165
|
-
n.Previous.Next = m;
|
90
|
+
n?.Previous?.Next = m;
|
166
|
-
n.Previous = m;
|
91
|
+
n?.Previous = m;
|
167
92
|
this.count++;
|
168
93
|
return m;
|
169
94
|
}
|
@@ -173,7 +98,7 @@
|
|
173
98
|
///</summary>
|
174
99
|
///<param name = "elem">新しい要素</param>
|
175
100
|
///<returns>新しく挿入されたノード</returns>
|
176
|
-
public fun InsertFirst(elem:
|
101
|
+
public fun InsertFirst(elem: Any?): Node? {
|
177
102
|
return this.InsertEAfterN(elem, this.dummy);
|
178
103
|
}
|
179
104
|
|
@@ -182,23 +107,23 @@
|
|
182
107
|
///</summary>
|
183
108
|
///<param name = "elem">新しい要素</param>
|
184
109
|
///<returns>新しく挿入されたノード</returns>
|
185
|
-
public fun InsertLast(elem:
|
110
|
+
public fun InsertLast(elem: Any?): Node? {
|
186
111
|
return this.InsertEBeforeN(elem, this.dummy);
|
187
112
|
}
|
188
113
|
|
189
114
|
///<summary>
|
190
|
-
/// ノード n
|
115
|
+
/// ノード n 自身を削除。
|
191
116
|
///</summary>
|
192
117
|
///<param name = "n">要素の削除位置</param>
|
193
118
|
///<returns>削除した要素の次のノード</returns>
|
194
|
-
public fun Erase(n: Node): Node {
|
119
|
+
public fun Erase(n: Node?): Node? {
|
195
120
|
if (n == this.dummy) {
|
196
121
|
return this.dummy;
|
197
122
|
}
|
198
|
-
n.Previous.Next = n.Next;
|
123
|
+
n?.Previous?.Next = n?.Next;
|
199
|
-
n.Next.Previous = n.Previous;
|
124
|
+
n?.Next?.Previous = n?.Previous;
|
200
125
|
this.count--;
|
201
|
-
return n.Next;
|
126
|
+
return n?.Next;
|
202
127
|
}
|
203
128
|
|
204
129
|
///<summary>
|
@@ -216,29 +141,77 @@
|
|
216
141
|
}
|
217
142
|
|
218
143
|
//#endregion
|
144
|
+
|
219
145
|
//#region IEnumerable<T>メンバ
|
220
146
|
|
221
|
-
|
147
|
+
operator fun iterator() = BidirectionalLinkedListIterator(this);
|
222
|
-
|
148
|
+
|
223
|
-
|
149
|
+
//#endregion
|
150
|
+
|
224
|
-
|
151
|
+
}
|
152
|
+
|
153
|
+
public class BidirectionalLinkedListIterator(val list: BidirectionalLinkedList) {
|
154
|
+
private var n: Node? = list.First;
|
155
|
+
fun next(): Node? {
|
156
|
+
return n?.Next;
|
225
157
|
}
|
158
|
+
fun hasNext(): Boolean {
|
159
|
+
return n != list.End;
|
160
|
+
}
|
161
|
+
}
|
226
162
|
|
163
|
+
///<summary>
|
164
|
+
/// 連結リストのノード。
|
165
|
+
///</summary>
|
166
|
+
public class Node {
|
167
|
+
|
168
|
+
//#region フィールド
|
169
|
+
|
170
|
+
private var value: Any?;
|
171
|
+
private var prev: Node?;
|
172
|
+
private var next: Node?;
|
173
|
+
|
174
|
+
//#endregion
|
175
|
+
|
176
|
+
//#region 初期化
|
177
|
+
|
227
|
-
|
178
|
+
internal constructor(_value: Any?, _prev: Node?, _next: Node?) {
|
179
|
+
this.value = _value;
|
180
|
+
this.prev = _prev;
|
228
|
-
|
181
|
+
this.next = _next;
|
229
182
|
}
|
230
183
|
|
231
184
|
//#endregion
|
232
185
|
|
233
|
-
// public fun hasNext(): boolean {
|
234
|
-
// return n!= this.End;
|
235
|
-
// }
|
236
|
-
// public fun next(): Iterator {
|
237
|
-
//
|
186
|
+
//#region プロパティ
|
187
|
+
|
188
|
+
public var Value: Any?
|
189
|
+
get() = this.value;
|
190
|
+
set(value) {
|
191
|
+
this.value = value;
|
192
|
+
}
|
193
|
+
|
194
|
+
/// <summary>
|
195
|
+
/// 前のノード。
|
196
|
+
/// </summary>
|
197
|
+
var Previous: Node?
|
198
|
+
get() = this.prev;
|
199
|
+
internal set(value) {
|
200
|
+
this.prev = value;
|
201
|
+
}
|
202
|
+
|
203
|
+
/// <summary>
|
204
|
+
/// 次のノード。
|
205
|
+
/// </summary>
|
206
|
+
var Next: Node?
|
207
|
+
get() = this.next;
|
208
|
+
internal set(value) {
|
209
|
+
this.next = value;
|
210
|
+
}
|
211
|
+
|
238
|
-
//
|
212
|
+
//#endregion
|
239
|
-
|
213
|
+
|
240
214
|
}
|
241
|
-
|
242
215
|
```
|
243
216
|
|
244
217
|
### 試したこと
|
1
タイトルに明記
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Kotlinで双方向連結リストを実装したい。
|
1
|
+
Kotlinで双方向連結リストを実装したい。特にGetEnumeratorと型引数Tらへんに困ってます。
|
body
CHANGED
File without changes
|