Question
#include template class DList{ struct Node{ T data_; Node* next_; Node* prev_; Node(const T& data=T{},Node* next=nullptr, Node* prev=nullptr){ data_=data; next_=next; prev_=prev; } }; Node* front_;
#include | |
template | |
class DList{ | |
struct Node{ | |
T data_; | |
Node* next_; | |
Node* prev_; | |
Node(const T& data=T{},Node* next=nullptr, Node* prev=nullptr){ | |
data_=data; | |
next_=next; | |
prev_=prev; | |
} | |
}; | |
Node* front_; | |
Node* back_; | |
public: | |
DList(){ | |
front_=nullptr; | |
back_=nullptr; | |
} | |
void push_front(const T& data); | |
~DList(); | |
class const_iterator{ | |
Node* curr_; | |
const_iterator(Node* n){ | |
curr_=n; | |
} | |
public: | |
const_iterator(){ | |
curr_=nullptr; | |
} | |
const_iterator& operator++(){**YOUR CODE**} | |
const_iterator operator++(int){**YOUR CODE**} | |
const_iterator& operator--(){**YOUR CODE**} | |
const_iterator operator--(int){**YOUR CODE**} | |
bool operator==(const_iterator rhs){**YOUR CODE**} | |
bool operator!=(const_iterator rhs){**YOUR CODE**} | |
const T& operator*()const{**YOUR CODE**} | |
}; | |
class iterator:public const_iterator{ | |
public: | |
iterator(); | |
iterator& operator++(){**YOUR CODE**} | |
iterator operator++(int){**YOUR CODE**} | |
iterator& operator--(){**YOUR CODE**} | |
iterator operator--(int){**YOUR CODE**} | |
T& operator*(){**YOUR CODE**} | |
const T& operator*()const{**YOUR CODE**} | |
}; | |
const_iterator cbegin() const{ } | |
iterator begin(){**YOUR CODE** } | |
const_iterator cend() const{**YOUR CODE**} | |
iterator end(){**YOUR CODE**} | |
}; | |
template | |
void DList | |
**YOUR CODE** | |
} | |
template | |
DList | |
**YOUR CODE** | |
} |
To support this add the following functions to each of DList and Sentinel classes:
iterator begin(); const_iterator cbegin() const;
These functions returns an iterator/const_iterator to the first node in the list, end() if list is empty.
iterator end(); const_iterator cend() const;
these functions returns an iterator/const_iterator to the node just after the last node in the linked list.
The const_iterator and iterators must support the following operations:
bool operator==(const_iterator rhs); - function returns true if rhs and current object refer to the same node bool operator!=(const_iterator rhs); - function returns true if rhs and current object does not refer to the same node
iterator operator++(); //prefix ++ iterator operator++(int); //postfix ++ const_iterator operator++(); //prefix ++ const_iterator operator++(int); //postfix ++
iterator advances to next node in list if iterator is not currently at end(). The iterator returned depends if it is prefix or postfix. prefix operator returns iterator to current node. postfix operator returns iterator to node before the increment.
iterator operator--(); //prefix -- iterator operator--(int); //postfix -- const_iterator operator--(); //prefix -- const_iterator operator--(int); //postfix --
iterator refer to the node "previous" to the linked list. The iterator returned depends if it is prefix or postfix. prefix operator returns iterator to current node. postfix operator returns iterator to node before the decrement.
const T& operator*() const; //in const_iterator const T& operator*() const; //in iterator
returns a const reference to data in the node referred to by the iterator.
T& operator*();
returns a reference to data in the node referred to by the iterator.
Please implement iterator and const_iterator for functions DList in C++ code language, thank you!
Step 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