Skip to content

Commit 7c79002

Browse files
committed
fix test 100 for DoublyLinkedList
1 parent 33f28f3 commit 7c79002

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

data-structures/DoublyLinkedList/__test__/test_DoublyLinkedList.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,25 @@ int main(int argc, char* argv[]){
161161
dList.change_at(2, 6); // 3 9 6 7 2 1 6
162162
dList.change_eq_all(6, 1); // 3 9 1 7 2 1 1
163163
dList.change_eq_first(1, 4); // 3 9 4 7 2 1 1
164-
dList.change_eq_last(1, 5); // 3 9 4 7 2 1 5
164+
dList.change_eq_last(2, 5); // 3 9 4 7 5 1 1
165165
assert(dList.size() == 7);
166166
assert(dList.front() == 3);
167-
assert(dList.back() == 5);
167+
assert(dList.back() == 1);
168168
assert(dList.get_at(3) == 7);
169-
assert(dList.contains(2) == 4);
169+
assert(dList.contains(5) == 4);
170170
assert(dList.get_at(0) == 3);
171171
assert(dList.get_at(1) == 9);
172172
assert(dList.get_at(2) == 4);
173173
assert(dList.get_at(3) == 7);
174-
assert(dList.get_at(4) == 2);
174+
assert(dList.get_at(4) == 5);
175175
assert(dList.get_at(5) == 1);
176-
assert(dList.get_at(6) == 5);
176+
assert(dList.get_at(6) == 1);
177177
dList.print();
178178
dList.print_reverse();
179-
dList.reverse(); // 5 1 2 7 4 9 3
180-
assert(dList.get_at(0) == 5);
179+
dList.reverse(); // 1 1 5 7 4 9 3
180+
assert(dList.get_at(0) == 1);
181181
assert(dList.get_at(1) == 1);
182-
assert(dList.get_at(2) == 2);
182+
assert(dList.get_at(2) == 5);
183183
assert(dList.get_at(3) == 7);
184184
assert(dList.get_at(4) == 4);
185185
assert(dList.get_at(5) == 9);

data-structures/DoublyLinkedList/include/DoublyLinkedList.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,28 +286,6 @@ void DoublyLinkedList<T>::print_reverse() const{
286286
}
287287

288288
// Private functions
289-
template <typename T>
290-
typename DoublyLinkedList<T>::_Node *DoublyLinkedList<T>::get_node(const size_t index) const{
291-
if(index == 0) {return _head;}
292-
if(index == _size - 1) {return _tail;}
293-
_Node *curr = nullptr;
294-
if(index < _size / 2){
295-
// from head
296-
curr = _head;
297-
for(size_t i = 0; i < index; ++i){
298-
curr = curr->_next;
299-
}
300-
}
301-
else{
302-
// from tail
303-
curr = _tail;
304-
for(size_t i = _size - 1; i > index; --i){
305-
curr = curr->_prev;
306-
}
307-
}
308-
return curr;
309-
}
310-
311289
template <typename T>
312290
size_t DoublyLinkedList<T>::search(const T &value, bool from_head) const{
313291
_Node *current = nullptr;
@@ -330,6 +308,28 @@ size_t DoublyLinkedList<T>::search(const T &value, bool from_head) const{
330308
return current ? curr_index : std::numeric_limits<size_t>::max();
331309
}
332310

311+
template <typename T>
312+
typename DoublyLinkedList<T>::_Node *DoublyLinkedList<T>::get_node(const size_t index) const{
313+
if(index == 0) {return _head;}
314+
if(index == _size - 1) {return _tail;}
315+
_Node *curr = nullptr;
316+
if(index < _size / 2){
317+
// from head
318+
curr = _head;
319+
for(size_t i = 0; i < index; ++i){
320+
curr = curr->_next;
321+
}
322+
}
323+
else{
324+
// from tail
325+
curr = _tail;
326+
for(size_t i = _size - 1; i > index; --i){
327+
curr = curr->_prev;
328+
}
329+
}
330+
return curr;
331+
}
332+
333333
// Destructor
334334
template <typename T>
335335
DoublyLinkedList<T>::~DoublyLinkedList(){

0 commit comments

Comments
 (0)