@@ -4,8 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
} ;
5
5
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6
6
const Node_1 = __importDefault ( require ( "./Node" ) ) ;
7
+ const IndexError_1 = __importDefault ( require ( "../utils/IndexError" ) ) ;
7
8
class SinglyLinkedList {
8
9
constructor ( ) {
10
+ // TODO: use constructor to initiate a filled linked list like you can do with sets?
9
11
this . _length = 0 ;
10
12
this . head = null ;
11
13
this . tail = null ;
@@ -14,7 +16,7 @@ class SinglyLinkedList {
14
16
// ? maybe make length a public getter with a private setter so that it's readonly outside
15
17
// ? the class, but modifiable inside by the other methods?
16
18
// when implemented, tsc error: Getter and setter accessors do not agree in visibility.ts(2379)
17
- // typescript doesn't support this,
19
+ // typescript doesn't support this,
18
20
// see https://github.com/microsoft/TypeScript/issues/2845
19
21
// design meeting notes: https://github.com/microsoft/TypeScript/issues/6735
20
22
// instead, i'm using a slightly uglier work around where there is no setter,
@@ -42,42 +44,48 @@ class SinglyLinkedList {
42
44
// O(n) time since we have to iterate over the whole list to find the new tail,
43
45
// and make the new tail's next point to null.
44
46
if ( ! this . head )
45
- return null ;
46
- const node = this . head ;
47
+ return ;
48
+ let node = this . head ;
47
49
if ( ! node . next ) {
48
50
this . head = null ;
49
51
this . tail = null ;
50
52
this . _length -- ;
51
53
return node ;
52
54
}
53
55
else {
54
- let childNode = node . next ;
55
- while ( childNode . next !== null ) {
56
+ console . assert ( this . length > 1 ) ;
57
+ let targetNode = node . next ;
58
+ while ( targetNode . next !== null ) {
56
59
// list traversal
57
- childNode = childNode . next ;
60
+ node = targetNode ;
61
+ targetNode = targetNode . next ;
58
62
}
59
63
node . next = null ;
60
64
this . tail = node ;
61
65
this . _length -- ;
62
- return childNode ;
66
+ return targetNode ;
63
67
}
64
68
}
65
69
unshift ( value ) {
66
70
// add node as first of list
67
71
// O(1)
68
72
const node = new Node_1 . default ( value ) ;
69
- if ( ! this . length )
73
+ if ( ! this . head ) {
74
+ this . head = node ;
70
75
this . tail = node ;
71
- node . next = this . head ;
72
- this . head = node ;
76
+ }
77
+ else {
78
+ node . next = this . head ;
79
+ this . head = node ;
80
+ }
73
81
this . _length ++ ;
74
82
return this . head ;
75
83
}
76
84
shift ( ) {
77
85
// remove first node from list
78
86
// O(1)
79
87
if ( ! this . head )
80
- return null ;
88
+ return ;
81
89
const newHead = this . head . next ;
82
90
const oldHead = this . head ;
83
91
oldHead . next = null ;
@@ -88,8 +96,8 @@ class SinglyLinkedList {
88
96
return oldHead ;
89
97
}
90
98
get ( index ) {
91
- if ( ! this . head || ( index > this . length - 1 || index < 0 ) )
92
- return null ;
99
+ if ( ! this . head || ( index > this . length || index < 0 ) )
100
+ throw new IndexError_1 . default ;
93
101
let counter = 0 ;
94
102
let node = this . head ;
95
103
while ( counter < index ) {
@@ -108,8 +116,10 @@ class SinglyLinkedList {
108
116
return selectedNode ;
109
117
}
110
118
insert ( value , index , options = { prevEnabled : false } ) {
111
- if ( index > this . length - 1 || index < 0 )
112
- return null ; // TODO: throw indexError ? user tried to insert at out of range index.
119
+ if ( ! index )
120
+ return this . push ( value ) ;
121
+ if ( index > this . length || index < 0 )
122
+ throw new IndexError_1 . default ;
113
123
if ( index === 0 )
114
124
return this . unshift ( value ) ;
115
125
if ( index === this . length )
@@ -129,8 +139,8 @@ class SinglyLinkedList {
129
139
return newNode ;
130
140
}
131
141
removeIndex ( index , options = { prevEnabled : false } ) {
132
- if ( index > this . length - 1 || index < 0 )
133
- return null ;
142
+ if ( index > this . length || index < 0 )
143
+ throw new IndexError_1 . default ;
134
144
if ( index === 0 )
135
145
return this . shift ( ) ;
136
146
if ( index === this . length - 1 )
@@ -148,5 +158,35 @@ class SinglyLinkedList {
148
158
this . _length -- ;
149
159
return targetNode ;
150
160
}
161
+ log ( beginning = 0 , end = this . length - 1 ) {
162
+ if ( ( beginning > this . length - 1 || beginning < 0 )
163
+ || ( end > this . length - 1 || end < 0 ) )
164
+ throw new IndexError_1 . default ;
165
+ let node = this . get ( beginning ) ;
166
+ let count = beginning ;
167
+ while ( node ) {
168
+ console . log ( `node ${ count } ` , node ) ;
169
+ if ( node . next ) {
170
+ node = node . next ;
171
+ count ++ ;
172
+ }
173
+ else
174
+ return ;
175
+ }
176
+ }
177
+ toString ( beginning = 0 , end = this . length - 1 ) {
178
+ if ( ( beginning > this . length - 1 || beginning < 0 )
179
+ || ( end > this . length - 1 || end < 0 ) )
180
+ throw new IndexError_1 . default ;
181
+ let node = this . get ( beginning ) ;
182
+ while ( node ) {
183
+ console . log ( node . data ) ;
184
+ if ( node . next ) {
185
+ node = node . next ;
186
+ }
187
+ else
188
+ return ;
189
+ }
190
+ }
151
191
}
152
192
exports . default = SinglyLinkedList ;
0 commit comments