Skip to content

Commit

Permalink
update: clean up reverse_it
Browse files Browse the repository at this point in the history
Signed-off-by: Rohith-Raju <[email protected]>
  • Loading branch information
Rohith-Raju committed Mar 7, 2024
1 parent 409a9b4 commit 1cba9f1
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions include/LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,31 @@ template <class T> class LinkedList {
virtual bool operator!=(__self other);

/* Operator overloads */
virtual __self &operator++(); // prefix
virtual __self operator++(int); // postfix
virtual T &operator*();
virtual T *operator->();
__self &operator++(); // prefix
__self operator++(int); // postfix
T &operator*();
T *operator->();
};

class reverse_it : public forward_it {
private:
LinkedList<T> *list = nullptr;

public:
typedef reverse_it __self;

reverse_it(Node *ptr) : forward_it(ptr) {}
reverse_it(LinkedList<T> *list) : forward_it(list->head) {
this->list = list;
}

reverse_it() : forward_it(nullptr) {}

~reverse_it() {
if (list != nullptr) {
list->clean();
delete list;
}
}
};
};

Expand All @@ -117,11 +131,11 @@ template <typename T>
typename LinkedList<T>::reverse_it LinkedList<T>::rbegin() {
LinkedList<T> *temp = new LinkedList<T>(*this);
temp->reverse();
return reverse_it(temp->head);
return reverse_it(temp);
}

template <typename T> typename LinkedList<T>::reverse_it LinkedList<T>::rend() {
return reverse_it(nullptr);
return reverse_it();
}

template <typename T>
Expand Down

0 comments on commit 1cba9f1

Please sign in to comment.