|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | 2 | import { DoublyLinkedList } from './DoublyLinkedList' |
3 | 3 |
|
4 | | -describe('list', () => { |
| 4 | +describe('doubleLinkedList', () => { |
5 | 5 | it('should create an empty linked list', () => { |
6 | 6 | const list = new DoublyLinkedList() |
7 | 7 |
|
8 | | - expect(list.head).toBe(null) |
| 8 | + expect(list.head?.next).equals(list.tail) |
| 9 | + expect(list.tail?.next).toBe(null) |
9 | 10 | expect(list.toString()).toBe('') |
10 | 11 | }) |
11 | 12 |
|
12 | | - it('should prepend a node to the linked list', () => { |
| 13 | + it('should find a node by index', () => { |
13 | 14 | const list = new DoublyLinkedList() |
14 | 15 |
|
15 | | - list.prepend(1) |
16 | | - expect(list.head?.value).toBe(1) |
17 | | - expect(list.toString()).toBe('1') |
| 16 | + list.addAtTail(1) |
| 17 | + list.addAtTail(2) |
| 18 | + list.addAtTail(3) |
18 | 19 |
|
19 | | - list.prepend(2) |
20 | | - expect(list.head?.value).toBe(2) |
21 | | - expect(list.head?.next?.value).toBe(1) |
22 | | - expect(list.head?.next?.previous?.value).toBe(2) |
23 | | - expect(list.toString()).toBe('2,1') |
| 20 | + expect(list.toString()).toBe('1,2,3') |
| 21 | + expect(list.get(-1)).toBe(null) |
| 22 | + expect(list.get(0)?.value).toBe(1) |
| 23 | + expect(list.get(1)?.value).toBe(2) |
| 24 | + expect(list.get(2)?.value).toBe(3) |
| 25 | + expect(list.get(3)).toBe(null) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should add a node at the head of the linked list', () => { |
| 29 | + const list = new DoublyLinkedList() |
| 30 | + |
| 31 | + list.addAtHead(1) |
| 32 | + list.addAtHead(2) |
| 33 | + list.addAtHead(3) |
24 | 34 |
|
25 | | - list.prepend(3) |
26 | | - expect(list.head?.value).toBe(3) |
27 | | - expect(list.head?.next?.value).toBe(2) |
28 | | - expect(list.head?.next?.previous?.value).toBe(3) |
29 | 35 | expect(list.toString()).toBe('3,2,1') |
30 | 36 | }) |
31 | 37 |
|
32 | | - it('should append a node to the linked list', () => { |
| 38 | + it('should add a node at the tail of the linked list', () => { |
33 | 39 | const list = new DoublyLinkedList() |
34 | 40 |
|
35 | | - list.append(1) |
36 | | - expect(list.head?.value).toBe(1) |
37 | | - expect(list.toString()).toBe('1') |
38 | | - |
39 | | - list.append(2) |
40 | | - expect(list.head?.value).toBe(1) |
41 | | - expect(list.head?.next?.value).toBe(2) |
42 | | - expect(list.head?.next?.previous?.value).toBe(1) |
43 | | - expect(list.toString()).toBe('1,2') |
44 | | - |
45 | | - list.append(3) |
46 | | - expect(list.head?.value).toBe(1) |
47 | | - expect(list.head?.next?.value).toBe(2) |
48 | | - expect(list.head?.next?.previous?.value).toBe(1) |
49 | | - expect(list.head?.next?.next?.value).toBe(3) |
50 | | - expect(list.head?.next?.next?.previous?.value).toBe(2) |
| 41 | + list.addAtTail(1) |
| 42 | + list.addAtTail(2) |
| 43 | + list.addAtTail(3) |
| 44 | + |
51 | 45 | expect(list.toString()).toBe('1,2,3') |
52 | 46 | }) |
53 | 47 |
|
54 | | - it('should insert a node to the linked list', () => { |
| 48 | + it('should add a node at a specific index of the linked list', () => { |
55 | 49 | const list = new DoublyLinkedList() |
56 | 50 |
|
57 | | - list.insert(4, 3) |
58 | | - expect(list.head?.value).toBe(4) |
59 | | - |
60 | | - list.insert(3, 2) |
61 | | - list.insert(2, 1) |
62 | | - list.insert(1, -7) |
63 | | - list.insert(10, 9) |
| 51 | + list.addAtIndex(0, 1) |
| 52 | + list.addAtIndex(1, 2) |
| 53 | + list.addAtIndex(2, 3) |
| 54 | + list.addAtIndex(3, 5) |
| 55 | + list.addAtIndex(3, 4) |
64 | 56 |
|
65 | | - expect(list.head?.value).toBe(1) |
66 | | - expect(list.head?.next?.value).toBe(4) |
67 | | - expect(list.head?.next?.next?.value).toBe(2) |
68 | | - expect(list.head?.next?.next?.previous?.value).toBe(4) |
69 | | - expect(list.toString()).toBe('1,4,2,3,10') |
| 57 | + expect(list.toString()).toBe('1,2,3,4,5') |
70 | 58 | }) |
71 | 59 |
|
72 | | - it('should find a node by value', () => { |
| 60 | + it('should delete a node by index', () => { |
73 | 61 | const list = new DoublyLinkedList() |
74 | 62 |
|
75 | | - expect(list.find(1)).toBeNull() |
| 63 | + list.addAtTail(1) |
| 64 | + list.addAtTail(2) |
| 65 | + list.addAtTail(3) |
| 66 | + list.addAtTail(4) |
| 67 | + list.addAtTail(5) |
| 68 | + |
| 69 | + expect(list.toString()).toBe('1,2,3,4,5') |
| 70 | + list.deleteAtIndex(0) |
| 71 | + expect(list.toString()).toBe('2,3,4,5') |
| 72 | + list.deleteAtIndex(1) |
| 73 | + expect(list.toString()).toBe('2,4,5') |
| 74 | + list.deleteAtIndex(2) |
| 75 | + expect(list.toString()).toBe('2,4') |
| 76 | + list.deleteAtIndex(2) |
| 77 | + expect(list.toString()).toBe('2,4') |
| 78 | + list.deleteAtIndex(0) |
| 79 | + expect(list.toString()).toBe('4') |
| 80 | + list.deleteAtIndex(0) |
| 81 | + expect(list.toString()).toBe('') |
| 82 | + }) |
76 | 83 |
|
77 | | - list.append(1) |
78 | | - expect(list.find(1)?.value).toBe(1) |
| 84 | + it('should find a node by value', () => { |
| 85 | + const list = new DoublyLinkedList() |
79 | 86 |
|
80 | | - list.append(2) |
81 | | - expect(list.find(2)?.value).toBe(2) |
| 87 | + list.addAtTail(1) |
| 88 | + list.addAtTail(2) |
| 89 | + list.addAtTail(3) |
82 | 90 |
|
83 | | - list.append(3) |
84 | 91 | expect(list.find(1)?.value).toBe(1) |
85 | 92 | expect(list.find(2)?.value).toBe(2) |
86 | 93 | expect(list.find(3)?.value).toBe(3) |
87 | 94 | }) |
88 | 95 |
|
89 | | - it('should delete a node by value from the linked list', () => { |
90 | | - const list = new DoublyLinkedList() |
91 | | - |
92 | | - list.append(1) |
93 | | - list.append(1) |
94 | | - list.append(2) |
95 | | - list.append(2) |
96 | | - list.append(3) |
97 | | - list.append(4) |
98 | | - list.append(5) |
99 | | - |
100 | | - list.delete(3) |
101 | | - expect(list.head?.value).toBe(1) |
102 | | - expect(list.head?.next?.value).toBe(1) |
103 | | - expect(list.head?.next?.next?.value).toBe(2) |
104 | | - expect(list.toString()).toBe('1,1,2,2,4,5') |
105 | | - |
106 | | - list.delete(1) |
107 | | - expect(list.head?.value).toBe(2) |
108 | | - expect(list.head?.previous).toBe(null) |
109 | | - expect(list.toString()).toBe('2,2,4,5') |
110 | | - |
111 | | - list.delete(4) |
112 | | - expect(list.head?.value).toBe(2) |
113 | | - expect(list.head?.next?.value).toBe(2) |
114 | | - expect(list.head?.next?.next?.value).toBe(5) |
115 | | - expect(list.head?.next?.next?.previous?.value).toBe(2) |
116 | | - expect(list.toString()).toBe('2,2,5') |
117 | | - |
118 | | - list.delete(5) |
119 | | - expect(list.head?.value).toBe(2) |
120 | | - expect(list.toString()).toBe('2,2') |
121 | | - |
122 | | - list.delete(2) |
123 | | - expect(list.head).toBe(null) |
124 | | - expect(list.toString()).toBe('') |
125 | | - }) |
126 | | - |
127 | | - it('should delete linked list head', () => { |
| 96 | + it('should delete a node by value', () => { |
128 | 97 | const list = new DoublyLinkedList() |
129 | 98 |
|
130 | | - list.append(1) |
131 | | - list.append(2) |
132 | | - list.append(3) |
133 | | - |
134 | | - list.deleteHead() |
135 | | - expect(list.head?.value).toBe(2) |
136 | | - expect(list.head?.previous).toBe(null) |
137 | | - expect(list.head?.next?.value).toBe(3) |
138 | | - expect(list.head?.next?.previous?.value).toBe(2) |
139 | | - expect(list.toString()).toBe('2,3') |
140 | | - |
141 | | - list.deleteHead() |
142 | | - expect(list.head?.value).toBe(3) |
143 | | - expect(list.head?.previous).toBe(null) |
144 | | - expect(list.head?.next).toBe(null) |
145 | | - expect(list.toString()).toBe('3') |
146 | | - |
147 | | - list.deleteHead() |
148 | | - expect(list.head).toBe(null) |
149 | | - expect(list.toString()).toBe('') |
| 99 | + list.addAtTail(1) |
| 100 | + list.addAtTail(2) |
| 101 | + list.addAtTail(3) |
| 102 | + list.addAtTail(4) |
| 103 | + list.addAtTail(5) |
| 104 | + |
| 105 | + expect(list.toString()).toBe('1,2,3,4,5') |
| 106 | + list.deleteValue(1) |
| 107 | + expect(list.toString()).toBe('2,3,4,5') |
| 108 | + list.deleteValue(3) |
| 109 | + expect(list.toString()).toBe('2,4,5') |
| 110 | + list.deleteValue(5) |
| 111 | + expect(list.toString()).toBe('2,4') |
150 | 112 | }) |
151 | 113 | }) |
0 commit comments