Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 29a3ab7

Browse files
merge: Iterator and log methods added for linked lists (#891)
* iterator, log methods added in SL * iterator, log methods added in DL * test file added for DoublyLL * format issue fix
1 parent 68ca0ce commit 29a3ab7

File tree

5 files changed

+206
-10984
lines changed

5 files changed

+206
-10984
lines changed

‎Data-Structures/Linked-List/DoublyLinkedList.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ function DoubleLinkedList () {
194194
this.getTail = function () {
195195
return tail
196196
}
197+
198+
// Method to iterate over the LinkedList
199+
this.iterator = function () {
200+
let currentNode = this.getHead()
201+
if (currentNode === null) return -1
202+
203+
const iterate = function * () {
204+
while (currentNode) {
205+
yield currentNode.element
206+
currentNode = currentNode.next
207+
}
208+
}
209+
return iterate()
210+
}
211+
212+
// Method to log the LinkedList, for debugging
213+
// it' a circular structure, so can't use stringify to debug the whole structure
214+
this.log = function () {
215+
let currentNode = this.getHead()
216+
while (currentNode) {
217+
console.log(currentNode.element)
218+
currentNode = currentNode.next
219+
}
220+
}
197221
}
198222

199223
// Example
@@ -202,5 +226,9 @@ function DoubleLinkedList () {
202226
// newDoubleLinkedList.append(1)
203227
// newDoubleLinkedList.append(2)
204228
// newDoubleLinkedList.size() // returns 2
229+
// const iterate = newDoubleLinkedList.iterator()
230+
// console.log(iterate.next().value) // 1
231+
// console.log(iterate.next().value) // 2
232+
// console.log(newDoubleLinkedList.log())
205233

206234
export { DoubleLinkedList }

‎Data-Structures/Linked-List/SinglyLinkedList.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,25 @@ class LinkedList {
211211

212212
return list
213213
}
214+
215+
// Method to iterate over the LinkedList
216+
iterator () {
217+
let currentNode = this.headNode
218+
if (currentNode === null) return -1
219+
220+
const iterate = function * () {
221+
while (currentNode) {
222+
yield currentNode.data
223+
currentNode = currentNode.next
224+
}
225+
}
226+
return iterate()
227+
}
228+
229+
// Method to log the LinkedList
230+
log () {
231+
console.log(JSON.stringify(this.headNode, null, 2))
232+
}
214233
}
215234

216235
export { Node, LinkedList }
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { DoubleLinkedList } from '../DoublyLinkedList'
2+
3+
describe('DoubleLinkedList', () => {
4+
it('Check append', () => {
5+
const list = new DoubleLinkedList()
6+
7+
list.append(1)
8+
expect(list.getHead().element).toEqual(1)
9+
10+
list.append(2)
11+
expect(list.getTail().element).toEqual(2)
12+
})
13+
14+
it('Check insert', () => {
15+
const list = new DoubleLinkedList()
16+
17+
list.insert(0, 1)
18+
expect(list.getHead().element).toEqual(1)
19+
20+
list.insert(1, 20)
21+
expect(list.getTail().element).toEqual(20)
22+
})
23+
24+
it('Check removeAt', () => {
25+
const list = new DoubleLinkedList()
26+
27+
list.insert(0, 10)
28+
list.insert(1, 40)
29+
list.insert(2, 30)
30+
31+
list.removeAt(0)
32+
expect(list.getHead().element).toEqual(40)
33+
34+
list.removeAt(1)
35+
expect(list.getTail().element).toEqual(40)
36+
})
37+
38+
it('Check delete', () => {
39+
const list = new DoubleLinkedList()
40+
41+
list.insert(0, 10)
42+
list.insert(1, 40)
43+
44+
list.delete(10)
45+
expect(list.getHead().element).toEqual(40)
46+
})
47+
48+
it('Check deleteTail', () => {
49+
const list = new DoubleLinkedList()
50+
51+
list.insert(0, 10)
52+
list.insert(1, 40)
53+
54+
list.deleteTail()
55+
expect(list.getTail().element).toEqual(10)
56+
})
57+
58+
it('Check toString', () => {
59+
const list = new DoubleLinkedList()
60+
61+
list.insert(0, 20)
62+
expect(list.toString()).toEqual('20')
63+
})
64+
65+
it('Check isEmpty', () => {
66+
const list = new DoubleLinkedList()
67+
68+
expect(list.isEmpty()).toEqual(true)
69+
70+
list.insert(0, 'Hello')
71+
expect(list.isEmpty()).toEqual(false)
72+
})
73+
74+
it('Check size', () => {
75+
const list = new DoubleLinkedList()
76+
expect(list.size()).toBe(0)
77+
78+
list.append(10)
79+
expect(list.size()).toBe(1)
80+
81+
list.removeAt(1)
82+
expect(list.size()).toBe(1)
83+
})
84+
85+
it('Check toArray', () => {
86+
const list = new DoubleLinkedList()
87+
list.append(1)
88+
list.append(2)
89+
90+
const listArray = list.toArray()
91+
expect(listArray).toEqual([1, 2])
92+
})
93+
94+
it('Check getHead', () => {
95+
const list = new DoubleLinkedList()
96+
expect(list.getHead()).toEqual(null)
97+
98+
list.append(1)
99+
list.append(2)
100+
expect(list.getHead()).toBeInstanceOf(Object)
101+
})
102+
103+
it('Check Iterator', () => {
104+
const list = new DoubleLinkedList()
105+
106+
let iterate = list.iterator()
107+
expect(iterate).toBe(-1)
108+
109+
const arr = [10, 20, 5]
110+
list.append(arr[0])
111+
list.append(arr[1])
112+
list.append(arr[2])
113+
iterate = list.iterator()
114+
115+
for (let i = 0; i < arr.length; i++) {
116+
expect(iterate.next().value).toBe(arr[i])
117+
}
118+
expect(iterate.next().value).toBe(undefined)
119+
120+
iterate = list.iterator()
121+
let count = 0
122+
for (const item of iterate) {
123+
expect(item).toBe(arr[count])
124+
count++
125+
}
126+
})
127+
})

‎Data-Structures/Linked-List/test/SinglyLinkedList.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,29 @@ describe('SinglyLinkedList', () => {
163163
list.removeFirst()
164164
expect(list.size()).toBe(1)
165165
})
166+
167+
it('Check Iterator', () => {
168+
const list = new LinkedList()
169+
170+
let iterate = list.iterator()
171+
expect(iterate).toBe(-1)
172+
173+
const arr = [10, 20, 5]
174+
list.addLast(arr[0])
175+
list.addLast(arr[1])
176+
list.addLast(arr[2])
177+
iterate = list.iterator()
178+
179+
for (let i = 0; i < arr.length; i++) {
180+
expect(iterate.next().value).toBe(arr[i])
181+
}
182+
expect(iterate.next().value).toBe(undefined)
183+
184+
iterate = list.iterator()
185+
let count = 0
186+
for (const item of iterate) {
187+
expect(item).toBe(arr[count])
188+
count++
189+
}
190+
})
166191
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /