|
18 | 18 | <input v-model="targetValue" type="number" name="Number" placeholder="Enter Number">
|
19 | 19 | <button type="submit">Search</button>
|
20 | 20 | </form>
|
| 21 | + <form v-if="showDeleteForm" @submit.prevent="removeItemFromLinkedList"> |
| 22 | + <input v-model="targetValue" type="number" name="Number" placeholder="Enter Number"> |
| 23 | + <button type="submit">Delete</button> |
| 24 | + </form> |
21 | 25 | <button @click="toggleSearchForm">Search</button>
|
22 | 26 | <button @click="toggleInsertForm">Insert</button>
|
| 27 | + <button @click="toggleDeleteForm">Delete Node</button> |
23 | 28 | </div>
|
24 | 29 | </template>
|
25 | 30 |
|
|
39 | 44 |
|
40 | 45 | append(data, element) {
|
41 | 46 | const newNode = new Node(data);
|
42 |
| - newNode.element.circle = element.circle; |
| 47 | + newNode.element.circle = element.circle.circle; |
| 48 | + newNode.element.text = element.circle.text; |
43 | 49 | if (!this.head) {
|
44 | 50 | newNode.element.lines = element.lines;
|
45 | 51 | this.head = newNode;
|
|
81 | 87 | circleContainer: undefined,
|
82 | 88 | showInputForm: false,
|
83 | 89 | showSearchForm: false,
|
| 90 | + showDeleteForm: false, |
84 | 91 | targetValue: null,
|
85 | 92 | insertedValue: null,
|
86 | 93 | }
|
|
100 | 107 | .duration(1000) // Animation duration in milliseconds
|
101 | 108 | .attr('fill', 'rgba(0, 255, 0, 0.8)');
|
102 | 109 | }
|
103 |
| -
|
104 | 110 | },
|
105 | 111 |
|
106 | 112 | toggleInsertForm() {
|
107 | 113 | this.showSuccessResult = false;
|
108 | 114 | this.showFailureResult = false;
|
109 | 115 | this.showInputForm = !this.showInputForm;
|
110 |
| - if (this.showSearchForm) { |
111 |
| - this.showSearchForm = !this.showSearchForm; |
112 |
| - } |
113 |
| -
|
| 116 | + this.hideDeleteForm(); |
| 117 | + this.hideSearchForm(); |
114 | 118 | },
|
| 119 | +
|
115 | 120 | toggleSearchForm() {
|
116 | 121 | this.showSuccessResult = false;
|
117 | 122 | this.showFailureResult = false;
|
118 | 123 | this.showSearchForm = !this.showSearchForm;
|
119 |
| - if (this.showInputForm) { |
120 |
| - this.showInputForm = !this.showInputForm; |
121 |
| - } |
| 124 | + this.hideDeleteForm(); |
| 125 | + this.hideInsertForm(); |
122 | 126 | },
|
123 | 127 |
|
124 |
| - insertFormSubmitAction() { |
125 |
| - if (this.insertedValue > 0) { |
| 128 | + toggleDeleteForm() { |
| 129 | + this.showSuccessResult = false; |
| 130 | + this.showFailureResult = false; |
| 131 | + this.hideInsertForm(); |
| 132 | + this.hideSearchForm(); |
| 133 | + this.showDeleteForm = !this.showDeleteForm; |
| 134 | + }, |
126 | 135 |
|
127 |
| - this.appendIntoLinkedList(this.insertedValue); |
128 |
| - this.showInputForm = false; |
129 |
| - this.insertedValue = null; |
130 |
| - } else { |
131 |
| - console.error("You've added negative number"); |
132 |
| - } |
| 136 | + hideDeleteForm(){ |
| 137 | + this.showDeleteForm = false; |
| 138 | + }, |
| 139 | + hideInsertForm(){ |
| 140 | + this.showInputForm = false; |
| 141 | + }, |
| 142 | + hideSearchForm(){ |
| 143 | + this.showSearchForm = false; |
133 | 144 | },
|
134 | 145 |
|
135 |
| - searchFormSubmitAction() { |
| 146 | + insertFormSubmitAction() { |
136 | 147 | if (this.insertedValue > 0) {
|
137 | 148 |
|
138 | 149 | this.appendIntoLinkedList(this.insertedValue);
|
|
170 | 181 |
|
171 | 182 | this.xAxis += this.circleRadius * 20;
|
172 | 183 |
|
173 |
| - return circle; |
| 184 | + return {circle: circle, text: text}; |
174 | 185 |
|
175 | 186 | // this.circleContainer.append('circle')
|
176 | 187 | // .attr('cx', 90)
|
|
218 | 229 | if (!pos) {
|
219 | 230 | const circle = this.createCircle(num);
|
220 | 231 | let line;
|
221 |
| - if(this.linkedList.head){ |
| 232 | + if (this.linkedList.head) { |
222 | 233 | line = this.createArrow((this.xAxis - this.circleRadius * 40) + 70);
|
223 | 234 | }
|
224 | 235 |
|
225 | 236 | this.linkedList.append(num, {circle: circle, lines: line});
|
226 | 237 | }
|
227 | 238 | },
|
228 | 239 |
|
| 240 | + removeItemFromLinkedList() { |
| 241 | + let current = this.linkedList.head; |
| 242 | +
|
| 243 | + while (current) { |
| 244 | + if (current.data == this.targetValue) { |
| 245 | + console.log("Item found to delete"); |
| 246 | + const currentCircleXAxis = parseFloat( current.element.circle.attr('cx')); |
| 247 | + const currentTextX = parseFloat( current.element.text.attr('x')); |
| 248 | + current.element.circle.remove(); |
| 249 | + current.element.text.remove(); |
| 250 | +
|
| 251 | + for(const line of current.element.lines){ |
| 252 | + line.remove(); |
| 253 | + } |
| 254 | + current.next?.element.circle.transition() |
| 255 | + .duration(1000) |
| 256 | + .attr('cx', currentCircleXAxis); |
| 257 | + current.next?.element.text.transition() |
| 258 | + .duration(1000) |
| 259 | + .attr('x', currentTextX); |
| 260 | + current.data = current.next.data; |
| 261 | + current.element = current.next.element; |
| 262 | + if (current.next) { |
| 263 | + current.next = current.next.next; |
| 264 | + } else { |
| 265 | + current.next = null; |
| 266 | + } |
| 267 | + console.log() |
| 268 | +
|
| 269 | +
|
| 270 | + break; |
| 271 | + } |
| 272 | + current = current.next; |
| 273 | + } |
| 274 | + this.targetValue = null; |
| 275 | + }, |
| 276 | +
|
229 | 277 | async searchIntoLinkedList() {
|
230 | 278 | this.showSuccessResult = false;
|
231 | 279 | this.showFailureResult = false;
|
|
245 | 293 | break;
|
246 | 294 | }
|
247 | 295 | await new Promise(resolve => setTimeout(resolve, 600));
|
248 |
| - if(current.next){ |
| 296 | + if (current.next) { |
249 | 297 | for (const line of current.element.lines) {
|
250 | 298 | line.transition().duration(300).attr('stroke', "pink");
|
251 | 299 | }
|
|
0 commit comments