mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
4 Commits
2f4bb188bc
...
9dc2ef3f63
Author | SHA1 | Date | |
---|---|---|---|
|
9dc2ef3f63 | ||
|
52d42cf70c | ||
|
c387aab2e7 | ||
|
218a9c3643 |
@ -269,4 +269,21 @@ export default class LinkedList {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* make the linked list iterable
|
||||||
|
* @return {iterator}
|
||||||
|
*/
|
||||||
|
[Symbol.iterator]() {
|
||||||
|
let currentNode = this.head;
|
||||||
|
|
||||||
|
return {
|
||||||
|
next: () => {
|
||||||
|
if (!currentNode) return { done: true };
|
||||||
|
const { value, next } = currentNode;
|
||||||
|
currentNode = next;
|
||||||
|
return { value, done: false };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -279,4 +279,17 @@ describe('LinkedList', () => {
|
|||||||
expect(linkedList.head.value).toBe(1);
|
expect(linkedList.head.value).toBe(1);
|
||||||
expect(linkedList.tail.value).toBe(3);
|
expect(linkedList.tail.value).toBe(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be iterable', () => {
|
||||||
|
const linkedList = new LinkedList();
|
||||||
|
expect(typeof linkedList[Symbol.iterator]).toBe('function');
|
||||||
|
|
||||||
|
// Add test values to linked list.
|
||||||
|
linkedList
|
||||||
|
.append(1)
|
||||||
|
.append(2)
|
||||||
|
.append(3);
|
||||||
|
|
||||||
|
expect([...linkedList]).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user