22 <div >
33 <h1 >Linked List</h1 >
44 <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 >
820 </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 >
1123 </div >
1224</template >
1325
1628 constructor (data ) {
1729 this .data = data;
1830 this .next = null ;
31+ this .element = null ;
1932 }
2033 }
2134
2437 this .head = null ;
2538 }
2639
27- append (data ) {
40+ append (data , element ) {
2841 const newNode = new Node (data);
42+ newNode .element = element;
2943 if (! this .head ) {
3044 this .head = newNode;
3145 return ;
5569 data () {
5670 return {
5771 linkedList: undefined ,
72+ result: " " ,
73+ showSuccessResult: false ,
74+ showFailureResult: false ,
5875 xAxis: 20 ,
5976 arrowXAxis: 0 ,
6077 circleRadius: 5 ,
6178 elements: [],
6279 circleContainer: undefined ,
6380 showInputForm: false ,
64- insertedValue: undefined ,
81+ showSearchForm: false ,
82+ targetValue: null ,
83+ insertedValue: null ,
6584 }
6685 },
6786
82101
83102 },
84103
85- toggleForm () {
104+ toggleInsertForm () {
105+ this .showSuccessResult = false ;
106+ this .showFailureResult = false ;
86107 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+ }
87120 },
88121
89- formSubmitAction () {
122+ insertFormSubmitAction () {
90123 if (this .insertedValue > 0 ) {
124+
91125 this .appendIntoLinkedList (this .insertedValue );
92- this .createArrow ((this .xAxis - this .circleRadius * 20 ) + 70 );
93- this .createCircle (this .insertedValue );
94126 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 ;
95139 } else {
96140 console .error (" You've added negative number" );
97141 }
117161 .attr (' dy' , ' .4em' ) // Vertical alignment adjustment
118162 .attr (' text-anchor' , ' middle' ) // Horizontal alignment
119163 .text (num);
120- if (createArrow){
121- this .createArrow (this .xAxis + 70 );
164+ if (createArrow) {
165+ this .createArrow (this .xAxis + 70 );
122166 }
123167 this .elements .push ({circle: circle, text: text});
124168
125169 this .xAxis += this .circleRadius * 20 ;
126170
171+ return circle;
172+
127173 // this.circleContainer.append('circle')
128174 // .attr('cx', 90)
129175 // .attr('cy', 100)
133179 //
134180 },
135181
136- createArrow (arrowX ){
182+ createArrow (arrowX ) {
137183 const arrowLength = 20 ;
138184 const arrowY = 100 ;
139185 // Line 1: Horizontal line
166212
167213 appendIntoLinkedList (num , pos ) {
168214 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 ;
170250 }
171251 },
172252
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);
178256 }
179257 }
180258 },
181259 mounted () {
182260 this .createContainer ();
183261 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 ])
191263 },
192264 }
193265 </script >
194266
195267<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+ }
196283
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+ }
197299 </style >
0 commit comments