Skip to content

Commit fbb5c79

Browse files
committed
adds linkedList
1 parent 86fc7a5 commit fbb5c79

File tree

2 files changed

+229
-4
lines changed

2 files changed

+229
-4
lines changed

linkedLists/index.js

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,135 @@
1+
function createNode(value) {
2+
return {
3+
value,
4+
next: null
5+
}
6+
}
7+
18
function createLinkedList() {
2-
return {}
9+
return {
10+
head: null,
11+
size: 0,
12+
13+
add(value) {
14+
const node = createNode(value)
15+
16+
if (this.head === null) {
17+
this.head = node
18+
this.size++
19+
return node
20+
}
21+
22+
let current = this.head
23+
while (current.next) {
24+
current = current.next
25+
}
26+
27+
current.next = node
28+
this.size++
29+
30+
return node
31+
},
32+
33+
insertAt(value, index) {
34+
if (index > 0 && index > this.size) {
35+
return null
36+
}
37+
38+
const node = createNode(value)
39+
40+
if (index === 0) {
41+
node.next = this.head
42+
this.head = node
43+
this.size++
44+
45+
return node
46+
}
47+
48+
let current = this.head
49+
let previous
50+
let i = 0
51+
52+
while (i < index) {
53+
i++
54+
previous = current
55+
current = current.next
56+
}
57+
58+
previous.next = node
59+
node.next = current
60+
this.size++
61+
62+
return node
63+
},
64+
65+
get(index) {
66+
if (index > 0 && index > this.size) {
67+
return null
68+
}
69+
70+
if (index === 0) {
71+
return this.head
72+
}
73+
74+
let current = this.head
75+
let i = 0
76+
77+
while (i < index) {
78+
i++
79+
current = current.next
80+
}
81+
82+
return current
83+
},
84+
85+
delete(index) {
86+
if (index > 0 && index > this.size) {
87+
return null
88+
}
89+
90+
if (index === 0) {
91+
const deleted = this.head
92+
93+
this.head = this.head.next
94+
this.size--
95+
96+
return deleted
97+
}
98+
99+
let current = this.head
100+
let previous
101+
let i = 0
102+
103+
while (i < index) {
104+
i++
105+
previous = current
106+
current = current.next
107+
}
108+
109+
const deleted = current
110+
previous.next = current.next
111+
this.size--
112+
113+
return deleted
114+
},
115+
116+
isEmpty() {
117+
return this.size === 0
118+
},
119+
120+
print() {
121+
let values = []
122+
let current = this.head
123+
124+
while (current && current.value) {
125+
values.push(current.value)
126+
current = current.next
127+
}
128+
129+
return values.join(' => ')
130+
}
131+
}
3132
}
4133

134+
exports.createNode = createNode
5135
exports.createLinkedList = createLinkedList

linkedLists/index.test.js

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,107 @@
1-
const { createLinkedList } = require('./index')
1+
const { createLinkedList, createNode } = require('./index')
2+
3+
describe('Node', () => {
4+
let node
5+
beforeEach(() => {
6+
node = createNode('a')
7+
})
8+
9+
test('instantiation', () => {
10+
expect(node).toBeDefined()
11+
expect(node.value).toEqual('a')
12+
})
13+
})
214

315
describe('Linked List', () => {
416
let linkedList
517
beforeEach(() => {
618
linkedList = createLinkedList()
719
})
820

9-
test('appeaseJestForNow', () => {
10-
expect(true).toBe(true)
21+
test('existence', () => {
22+
expect(linkedList).toBeDefined()
23+
})
24+
25+
test('size', () => {
26+
expect(linkedList.size).toEqual(0)
27+
})
28+
29+
test('head', () => {
30+
expect(linkedList.head).toBeNull()
31+
32+
const a = linkedList.add('a')
33+
expect(linkedList.head).toEqual(a)
34+
})
35+
36+
test('add', () => {
37+
const a = linkedList.add('a')
38+
const b = linkedList.add('b')
39+
40+
expect(linkedList.head.next).toEqual(b)
41+
expect(linkedList.size).toEqual(2)
42+
})
43+
44+
test('insertAt', () => {
45+
const values = ['a', 'b', 'c', 'd', 'e']
46+
const [a, b, ...otherNodes] = values.map(val => linkedList.add(val))
47+
48+
const badIndex = linkedList.insertAt('f', 7)
49+
50+
expect(badIndex).toBeNull()
51+
52+
const f = linkedList.insertAt('f', 0)
53+
expect(f.next).toEqual(a)
54+
expect(linkedList.head).toEqual(f)
55+
expect(linkedList.size).toEqual(6)
56+
57+
const g = linkedList.insertAt('g', 2)
58+
expect(g.next).toEqual(b)
59+
expect(linkedList.size).toEqual(7)
60+
})
61+
62+
test('get', () => {
63+
const values = ['a', 'b', 'c', 'd', 'e']
64+
const nodes = values.map(val => linkedList.add(val))
65+
66+
const badIndex = linkedList.get(7)
67+
expect(badIndex).toBeNull()
68+
69+
const head = linkedList.get(0)
70+
expect(head).toEqual(nodes[0])
71+
72+
const atIndexThree = linkedList.get(3)
73+
expect(atIndexThree).toEqual(nodes[3])
74+
})
75+
76+
test('delete', () => {
77+
const values = ['a', 'b', 'c', 'd', 'e']
78+
const nodes = values.map(val => linkedList.add(val))
79+
80+
const badIndex = linkedList.delete(7)
81+
expect(badIndex).toBeNull()
82+
83+
const head = linkedList.delete(0)
84+
expect(head).toEqual(nodes[0])
85+
86+
const d = linkedList.delete(2)
87+
88+
expect(d).toEqual(nodes[3])
89+
expect(linkedList.size).toEqual(3)
90+
expect(linkedList.get(2)).toEqual(nodes[4])
91+
})
92+
93+
test('isEmpty', () => {
94+
expect(linkedList.isEmpty()).toBe(true)
95+
linkedList.add('a')
96+
expect(linkedList.isEmpty()).toBe(false)
97+
})
98+
99+
test('print', () => {
100+
const values = ['a', 'b', 'c', 'd', 'e']
101+
values.forEach(val => {
102+
linkedList.add(val)
103+
})
104+
105+
expect(linkedList.print()).toEqual('a => b => c => d => e')
11106
})
12107
})

0 commit comments

Comments
 (0)