Skip to content

Commit 6346529

Browse files
committed
AV-07: initially added delete operation to linked list
1 parent fba09a2 commit 6346529

File tree

1 file changed

+69
-21
lines changed

1 file changed

+69
-21
lines changed

src/algorithms/linked-list/Linked-List.vue

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
<input v-model="targetValue" type="number" name="Number" placeholder="Enter Number">
1919
<button type="submit">Search</button>
2020
</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>
2125
<button @click="toggleSearchForm">Search</button>
2226
<button @click="toggleInsertForm">Insert</button>
27+
<button @click="toggleDeleteForm">Delete Node</button>
2328
</div>
2429
</template>
2530

@@ -39,7 +44,8 @@
3944
4045
append(data, element) {
4146
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;
4349
if (!this.head) {
4450
newNode.element.lines = element.lines;
4551
this.head = newNode;
@@ -81,6 +87,7 @@
8187
circleContainer: undefined,
8288
showInputForm: false,
8389
showSearchForm: false,
90+
showDeleteForm: false,
8491
targetValue: null,
8592
insertedValue: null,
8693
}
@@ -100,39 +107,43 @@
100107
.duration(1000) // Animation duration in milliseconds
101108
.attr('fill', 'rgba(0, 255, 0, 0.8)');
102109
}
103-
104110
},
105111
106112
toggleInsertForm() {
107113
this.showSuccessResult = false;
108114
this.showFailureResult = false;
109115
this.showInputForm = !this.showInputForm;
110-
if (this.showSearchForm) {
111-
this.showSearchForm = !this.showSearchForm;
112-
}
113-
116+
this.hideDeleteForm();
117+
this.hideSearchForm();
114118
},
119+
115120
toggleSearchForm() {
116121
this.showSuccessResult = false;
117122
this.showFailureResult = false;
118123
this.showSearchForm = !this.showSearchForm;
119-
if (this.showInputForm) {
120-
this.showInputForm = !this.showInputForm;
121-
}
124+
this.hideDeleteForm();
125+
this.hideInsertForm();
122126
},
123127
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+
},
126135
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;
133144
},
134145
135-
searchFormSubmitAction() {
146+
insertFormSubmitAction() {
136147
if (this.insertedValue > 0) {
137148
138149
this.appendIntoLinkedList(this.insertedValue);
@@ -170,7 +181,7 @@
170181
171182
this.xAxis += this.circleRadius * 20;
172183
173-
return circle;
184+
return {circle: circle, text: text};
174185
175186
// this.circleContainer.append('circle')
176187
// .attr('cx', 90)
@@ -218,14 +229,51 @@
218229
if (!pos) {
219230
const circle = this.createCircle(num);
220231
let line;
221-
if(this.linkedList.head){
232+
if (this.linkedList.head) {
222233
line = this.createArrow((this.xAxis - this.circleRadius * 40) + 70);
223234
}
224235
225236
this.linkedList.append(num, {circle: circle, lines: line});
226237
}
227238
},
228239
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+
229277
async searchIntoLinkedList() {
230278
this.showSuccessResult = false;
231279
this.showFailureResult = false;
@@ -245,7 +293,7 @@
245293
break;
246294
}
247295
await new Promise(resolve => setTimeout(resolve, 600));
248-
if(current.next){
296+
if (current.next) {
249297
for (const line of current.element.lines) {
250298
line.transition().duration(300).attr('stroke', "pink");
251299
}

0 commit comments

Comments
 (0)