Skip to content

Commit 098a7d6

Browse files
committed
--update: removeAt(index) with refactor in tests
1 parent 20db10b commit 098a7d6

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

src/_DataStructures_/LinkedList/LinkedList.test.js

+51-39
Original file line numberDiff line numberDiff line change
@@ -169,51 +169,63 @@ describe('Data Structures: Linked Lists', () => {
169169
});
170170
});
171171

172-
describe('getAt(index)', () => {
172+
describe('Get/Add/Remove at specific positions', () => {
173173
beforeEach(() => {
174174
list.addAtBeginning('Hello');
175175
list.addAtEnd('There!');
176176
list.addAtEnd('Welcome');
177177
});
178178

179-
it('Should return `null` for empty list regardless of index value', () => {
180-
list.delete();
181-
expect(list.getAt(10)).toEqual(null);
182-
});
183-
184-
it('Should return the node for given index', () => {
185-
expect(list.getAt(1).data).toEqual('There!');
186-
});
187-
188-
it('Should return the last element for large index', () => {
189-
expect(list.getAt(10).data).toEqual('Welcome');
190-
});
191-
});
192-
193-
describe('addAt(index, value)', () => {
194-
beforeEach(() => {
195-
list.addAtBeginning('Hello');
196-
list.addAtEnd('There!');
197-
list.addAtEnd('Welcome');
198-
});
199-
200-
it('Should add at the beginning of empty list', () => {
201-
list.delete();
202-
list.addAt(10, 'Boom');
203-
expect(list.getFirst().data).toEqual('Boom');
204-
});
205-
206-
it('Should add at the end of the list if the index is out of bound', () => {
207-
list.addAtEnd(1010);
208-
list.addAt(10, 'Boom');
209-
expect(list.getLast().data).toEqual('Boom');
210-
});
211-
212-
it('Should add new element at the given position', () => {
213-
list.addAt(2, 'Stranger');
214-
expect(list.getAt(2).data).toEqual('Stranger');
215-
expect(list.getAt(1).data).toEqual('There!');
216-
expect(list.getAt(0).data).toEqual('Hello');
179+
describe('getAt(index)', () => {
180+
it('Should return `null` for empty list regardless of index value', () => {
181+
list.delete();
182+
expect(list.getAt(10)).toEqual(null);
183+
});
184+
185+
it('Should return the node for given index', () => {
186+
expect(list.getAt(1).data).toEqual('There!');
187+
});
188+
189+
it('Should return the last element for large index', () => {
190+
expect(list.getAt(10).data).toEqual('Welcome');
191+
});
192+
});
193+
194+
describe('addAt(index, value)', () => {
195+
it('Should add at the beginning of empty list', () => {
196+
list.delete();
197+
list.addAt(10, 'Boom');
198+
expect(list.getFirst().data).toEqual('Boom');
199+
});
200+
201+
it('Should add at the end of the list if the index is out of bound', () => {
202+
list.addAtEnd(1010);
203+
list.addAt(10, 'Boom');
204+
expect(list.getLast().data).toEqual('Boom');
205+
});
206+
207+
it('Should add new element at the given position', () => {
208+
list.addAt(2, 'Stranger');
209+
expect(list.getAt(2).data).toEqual('Stranger');
210+
expect(list.getAt(1).data).toEqual('There!');
211+
expect(list.getAt(0).data).toEqual('Hello');
212+
});
213+
});
214+
215+
describe('removeAt(index)', () => {
216+
it('Should return null for empty list', () => {
217+
list.delete();
218+
expect(list.removeAt(10)).toEqual(null);
219+
});
220+
221+
it('Should remove last element for large index value', () => {
222+
expect(list.removeAt(10)).toEqual('Welcome');
223+
});
224+
225+
it('Should remove the element at given index value', () => {
226+
expect(list.removeAt(3)).toEqual('Welcome');
227+
expect(list.removeAt(2)).toEqual('There!');
228+
});
217229
});
218230
});
219231
});

src/_DataStructures_/LinkedList/index.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,31 @@ class LinkedList {
104104
}
105105

106106
previous.next = new Node(element, previous.next);
107-
return 0;
107+
return null;
108+
}
109+
110+
removeAt(index) {
111+
if (!this.head) {
112+
return null;
113+
}
114+
115+
if (index >= this.length()) {
116+
return this.removeFromEnd();
117+
}
118+
119+
let address = this.head;
120+
let previous = address;
121+
let count = index;
122+
123+
while (count) {
124+
address = address.next;
125+
previous = address;
126+
count -= 1;
127+
}
128+
129+
const { data } = address.data;
130+
previous.next = address.next.next;
131+
return data;
108132
}
109133

110134
length() {

0 commit comments

Comments
 (0)