@@ -63,7 +63,7 @@ class LinkedList {
63
63
display () {
64
64
let current = this .head ;
65
65
while (current) {
66
- console .log (current .data );
66
+ console .log (" data: " , current .data , " Element: " , current . element );
67
67
current = current .next ;
68
68
}
69
69
}
@@ -155,7 +155,6 @@ export default {
155
155
},
156
156
157
157
createCircle (num , createArrow ) {
158
-
159
158
const finalRadius = 20 ;
160
159
161
160
const circle = this .circleContainer .append (' circle' )
@@ -182,14 +181,6 @@ export default {
182
181
this .xAxis += this .circleRadius * 20 ;
183
182
184
183
return {circle: circle, text: text};
185
-
186
- // this.circleContainer.append('circle')
187
- // .attr('cx', 90)
188
- // .attr('cy', 100)
189
- // .attr('r', finalRadius)
190
- // .attr('fill', 'red');
191
- //
192
- //
193
184
},
194
185
195
186
createArrow (groupX ) {
@@ -227,9 +218,11 @@ export default {
227
218
},
228
219
229
220
appendIntoLinkedList (num , pos ) {
221
+
230
222
if (! pos) {
231
223
const circle = this .createCircle (num);
232
224
let arrow;
225
+
233
226
if (this .linkedList .head ) {
234
227
arrow = this .createArrow ((this .xAxis - this .circleRadius * 40 ) + 70 );
235
228
}
@@ -238,100 +231,115 @@ export default {
238
231
}
239
232
},
240
233
241
-
242
- deleteNode (node , cx , x , arrowX ) {
243
- if (node) {
244
- const currentCircleXAxis = parseFloat (node .element .circle .attr (' cx' ));
245
- const currentTextX = parseFloat (node .element .text .attr (' x' ));
246
- let nextArrow;
247
- if (node .element .arrow ) {
248
- nextArrow = parseFloat (node .element .arrow .attr (' transform' ).split (' (' )[1 ].split (' ,' )[0 ]);
249
- }
250
-
251
- console .log (cx, currentCircleXAxis, x, currentTextX);
252
- node .element .circle .transition ()
253
- .duration (1000 )
254
- .attr (' cx' , cx);
255
-
256
- node .element .text .transition ()
257
- .duration (1000 )
258
- .attr (' x' , x);
259
- if (node .element .arrow ) {
260
- console .log (" arrowx:" , nextArrow, arrowX);
261
- node .element .arrow .attr (' transform' , ` translate(${ arrowX} , 0)` );
262
- console .log (parseFloat (node .element .arrow .attr (' transform' ).replace (/ [^ \d. -] / g , ' ' )));
263
- }
264
- this .deleteNode (node .next , currentCircleXAxis, currentTextX, nextArrow)
265
- }
266
-
267
- },
268
-
269
234
removeItemFromLinkedList () {
270
235
let current = this .linkedList .head ;
271
236
let prev = current;
272
237
while (current) {
273
238
274
239
if (current .data === this .targetValue ) {
275
- const currentCircleXAxis = parseFloat (current .element .circle .attr (' cx' ));
276
- const currentTextX = parseFloat (current .element .text .attr (' x' ));
240
+ let currentCircleXAxis = parseFloat (current .element .circle .attr (' cx' ));
241
+ let currentTextX = parseFloat (current .element .text .attr (' x' ));
277
242
let currentArrowX;
243
+
278
244
if (current .element .arrow ) {
279
245
currentArrowX = parseFloat (current .element .arrow .attr (' transform' ).replace (/ [^ \d. -] / g , ' ' )) / 10 ;
280
- } else if (prev) {
246
+ } else if (prev . element . arrow ) {
281
247
prev .element .arrow .remove ();
248
+ prev .element .arrow = null ;
282
249
}
283
250
current .element .circle .remove ();
284
251
current .element .text .remove ();
285
- if (current .next ) {
286
- current .data = current .next .data ;
287
252
253
+ if (current .next ) {
288
254
if (current .element .arrow ) {
289
- current .element .arrow .remove ()
255
+ current .element .arrow .remove ();
256
+ current .element .arrow = null ;
257
+ }
258
+
259
+ if (current === this .linkedList .head ) {
260
+ this .linkedList .head = current .next ;
261
+ } else {
262
+ prev .next = current .next ;
290
263
}
291
- current .element = current .next .element ;
292
- this .deleteNode (current .next , currentCircleXAxis, currentTextX, currentArrowX);
264
+
265
+ current = current .next ;
266
+ while (current) {
267
+ current .element .circle .transition ()
268
+ .duration (1000 )
269
+ .attr (' cx' , currentCircleXAxis);
270
+
271
+ current .element .text .transition ()
272
+ .duration (1000 )
273
+ .attr (' x' , currentTextX);
274
+
275
+ if (current .element .arrow ) {
276
+ current .element .arrow .attr (' transform' , ` translate(${ currentArrowX} , 0)` );
277
+ }
278
+
279
+ currentCircleXAxis = parseFloat (current .element .circle .attr (' cx' ));
280
+ currentTextX = parseFloat (current .element .text .attr (' x' ));
281
+
282
+ if (current .element .arrow ) {
283
+ currentArrowX = parseFloat (current .element .arrow .attr (' transform' ).split (' (' )[1 ].split (' ,' )[0 ]);
284
+ }
285
+ current = current .next ;
286
+ }
287
+
288
+ } else if (current === this .linkedList .head ) {
289
+ this .linkedList .head = current .next ;
290
+ } else if (prev) {
291
+ prev .next = null ;
293
292
}
294
293
this .xAxis -= this .circleRadius * 20 ;
295
294
break ;
296
295
}
296
+
297
297
prev = current;
298
298
current = current .next ;
299
299
}
300
+
300
301
this .targetValue = null ;
301
302
},
302
303
303
304
async searchIntoLinkedList () {
304
305
this .showSuccessResult = false ;
305
306
this .showFailureResult = false ;
307
+
306
308
if (this .targetValue > 0 ) {
307
309
this .showSearchForm = false ;
308
310
let current = this .linkedList .head ;
309
311
let found;
310
312
let pos = 1 ;
313
+
311
314
while (current) {
312
315
found = current .data === this .targetValue ;
313
316
current .element .circle .transition ()
314
317
.duration (500 ).attr (' fill' , " pink" );
318
+
315
319
await new Promise (resolve => setTimeout (resolve, 600 ));
320
+
316
321
if (found) {
317
322
current .element .circle .transition ()
318
323
.duration (1000 ).attr (' fill' , " green" );
319
324
break ;
320
325
}
326
+
321
327
await new Promise (resolve => setTimeout (resolve, 600 ));
328
+
322
329
if (current .next ) {
323
330
if (current .element .arrow ) {
324
331
current .element .arrow .selectAll (' line' ).transition ().duration (300 ).attr (' stroke' , " pink" );
325
332
}
326
333
await new Promise (resolve => setTimeout (resolve, 300 ));
327
334
}
335
+
328
336
current = current .next ;
329
337
pos++ ;
330
338
}
339
+
331
340
if (found) {
332
341
this .result = pos;
333
342
this .showSuccessResult = true ;
334
- console .log (" target found" );
335
343
} else {
336
344
this .showFailureResult = true ;
337
345
}
@@ -345,6 +353,7 @@ export default {
345
353
}
346
354
}
347
355
},
356
+
348
357
mounted () {
349
358
this .createContainer ();
350
359
this .linkedList = new LinkedList ();
0 commit comments