Skip to content

Commit 61d6efd

Browse files
committed
Started working on Linked Lists
1 parent afac75b commit 61d6efd

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

src/linked-list/linked-list.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export class Node {
2+
constructor(key, next = null) {
3+
this._key = key;
4+
this._next = next;
5+
}
6+
7+
get key() {
8+
return this._key
9+
}
10+
get next() {
11+
return this._next;
12+
}
13+
}
14+
15+
16+
const _size = Symbol('size');
17+
const _head = Symbol('head');
18+
19+
export class LinkedList {
20+
constructor() {
21+
this[_size] = 0;
22+
this[_head] = null;
23+
}
24+
25+
get head() {
26+
return this[_head];
27+
}
28+
29+
size() {
30+
return this[_size];
31+
}
32+
33+
isEmpty() {
34+
return this.size() === 0;
35+
}
36+
37+
search(item) { }
38+
insertLast(item) { }
39+
insertFirst(item) { }
40+
insertBefore(item, before) { }
41+
insertAfter(item, after) { }
42+
remove(item) { }
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LinkedList } from './linked-list';
2+
3+
describe('Linked List', () => {
4+
let list;
5+
beforeEach(() => {
6+
list = new LinkedList
7+
});
8+
9+
it('should create a linked list object', () => {
10+
expect(list).toBeDefined();
11+
expect(list.size()).toBe(0);
12+
expect(list.head).toBe(null);
13+
expect(list.isEmpty()).toBeTruthy();
14+
});
15+
});

src/test-index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require('./binary-search-tree/binary-search-tree.spec');
22
require('./graph/graph.spec');
3-
require('./queue/queue.spec');
43
require('./graph/adj-list.spec');
4+
require('./queue/queue.spec');
5+
require('./linked-list/linked-list.spec');

typings/data-structures.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ declare namespace DataStructures {
3434
enqueue(o): void;
3535
dequeue(): Error | void;
3636
size(): number;
37-
isEmpty(): void;
37+
isEmpty(): boolean;
3838
top(): any;
3939
clear(): void;
4040
}
41+
42+
class LinkedList {
43+
head(): any;
44+
size(): number;
45+
isEmpty(): boolean;
46+
}
4147
}

0 commit comments

Comments
 (0)