|
1 | 1 | <template>
|
2 | 2 | <div>
|
3 | 3 | <h1>Linked List</h1>
|
| 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> |
| 8 | + </form> |
| 9 | + <button @click="change">Animation</button> |
| 10 | + <button @click="toggleForm">Insert</button> |
4 | 11 | </div>
|
5 | 12 | </template>
|
6 | 13 |
|
7 | 14 | <script>
|
| 15 | + class Node { |
| 16 | + constructor(data) { |
| 17 | + this.data = data; |
| 18 | + this.next = null; |
| 19 | + } |
| 20 | + } |
| 21 | +
|
| 22 | + class LinkedList { |
| 23 | + constructor() { |
| 24 | + this.head = null; |
| 25 | + } |
| 26 | +
|
| 27 | + append(data) { |
| 28 | + const newNode = new Node(data); |
| 29 | + if (!this.head) { |
| 30 | + this.head = newNode; |
| 31 | + return; |
| 32 | + } |
| 33 | +
|
| 34 | + let current = this.head; |
| 35 | + while (current.next) { |
| 36 | + current = current.next; |
| 37 | + } |
| 38 | + current.next = newNode; |
| 39 | + } |
| 40 | +
|
| 41 | + display() { |
| 42 | + let current = this.head; |
| 43 | + while (current) { |
| 44 | + console.log(current.data); |
| 45 | + current = current.next; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | +
|
| 51 | + import * as d3 from 'd3'; |
| 52 | +
|
8 | 53 | export default {
|
9 |
| - name: "Linked-List" |
| 54 | + name: "Linked-List", |
| 55 | + data() { |
| 56 | + return { |
| 57 | + linkedList: undefined, |
| 58 | + xAxis: 20, |
| 59 | + arrowXAxis: 0, |
| 60 | + circleRadius: 5, |
| 61 | + elements: [], |
| 62 | + circleContainer: undefined, |
| 63 | + showInputForm: false, |
| 64 | + insertedValue: undefined, |
| 65 | + } |
| 66 | + }, |
| 67 | +
|
| 68 | + methods: { |
| 69 | + createContainer() { |
| 70 | + this.circleContainer = d3.select(this.$refs.circleContainer) |
| 71 | + .append('svg') |
| 72 | + .attr('width', 500) |
| 73 | + .attr('height', 200); |
| 74 | + }, |
| 75 | +
|
| 76 | + change() { |
| 77 | + for (const element of this.elements) { |
| 78 | + element.circle.transition() |
| 79 | + .duration(1000) // Animation duration in milliseconds |
| 80 | + .attr('fill', 'rgba(0, 255, 0, 0.8)'); |
| 81 | + } |
| 82 | +
|
| 83 | + }, |
| 84 | +
|
| 85 | + toggleForm() { |
| 86 | + this.showInputForm = !this.showInputForm; |
| 87 | + }, |
| 88 | +
|
| 89 | + formSubmitAction() { |
| 90 | + if (this.insertedValue > 0) { |
| 91 | + this.appendIntoLinkedList(this.insertedValue); |
| 92 | + this.createArrow((this.xAxis - this.circleRadius * 20) + 70); |
| 93 | + this.createCircle(this.insertedValue); |
| 94 | + this.showInputForm = false; |
| 95 | + } else { |
| 96 | + console.error("You've added negative number"); |
| 97 | + } |
| 98 | + }, |
| 99 | +
|
| 100 | + createCircle(num, createArrow) { |
| 101 | +
|
| 102 | + const finalRadius = 20; |
| 103 | +
|
| 104 | + const circle = this.circleContainer.append('circle') |
| 105 | + .attr('cx', this.xAxis) |
| 106 | + .attr('cy', 100) |
| 107 | + .attr('r', this.circleRadius) |
| 108 | + .attr('fill', 'rgba(0, 0, 255, 0.5)') |
| 109 | + .attr('opacity', .5); |
| 110 | + circle.transition() |
| 111 | + .duration(1000) // Animation duration in milliseconds |
| 112 | + .attr('r', finalRadius); |
| 113 | +
|
| 114 | + const text = this.circleContainer.append('text') |
| 115 | + .attr('x', this.xAxis) |
| 116 | + .attr('y', 100) |
| 117 | + .attr('dy', '.4em') // Vertical alignment adjustment |
| 118 | + .attr('text-anchor', 'middle') // Horizontal alignment |
| 119 | + .text(num); |
| 120 | + if(createArrow){ |
| 121 | + this.createArrow(this.xAxis+70); |
| 122 | + } |
| 123 | + this.elements.push({circle: circle, text: text}); |
| 124 | +
|
| 125 | + this.xAxis += this.circleRadius * 20; |
| 126 | +
|
| 127 | + // this.circleContainer.append('circle') |
| 128 | + // .attr('cx', 90) |
| 129 | + // .attr('cy', 100) |
| 130 | + // .attr('r', finalRadius) |
| 131 | + // .attr('fill', 'red'); |
| 132 | + // |
| 133 | + // |
| 134 | + }, |
| 135 | +
|
| 136 | + createArrow(arrowX){ |
| 137 | + const arrowLength = 20; |
| 138 | + const arrowY = 100; |
| 139 | + // Line 1: Horizontal line |
| 140 | + this.circleContainer.append('line') |
| 141 | + .attr('x1', arrowX - arrowLength - 20) |
| 142 | + .attr('y1', arrowY) |
| 143 | + .attr('x2', arrowX) |
| 144 | + .attr('y2', arrowY) |
| 145 | + .attr('stroke', 'black') |
| 146 | + .attr('stroke-width', 2); |
| 147 | +
|
| 148 | + // Line 2: Diagonal line (left) |
| 149 | + this.circleContainer.append('line') |
| 150 | + .attr('x1', arrowX - 10) |
| 151 | + .attr('y1', arrowY - 5) |
| 152 | + .attr('x2', arrowX - arrowLength + 20) |
| 153 | + .attr('y2', arrowY) |
| 154 | + .attr('stroke', 'black') |
| 155 | + .attr('stroke-width', 2); |
| 156 | +
|
| 157 | + // Line 3: Diagonal line (right) |
| 158 | + this.circleContainer.append('line') |
| 159 | + .attr('x1', arrowX - 10) |
| 160 | + .attr('y1', arrowY + 5) |
| 161 | + .attr('x2', arrowX - arrowLength + 20) |
| 162 | + .attr('y2', arrowY) |
| 163 | + .attr('stroke', 'black') |
| 164 | + .attr('stroke-width', 2); |
| 165 | + }, |
| 166 | +
|
| 167 | + appendIntoLinkedList(num, pos) { |
| 168 | + if (!pos) { |
| 169 | + this.linkedList.append(num); |
| 170 | + } |
| 171 | + }, |
| 172 | +
|
| 173 | + generateLinkedList() { |
| 174 | + let current = this.linkedList.head; |
| 175 | + while (current) { |
| 176 | + this.createCircle(current.data, current.next); |
| 177 | + current = current.next; |
| 178 | + } |
| 179 | + } |
| 180 | + }, |
| 181 | + mounted() { |
| 182 | + this.createContainer(); |
| 183 | + 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 | +
|
| 191 | + }, |
10 | 192 | }
|
11 | 193 | </script>
|
12 | 194 |
|
|
0 commit comments