Skip to content

Commit ed2eb43

Browse files
committedJan 4, 2019
--refactor: tests and utils
1 parent 8c9ed88 commit ed2eb43

File tree

2 files changed

+159
-49
lines changed

2 files changed

+159
-49
lines changed
 

‎src/_DataStructures_/LinkedList/LinkedList.test.js

+123-31
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,141 @@ describe('Data Structures: Linked Lists', () => {
3232
list = new LinkedList();
3333
});
3434

35-
it('Should add element at beginning using list.addAtBeginning()', () => {
36-
list.addAtBeginning(12);
37-
expect(list.head.data).toEqual(12);
35+
describe('addAtBeginning(value)', () => {
36+
it('Should add element at beginning', () => {
37+
list.addAtBeginning(12);
38+
expect(list.head.data).toEqual(12);
3839

39-
list.addAtBeginning(15);
40-
expect(list.head.data).toEqual(15);
40+
list.addAtBeginning(15);
41+
expect(list.head.data).toEqual(15);
42+
});
43+
44+
it('Should return the 10 as the first element in the list', () => {
45+
list.addAtBeginning(10);
46+
expect(list.getFirst(10));
47+
});
4148
});
4249

43-
it('Should add element at end using list.addAtEnd()', () => {
44-
list.addAtEnd(12);
45-
expect(list.head.data).toEqual(12);
50+
describe('addAtEnd(value)', () => {
51+
it('Should add element at end', () => {
52+
list.addAtBeginning(10);
53+
list.addAtEnd(12);
54+
expect(list.getLast()).toEqual(12);
55+
});
56+
57+
it('Should add at the beginning if the list is empty', () => {
58+
list.addAtEnd(15);
59+
60+
expect(list.head.data).toEqual(15);
61+
expect(list.head.next).toEqual(null);
62+
});
63+
64+
it('Should return 4 as length of the list after adding at the end', () => {
65+
list.addAtEnd(15);
66+
list.addAtEnd(23);
67+
list.addAtEnd(33);
68+
list.addAtEnd(10);
69+
70+
expect(list.length()).toEqual(4);
71+
});
72+
});
73+
74+
describe('length() of the list', () => {
75+
it('Should return 0 if the list is empty', () => {
76+
expect(list.length()).toEqual(0);
77+
});
78+
79+
it('Should return the present size of the list after adding elements', () => {
80+
list.addAtBeginning(1);
81+
list.addAtBeginning(2);
82+
list.addAtBeginning(3);
83+
expect(list.length()).toEqual(3);
84+
});
85+
86+
it('Should return the present size of the list after removing elements', () => {
87+
list.addAtBeginning(1);
88+
list.addAtBeginning(2);
89+
list.addAtBeginning(3);
90+
expect(list.length()).toEqual(3);
4691

47-
list.addAtEnd(15);
48-
expect(list.head.data).toEqual(12);
49-
expect(list.head.next.data).toEqual(15);
50-
expect(list.length()).toEqual(2);
92+
list.removeFromEnd();
93+
list.removeFromBeginning();
94+
expect(list.length()).toEqual(1);
95+
});
5196
});
5297

53-
it('Should return the present size of the list using list.length()', () => {
54-
expect(list.length()).toEqual(0);
55-
list.addAtBeginning(1);
56-
list.addAtBeginning(2);
57-
list.addAtBeginning(3);
58-
expect(list.length()).toEqual(3);
98+
describe('removeFromBeginning()', () => {
99+
it('Should remove element at front', () => {
100+
list.addAtBeginning(12);
101+
expect(list.removeFromBeginning()).toEqual(12);
102+
expect(list.length()).toEqual(0);
103+
});
104+
105+
it('Should return the element after removing it', () => {
106+
list.addAtBeginning(15);
107+
list.addAtBeginning(16);
108+
expect(list.removeFromBeginning()).toEqual(16);
109+
});
110+
111+
it('Should not throw error if the list is empty', () => {
112+
expect(() => list.removeFromBeginning()).not.toThrow();
113+
});
114+
});
115+
116+
describe('removeFromEnd()', () => {
117+
it('Should return `null` for empty list', () => {
118+
expect(list.removeFromEnd()).toEqual(null);
119+
});
120+
121+
it('Should remove element at last', () => {
122+
list.addAtBeginning('Hello');
123+
list.addAtBeginning(14);
124+
list.addAtEnd(15);
125+
expect(list.removeFromEnd()).toEqual(15);
126+
});
127+
128+
it('Should reduce the lengh of the list', () => {
129+
list.addAtBeginning(14);
130+
list.addAtEnd(15);
131+
expect(list.length()).toEqual(2);
132+
133+
list.removeFromEnd();
134+
expect(list.length()).toEqual(1);
135+
});
136+
137+
it('Should return the last element after removing it', () => {
138+
list.addAtBeginning(14);
139+
list.addAtEnd(15);
140+
expect(list.removeFromEnd()).toEqual(15);
141+
});
59142
});
60143

61-
it('Should remove element at front using list.removeFromBeginning()', () => {
62-
list.addAtBeginning(12);
63-
expect(list.removeFromBeginning()).toEqual(12);
144+
describe('getLast()', () => {
145+
it('Should return `null` if the list is empty', () => {
146+
expect(list.getLast()).toEqual(null);
147+
});
64148

65-
list.addAtBeginning(15);
66-
list.addAtBeginning(16);
67-
expect(list.removeFromBeginning()).toEqual(16);
68-
expect(list.length()).toEqual(1);
149+
it('Should return 10 as the last item in the list', () => {
150+
list.addAtEnd(15);
151+
list.addAtEnd(23);
152+
list.addAtEnd(33);
153+
list.addAtEnd(10);
154+
expect(list.getLast()).toEqual(10);
155+
});
69156
});
70157

71-
it('Should remove element at last using list.removeFromEnd()', () => {
72-
expect(list.removeFromEnd()).toEqual(null);
158+
describe('getFirst()', () => {
159+
it('Should return `null` if the list is empty', () => {
160+
expect(list.getFirst()).toEqual(null);
161+
});
73162

74-
list.addAtBeginning(15);
75-
list.addAtBeginning(14);
76-
expect(list.removeFromEnd()).toEqual(15);
77-
expect(list.length()).toEqual(1);
163+
it('Should return 15 as the last item in the list', () => {
164+
list.addAtBeginning(15);
165+
list.addAtEnd(23);
166+
list.addAtEnd(33);
167+
list.addAtEnd(10);
168+
expect(list.getFirst()).toEqual(15);
169+
});
78170
});
79171
});
80172
});

‎src/_DataStructures_/LinkedList/index.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ class LinkedList {
1111
}
1212

1313
addAtBeginning(element) {
14-
const node = new Node(element, null);
15-
16-
if (!this.head) {
17-
this.head = node;
18-
} else {
19-
node.next = this.head;
20-
this.head = node;
21-
}
14+
this.head = new Node(element, this.head);
2215
}
2316

2417
addAtEnd(element) {
@@ -37,16 +30,6 @@ class LinkedList {
3730
}
3831
}
3932

40-
length() {
41-
let address = this.head;
42-
let count = 0;
43-
while (address) {
44-
count += 1;
45-
address = address.next;
46-
}
47-
return count;
48-
}
49-
5033
removeFromBeginning() {
5134
if (!this.head) {
5235
return null;
@@ -69,6 +52,41 @@ class LinkedList {
6952
address.next = null;
7053
return data;
7154
}
55+
56+
getFirst() {
57+
if (!this.head) {
58+
return null;
59+
}
60+
return this.head.data;
61+
}
62+
63+
getLast() {
64+
if (!this.head) {
65+
return null;
66+
}
67+
68+
let address = this.head;
69+
70+
while (address.next) {
71+
address = address.next;
72+
}
73+
74+
return address.data;
75+
}
76+
77+
length() {
78+
let address = this.head;
79+
let count = 0;
80+
while (address) {
81+
count += 1;
82+
address = address.next;
83+
}
84+
return count;
85+
}
86+
87+
delete() {
88+
this.head = null;
89+
}
7290
}
7391

7492
module.exports = { LinkedList, Node };

0 commit comments

Comments
 (0)
Please sign in to comment.