2
2
<div >
3
3
<h1 >Linked List</h1 >
4
4
<div ref =" circleContainer" ></div >
5
- <form v-if =" showInputForm" @submit.prevent =" formSubmitAction" >
6
- <input v-model =" insertedValue" type =" number" name =" Number" id =" " placeholder =" Enter Number" >
7
- <button type =" submit" : =" insertedValue" >Add</button >
5
+ <div v-if =" showSuccessResult" class =" success-message" >
6
+ Search result found at: "{{ result }}"
7
+ </div >
8
+
9
+ <div v-if =" showFailureResult" class =" fail-message" >
10
+ Searched item not found!
11
+ </div >
12
+ <hr >
13
+ <form v-if =" showInputForm" @submit.prevent =" insertFormSubmitAction" >
14
+ <input v-model =" insertedValue" type =" number" name =" Number" placeholder =" Enter Number" >
15
+ <button type =" submit" >Add</button >
16
+ </form >
17
+ <form v-if =" showSearchForm" @submit.prevent =" searchIntoLinkedList" >
18
+ <input v-model =" targetValue" type =" number" name =" Number" placeholder =" Enter Number" >
19
+ <button type =" submit" >Search</button >
8
20
</form >
9
- <button @click =" change " >Animation </button >
10
- <button @click =" toggleForm " >Insert</button >
21
+ <button @click =" toggleSearchForm " >Search </button >
22
+ <button @click =" toggleInsertForm " >Insert</button >
11
23
</div >
12
24
</template >
13
25
16
28
constructor (data ) {
17
29
this .data = data;
18
30
this .next = null ;
31
+ this .element = null ;
19
32
}
20
33
}
21
34
24
37
this .head = null ;
25
38
}
26
39
27
- append (data ) {
40
+ append (data , element ) {
28
41
const newNode = new Node (data);
42
+ newNode .element = element;
29
43
if (! this .head ) {
30
44
this .head = newNode;
31
45
return ;
55
69
data () {
56
70
return {
57
71
linkedList: undefined ,
72
+ result: " " ,
73
+ showSuccessResult: false ,
74
+ showFailureResult: false ,
58
75
xAxis: 20 ,
59
76
arrowXAxis: 0 ,
60
77
circleRadius: 5 ,
61
78
elements: [],
62
79
circleContainer: undefined ,
63
80
showInputForm: false ,
64
- insertedValue: undefined ,
81
+ showSearchForm: false ,
82
+ targetValue: null ,
83
+ insertedValue: null ,
65
84
}
66
85
},
67
86
82
101
83
102
},
84
103
85
- toggleForm () {
104
+ toggleInsertForm () {
105
+ this .showSuccessResult = false ;
106
+ this .showFailureResult = false ;
86
107
this .showInputForm = ! this .showInputForm ;
108
+ if (this .showSearchForm ) {
109
+ this .showSearchForm = ! this .showSearchForm ;
110
+ }
111
+
112
+ },
113
+ toggleSearchForm () {
114
+ this .showSuccessResult = false ;
115
+ this .showFailureResult = false ;
116
+ this .showSearchForm = ! this .showSearchForm ;
117
+ if (this .showInputForm ) {
118
+ this .showInputForm = ! this .showInputForm ;
119
+ }
87
120
},
88
121
89
- formSubmitAction () {
122
+ insertFormSubmitAction () {
90
123
if (this .insertedValue > 0 ) {
124
+
91
125
this .appendIntoLinkedList (this .insertedValue );
92
- this .createArrow ((this .xAxis - this .circleRadius * 20 ) + 70 );
93
- this .createCircle (this .insertedValue );
94
126
this .showInputForm = false ;
127
+ this .insertedValue = null ;
128
+ } else {
129
+ console .error (" You've added negative number" );
130
+ }
131
+ },
132
+
133
+ searchFormSubmitAction () {
134
+ if (this .insertedValue > 0 ) {
135
+
136
+ this .appendIntoLinkedList (this .insertedValue );
137
+ this .showInputForm = false ;
138
+ this .insertedValue = null ;
95
139
} else {
96
140
console .error (" You've added negative number" );
97
141
}
117
161
.attr (' dy' , ' .4em' ) // Vertical alignment adjustment
118
162
.attr (' text-anchor' , ' middle' ) // Horizontal alignment
119
163
.text (num);
120
- if (createArrow){
121
- this .createArrow (this .xAxis + 70 );
164
+ if (createArrow) {
165
+ this .createArrow (this .xAxis + 70 );
122
166
}
123
167
this .elements .push ({circle: circle, text: text});
124
168
125
169
this .xAxis += this .circleRadius * 20 ;
126
170
171
+ return circle;
172
+
127
173
// this.circleContainer.append('circle')
128
174
// .attr('cx', 90)
129
175
// .attr('cy', 100)
133
179
//
134
180
},
135
181
136
- createArrow (arrowX ){
182
+ createArrow (arrowX ) {
137
183
const arrowLength = 20 ;
138
184
const arrowY = 100 ;
139
185
// Line 1: Horizontal line
166
212
167
213
appendIntoLinkedList (num , pos ) {
168
214
if (! pos) {
169
- this .linkedList .append (num);
215
+ const ele = this .createCircle (num);
216
+ this .createArrow ((this .xAxis - this .circleRadius * 20 ) + 70 );
217
+ this .linkedList .append (num, ele);
218
+ }
219
+ },
220
+
221
+ async searchIntoLinkedList () {
222
+ this .showSuccessResult = false ;
223
+ this .showFailureResult = false ;
224
+ if (this .targetValue > 0 ) {
225
+ this .showSearchForm = false ;
226
+ let current = this .linkedList .head ;
227
+ let found;
228
+ let pos = 1 ;
229
+ while (current) {
230
+ found = current .data == this .targetValue ;
231
+ current .element .transition ()
232
+ .duration (1000 ).attr (' fill' , " pink" );
233
+ await new Promise (resolve => setTimeout (resolve, 1000 ));
234
+ if (found) {
235
+ current .element .transition ()
236
+ .duration (1000 ).attr (' fill' , " green" );
237
+ break ;
238
+ }
239
+ current = current .next ;
240
+ pos++ ;
241
+ }
242
+ if (found) {
243
+ this .result = pos;
244
+ this .showSuccessResult = true ;
245
+ console .log (" target found" );
246
+ } else {
247
+ this .showFailureResult = true ;
248
+ }
249
+ this .targetValue = null ;
170
250
}
171
251
},
172
252
173
- generateLinkedList () {
174
- let current = this .linkedList .head ;
175
- while (current) {
176
- this .createCircle (current .data , current .next );
177
- current = current .next ;
253
+ generateLinkedList (nums ) {
254
+ for (const n of nums) {
255
+ this .appendIntoLinkedList (n);
178
256
}
179
257
}
180
258
},
181
259
mounted () {
182
260
this .createContainer ();
183
261
this .linkedList = new LinkedList ();
184
- this .linkedList .append (1 );
185
- this .linkedList .append (2 );
186
- this .linkedList .append (3 );
187
- this .linkedList .append (5 );
188
- this .linkedList .append (7 );
189
- this .generateLinkedList ();
190
-
262
+ this .generateLinkedList ([1 , 2 , 3 ])
191
263
},
192
264
}
193
265
</script >
194
266
195
267
<style scoped>
268
+ .success-message {
269
+ background-color : #4caf50 ;
270
+ color : white ;
271
+ padding : 10px ;
272
+ border-radius : 5px ;
273
+ margin-top : 10px ;
274
+ }
275
+
276
+ .fail-message {
277
+ background-color : #af3e2e ;
278
+ color : white ;
279
+ padding : 10px ;
280
+ border-radius : 5px ;
281
+ margin-top : 10px ;
282
+ }
196
283
284
+ button {
285
+ background-color : #3c3f41 ;
286
+ color : white ;
287
+ border : none ;
288
+ padding : 10px 20px ;
289
+ margin : 5px ;
290
+ border-radius : 5px ;
291
+ cursor : pointer ;
292
+ }
293
+
294
+ input {
295
+ padding : 10px ;
296
+ border : 1px solid #ccc ;
297
+ border-radius : 5px ;
298
+ }
197
299
</style >
0 commit comments