Skip to content

Commit

Permalink
fix test 100 for DoublyLinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
bitdove committed Sep 29, 2024
1 parent 33f28f3 commit 7c79002
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,25 @@ int main(int argc, char* argv[]){
dList.change_at(2, 6); // 3 9 6 7 2 1 6
dList.change_eq_all(6, 1); // 3 9 1 7 2 1 1
dList.change_eq_first(1, 4); // 3 9 4 7 2 1 1
dList.change_eq_last(1, 5); // 3 9 4 7 2 1 5
dList.change_eq_last(2, 5); // 3 9 4 7 5 1 1
assert(dList.size() == 7);
assert(dList.front() == 3);
assert(dList.back() == 5);
assert(dList.back() == 1);
assert(dList.get_at(3) == 7);
assert(dList.contains(2) == 4);
assert(dList.contains(5) == 4);
assert(dList.get_at(0) == 3);
assert(dList.get_at(1) == 9);
assert(dList.get_at(2) == 4);
assert(dList.get_at(3) == 7);
assert(dList.get_at(4) == 2);
assert(dList.get_at(4) == 5);
assert(dList.get_at(5) == 1);
assert(dList.get_at(6) == 5);
assert(dList.get_at(6) == 1);
dList.print();
dList.print_reverse();
dList.reverse(); // 5 1 2 7 4 9 3
assert(dList.get_at(0) == 5);
dList.reverse(); // 1 1 5 7 4 9 3
assert(dList.get_at(0) == 1);
assert(dList.get_at(1) == 1);
assert(dList.get_at(2) == 2);
assert(dList.get_at(2) == 5);
assert(dList.get_at(3) == 7);
assert(dList.get_at(4) == 4);
assert(dList.get_at(5) == 9);
Expand Down
44 changes: 22 additions & 22 deletions data-structures/DoublyLinkedList/include/DoublyLinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,28 +286,6 @@ void DoublyLinkedList<T>::print_reverse() const{
}

// Private functions
template <typename T>
typename DoublyLinkedList<T>::_Node *DoublyLinkedList<T>::get_node(const size_t index) const{
if(index == 0) {return _head;}
if(index == _size - 1) {return _tail;}
_Node *curr = nullptr;
if(index < _size / 2){
// from head
curr = _head;
for(size_t i = 0; i < index; ++i){
curr = curr->_next;
}
}
else{
// from tail
curr = _tail;
for(size_t i = _size - 1; i > index; --i){
curr = curr->_prev;
}
}
return curr;
}

template <typename T>
size_t DoublyLinkedList<T>::search(const T &value, bool from_head) const{
_Node *current = nullptr;
Expand All @@ -330,6 +308,28 @@ size_t DoublyLinkedList<T>::search(const T &value, bool from_head) const{
return current ? curr_index : std::numeric_limits<size_t>::max();
}

template <typename T>
typename DoublyLinkedList<T>::_Node *DoublyLinkedList<T>::get_node(const size_t index) const{
if(index == 0) {return _head;}
if(index == _size - 1) {return _tail;}
_Node *curr = nullptr;
if(index < _size / 2){
// from head
curr = _head;
for(size_t i = 0; i < index; ++i){
curr = curr->_next;
}
}
else{
// from tail
curr = _tail;
for(size_t i = _size - 1; i > index; --i){
curr = curr->_prev;
}
}
return curr;
}

// Destructor
template <typename T>
DoublyLinkedList<T>::~DoublyLinkedList(){
Expand Down

0 comments on commit 7c79002

Please sign in to comment.