@@ -60,17 +60,18 @@ TwoLinkList.prototype.AddToEnd = function(data) {
60
60
61
61
this . count ++ ;
62
62
}
63
-
64
63
/**
65
64
* Returns the item at the desired position in the list.
66
65
* @param {Number } position - The position in the list where the item should be located.
67
66
*/
68
67
TwoLinkList . prototype . FindAt = function ( position ) {
69
- // Show error when trying to access position outside of list .
70
- if ( this . count === 0 || position > this . count || position < 1 ) {
71
- throw new Err . DSException ( Err . ListErr . NonExistant ( OneLinkList . name ) , 101 ) ;
68
+ // Show error when trying to access an invalid position .
69
+ if ( this . count === 0 ) {
70
+ throw new Err . DSException ( Err . ListErr . EmptyList , 100 ) ;
72
71
} else if ( position % 1 != 0 || typeof position != Number . name . toLowerCase ( ) ) {
73
- throw new Err . DSException ( Err . ListErr . InvalidPosition ( ) , 102 ) ;
72
+ throw new Err . DSException ( Err . ListErr . InvalidPosition ( ) , 101 ) ;
73
+ } else if ( position > this . count || position < 1 ) {
74
+ throw new Err . DSException ( Err . ListErr . NonExistant ( TwoLinkList . name ) , 102 ) ;
74
75
}
75
76
76
77
// Sequentially search for item (1 -> last).
@@ -81,52 +82,131 @@ TwoLinkList.prototype.FindAt = function(position) {
81
82
82
83
return currItem ;
83
84
}
84
-
85
85
/**
86
86
* Removes the item at the desired position in the list.
87
87
* @param {Number } position - The position in the list where the item should be located.
88
88
*/
89
89
TwoLinkList . prototype . RemoveAt = function ( position ) {
90
90
// Show error when trying to access position outside of list.
91
91
if ( this . count === 0 || position > this . count || position < 1 ) {
92
- throw new Err . DSException ( Err . ListErr . NonExistant ( OneLinkList . name ) , 201 ) ;
92
+ throw new Err . DSException ( Err . ListErr . NonExistant ( TwoLinkList . name ) , 201 ) ;
93
93
}
94
94
// Show error when the position is invalid.
95
95
else if ( position % 1 != 0 || typeof position != Number . name . toLowerCase ( ) ) {
96
96
throw new Err . DSException ( Err . ListErr . InvalidPosition ( ) , 202 ) ;
97
97
}
98
98
99
- // Linearly search for item before desired item (1 -> last).
100
- let currItem = this . first ,
101
- beforeItemToRemove , itemToRemove , currCount ;
102
- for ( currCount = 1 ; currCount < position ; currCount ++ ) {
103
- beforeItemToRemove = currItem ;
104
- itemToRemove = currItem . next ;
105
- currItem = currItem . next ;
99
+ // Handle list with one item.
100
+ if ( this . count === 1 ) {
101
+ this . first = null ;
102
+ this . last = null ;
106
103
}
104
+ // Linearly search for item before desired item (1 -> last).
105
+ else {
106
+ let currItem = this . first ,
107
+ itemToRemove = currItem ;
108
+ for ( let currCount = 1 ; currCount < position ; currCount ++ ) {
109
+ itemToRemove = currItem . next ;
110
+ currItem = currItem . next ;
111
+ }
107
112
108
- // if(currCount)
109
- beforeItemToRemove . next = itemToRemove . next ;
110
- delete itemToRemove ;
113
+ // Special Case: position == 1
114
+ if ( position === 1 ) {
115
+ itemToRemove . next . prev = null ;
116
+ this . first = itemToRemove . next ;
117
+ }
118
+ // Special Case: position == last
119
+ else if ( position === this . count ) {
120
+ itemToRemove . prev . next = null ;
121
+ this . last = itemToRemove . prev ;
122
+ }
123
+ // All other positions
124
+ else {
125
+ itemToRemove . prev . next = itemToRemove . next ;
126
+ itemToRemove . next . prev = itemToRemove . prev ;
127
+ }
128
+
129
+ itemToRemove = null ;
130
+ }
111
131
this . count -- ;
112
132
}
113
-
114
133
/**
115
- * Print all items of the list to the console.
134
+ * Removes the first item matching the data passed.
135
+ * @param {any } data - The data to search and destroy.
116
136
*/
117
- TwoLinkList . prototype . PrintAll = function ( ) {
118
- // Check if list is empty.
119
- if ( this . count === 0 || ! ( this . first instanceof Item ) ) {
120
- console . log ( 'Empty list.' ) ;
121
- return ;
137
+ TwoLinkList . prototype . RemoveThis = function ( data ) {
138
+ // Check if the list is empty.
139
+ if ( this . count === 0 ) {
140
+ throw new Err . DSException ( Err . ListErr . NonExistant ( TwoLinkList . name ) , 401 ) ;
141
+ }
142
+ // Check if valid data was passed as an argument.
143
+ else if ( data === undefined ) {
144
+ throw new Err . DSException ( Err . ListErr . InvalidData ( ) , 402 ) ;
122
145
}
123
146
124
- // Print each item to the console sequentially.
125
- let curr = this . first ;
126
- while ( curr ) {
127
- console . log ( curr ) ;
128
- curr = curr . next ;
147
+ // Search through list sequentially for the data.
148
+ let currItem = this . first ,
149
+ itemToRemove ;
150
+
151
+ // Handle list with one item.
152
+ if ( this . count === 1 ) {
153
+ if ( typeof ( data ) == 'object' ) {
154
+ if ( JSON . stringify ( currItem . data ) === JSON . stringify ( data ) ) {
155
+ this . first = null ;
156
+ this . last = null ;
157
+ this . count -- ;
158
+ return ;
159
+ }
160
+ } else {
161
+ if ( currItem . data === data ) {
162
+ this . first = null ;
163
+ this . last = null ;
164
+ this . count -- ;
165
+ return ;
166
+ }
167
+ }
168
+ }
169
+ // Handle list with more than one item.
170
+ else {
171
+ // If data param is an object.
172
+ if ( typeof ( data ) == 'object' ) {
173
+ while ( currItem . next ) {
174
+ if ( JSON . stringify ( currItem . next . data ) === JSON . stringify ( data ) ) {
175
+ itemToRemove = currItem . next ;
176
+ currItem . next = currItem . next . next ;
177
+
178
+ // Check if removing last item.
179
+ if ( itemToRemove === this . last ) {
180
+ this . last = currItem ;
181
+ }
182
+ this . count -- ;
183
+ break ;
184
+ }
185
+
186
+ currItem = currItem . next ;
187
+ }
188
+ }
189
+ // Else data param is a non-reference data type.
190
+ else {
191
+ while ( currItem . next ) {
192
+ if ( currItem . next . data === data ) {
193
+ itemToRemove = currItem . next ;
194
+ currItem . next = currItem . next . next ;
195
+
196
+ // Check if removing last item.
197
+ if ( itemToRemove === this . last ) {
198
+ this . last = currItem ;
199
+ }
200
+ this . count -- ;
201
+ break ;
202
+ }
203
+
204
+ currItem = currItem . next ;
205
+ }
206
+ }
129
207
}
208
+
209
+ itemToRemove = null ;
130
210
}
131
211
132
212
module . exports = TwoLinkList ;
0 commit comments