Question
In c++. I am needing four functions created to be used in a linked list class I am creating in c++. I know the crbegin()
In c++. I am needing four functions created to be used in a linked list class I am creating in c++. I know the crbegin() and crend() functions already exist in STL, but I am needing to write code for those and the rbegin() and rend() functions. I am doing the part of the test app, so I only need help with creating those four functions.
In case it is needed, this is my code for the const_iterator and iterator classes.
class const_iterator {
protected:
Node *current;
T & retrieve() const {return current->data; }
const_iterator( Node* p ) : current(p) {}
friend class List
public:
const_iterator(): current(nullptr) {}
T & operator*() const {
return retrieve();
}
const_iterator & operator++() {
current = current->next;
return *this;
}
const_iterator operator++(int) {
const_iterator old = *this;
++(*this);
return old;
}
bool operator==(const const_iterator & rhs) const {
return current == rhs.current;
}
bool operator!=(const const_iterator & rhs) const {
return !( *this == rhs);
}
};
/ow add iteratior class
class iterator: public const_iterator {
protected:
iterator (Node *p) : const_iterator(p) {}
friend class List
public:
iterator() {}
T & operator *() {
return const_iterator:: retrieve();
}
const T & operator* () const {
return const_iterator::operator*();
}
iterator & operator++ () {
this->current = const_iterator::current->next;
return * this;
}
iterator operator++ (int) {
iterator old = *this;
**(*this);
return old;
}
};
Containers in the STL provides methods that you can use to walk through a container from back to front. Add rbegin(), rend(), crbegin(), and crend() methods to the list class that each return an iterator or const_interator. Note that rbegin() and crbegin() need to return an iterator at the END of the list and rend() and crend() need to return iterators to just past the actual first node in the list (i.e., the head node, just like end() and cend() do with the tail node). Modify the test app from an eariler question to demonstrate that your methods work as expectedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started