@@ -63,7 +63,7 @@ class LinkedList {
6363 display () {
6464 let current = this .head ;
6565 while (current) {
66- console .log (current .data );
66+ console .log (" data: " , current .data , " Element: " , current . element );
6767 current = current .next ;
6868 }
6969 }
@@ -155,7 +155,6 @@ export default {
155155 },
156156
157157 createCircle (num , createArrow ) {
158-
159158 const finalRadius = 20 ;
160159
161160 const circle = this .circleContainer .append (' circle' )
@@ -182,14 +181,6 @@ export default {
182181 this .xAxis += this .circleRadius * 20 ;
183182
184183 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- //
193184 },
194185
195186 createArrow (groupX ) {
@@ -227,9 +218,11 @@ export default {
227218 },
228219
229220 appendIntoLinkedList (num , pos ) {
221+
230222 if (! pos) {
231223 const circle = this .createCircle (num);
232224 let arrow;
225+
233226 if (this .linkedList .head ) {
234227 arrow = this .createArrow ((this .xAxis - this .circleRadius * 40 ) + 70 );
235228 }
@@ -238,100 +231,115 @@ export default {
238231 }
239232 },
240233
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-
269234 removeItemFromLinkedList () {
270235 let current = this .linkedList .head ;
271236 let prev = current;
272237 while (current) {
273238
274239 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' ));
277242 let currentArrowX;
243+
278244 if (current .element .arrow ) {
279245 currentArrowX = parseFloat (current .element .arrow .attr (' transform' ).replace (/ [^ \d. -] / g , ' ' )) / 10 ;
280- } else if (prev) {
246+ } else if (prev . element . arrow ) {
281247 prev .element .arrow .remove ();
248+ prev .element .arrow = null ;
282249 }
283250 current .element .circle .remove ();
284251 current .element .text .remove ();
285- if (current .next ) {
286- current .data = current .next .data ;
287252
253+ if (current .next ) {
288254 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 ;
290263 }
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 ;
293292 }
294293 this .xAxis -= this .circleRadius * 20 ;
295294 break ;
296295 }
296+
297297 prev = current;
298298 current = current .next ;
299299 }
300+
300301 this .targetValue = null ;
301302 },
302303
303304 async searchIntoLinkedList () {
304305 this .showSuccessResult = false ;
305306 this .showFailureResult = false ;
307+
306308 if (this .targetValue > 0 ) {
307309 this .showSearchForm = false ;
308310 let current = this .linkedList .head ;
309311 let found;
310312 let pos = 1 ;
313+
311314 while (current) {
312315 found = current .data === this .targetValue ;
313316 current .element .circle .transition ()
314317 .duration (500 ).attr (' fill' , " pink" );
318+
315319 await new Promise (resolve => setTimeout (resolve, 600 ));
320+
316321 if (found) {
317322 current .element .circle .transition ()
318323 .duration (1000 ).attr (' fill' , " green" );
319324 break ;
320325 }
326+
321327 await new Promise (resolve => setTimeout (resolve, 600 ));
328+
322329 if (current .next ) {
323330 if (current .element .arrow ) {
324331 current .element .arrow .selectAll (' line' ).transition ().duration (300 ).attr (' stroke' , " pink" );
325332 }
326333 await new Promise (resolve => setTimeout (resolve, 300 ));
327334 }
335+
328336 current = current .next ;
329337 pos++ ;
330338 }
339+
331340 if (found) {
332341 this .result = pos;
333342 this .showSuccessResult = true ;
334- console .log (" target found" );
335343 } else {
336344 this .showFailureResult = true ;
337345 }
@@ -345,6 +353,7 @@ export default {
345353 }
346354 }
347355 },
356+
348357 mounted () {
349358 this .createContainer ();
350359 this .linkedList = new LinkedList ();
0 commit comments