Skip to content

Commit 41e9de9

Browse files
committed
Add fromArray and toArray methods to the linked list class.
1 parent 1173918 commit 41e9de9

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/data-structures/linkedList.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,96 @@ describe('List', () => {
171171
})
172172
})
173173

174+
describe('fromArray', () => {
175+
it('should create a list from an array of elements', () => {
176+
const array = [1, 2, 3, 4]
177+
const list = new List<number>()
178+
list.fromArray(array)
179+
180+
const result = [] as number[]
181+
list.map((node) => result.push(node.data))
182+
183+
expect(result).toEqual(array)
184+
})
185+
186+
it('should handle an empty array', () => {
187+
const array: number[] = []
188+
const list = new List<number>()
189+
list.fromArray(array)
190+
191+
expect(list.head).toBeNull()
192+
expect(list.tail).toBeNull()
193+
})
194+
195+
it('should correctly set head and tail for a single element array', () => {
196+
const array = [42]
197+
const list = new List<number>()
198+
list.fromArray(array)
199+
200+
expect(list.head).not.toBeNull()
201+
expect(list.tail).not.toBeNull()
202+
expect(list.head).toBe(list.tail)
203+
expect(list.head?.data).toBe(42)
204+
})
205+
206+
it('should correctly link nodes', () => {
207+
const array = [1, 2, 3]
208+
const list = new List<number>()
209+
list.fromArray(array)
210+
211+
expect(list.head?.data).toBe(1)
212+
expect(list.head?.next?.data).toBe(2)
213+
expect(list.head?.next?.next?.data).toBe(3)
214+
expect(list.head?.next?.next?.next).toBeNull()
215+
216+
expect(list.tail?.data).toBe(3)
217+
expect(list.tail?.prev?.data).toBe(2)
218+
expect(list.tail?.prev?.prev?.data).toBe(1)
219+
expect(list.tail?.prev?.prev?.prev).toBeNull()
220+
})
221+
})
222+
223+
describe('toArray', () => {
224+
it('should return an array of all list nodes\' data', () => {
225+
const list = new List<number>()
226+
list.push(new Node(1))
227+
list.push(new Node(2))
228+
list.push(new Node(3))
229+
230+
const result = list.toArray()
231+
232+
expect(result).toEqual([1, 2, 3])
233+
})
234+
235+
it('should return an empty array when the list is empty', () => {
236+
const list = new List<number>()
237+
238+
const result = list.toArray()
239+
240+
expect(result).toEqual([])
241+
})
242+
243+
it('should handle a list with a single node', () => {
244+
const list = new List<number>()
245+
list.push(new Node(42))
246+
247+
const result = list.toArray()
248+
249+
expect(result).toEqual([42])
250+
})
251+
252+
it('should correctly handle a list with multiple nodes', () => {
253+
const list = new List<string>()
254+
list.push(new Node('a'))
255+
list.push(new Node('b'))
256+
list.push(new Node('c'))
257+
258+
const result = list.toArray()
259+
260+
expect(result).toEqual(['a', 'b', 'c'])
261+
})
262+
})
263+
174264
describe('toString', () => {
175265
it('should return a concatenated string of JSON stringified node data', () => {
176266
const node1 = new Node({ value: 1 })

src/data-structures/linkedList.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ class List<T> {
126126
[left.data, right.data] = [right.data, left.data]
127127
}
128128

129+
/**
130+
* Fills the List with data from an array.
131+
* @param {T[]} array - The array to fill the list with.
132+
*/
133+
fromArray(array: T[]): void {
134+
array.forEach((data) => this.push(new Node(data)))
135+
}
136+
137+
/**
138+
* @returns {T[]} An array of all list nodes' data.
139+
*/
140+
toArray(): T[] {
141+
const array: T[] = []
142+
this.map((node) => array.push(node.data))
143+
return array
144+
}
145+
129146
/**
130147
* Returns all list nodes' data concatenated into a single string.
131148
* @returns {String} The concatenated string of all nodes' data.

0 commit comments

Comments
 (0)