Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

GIVEN CODE template class DList { private: struct Node { T data; Node* next; Node* prev; Node(const T& d = T{}, Node* n = nullptr,

GIVEN CODE

template

class DList {

private:

struct Node {

T data;

Node* next;

Node* prev;

Node(const T& d = T{}, Node* n = nullptr, Node* p = nullptr)

: data(d), next(n), prev(p) {}

};

Node* head;

Node* tail;

int sz;

public:

class const_iterator {

public:

const_iterator() : current(nullptr) {}

const T& operator*() const {

return retrieve();

}

const_iterator& operator++() {

current = current->next;

return *this;

}

const_iterator operator++(int) {

const_iterator old = *this;

++(*this);

return old;

}

const_iterator& operator--() {

current = current->prev;

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);

}

protected:

Node* current;

const_iterator(Node* p) : current(p) {}

T& retrieve() const {

return current->data;

}

friend class DList;

};

class iterator : public const_iterator {

public:

iterator() {}

T& operator*() {

return const_iterator::retrieve();

}

const T& operator*() const {

return const_iterator::operator*();

}

iterator& operator++() {

this->current = this->current->next;

return *this;

}

iterator operator++(int) {

iterator old = *this;

++(*this);

return old;

}

iterator& operator--() {

this->current = this->current->prev;

return *this;

}

iterator operator--(int) {

iterator old = *this;

--(*this);

return old;

}

protected:

iterator(Node* p) : const_iterator(p) {}

friend class DList;

};

DList() : head(new Node()), tail(new Node()), sz(0) {

head->next = tail;

tail->prev = head;

}

~DList() {

clear();

delete head;

delete tail;

}

DList(const DList& rhs) : head(new Node()), tail(new Node()), sz(rhs.sz) {

head->next = tail;

tail->prev = head;

for (auto it = rhs.cbegin(); it != rhs.cend(); ++it) {

insert(cend(), *it);

}

}

DList& operator=(const DList& rhs) {

DList copy = rhs;

std::swap(*this, copy);

return *this;

}

DList(DList&& rhs) : head(rhs.head), tail(rhs.tail), sz(rhs.sz) {

rhs.head = nullptr;

rhs.tail = nullptr;

rhs.sz = 0;

}

DList& operator=(DList&& rhs) {

std::swap(head, rhs.head);

std::swap(tail, rhs.tail);

std::swap(sz, rhs.sz);

return *this;

}

iterator insert(iterator it, const T& data) {

Node* p = it.current;

++sz;

return iterator(p)

iterator search(const T& data);

iterator erase(iterator it);

void sort(iterator first, iterator last);
bool empty() const;
int size() const;
iterator begin(){}
iterator end(){}
const_iterator cbegin() const{}
const_iterator cend() const{}
};
template
DList::DList(){
}
template
DList::~DList(){
}
template
DList::DList(const DList& rhs){
}
template
DList& DList::operator=(const DList& rhs){
}
template
DList::DList(DList&& rhs){
}
template
DList& DList::operator=(DList&& rhs){
}
template
typename DList::iterator DList::insert(iterator it, const T& data){
}
template
typename DList::iterator DList::search(const T& data){
}
template
typename DList::iterator DList::erase(iterator it){
}
template
void DList::sort(iterator first, iterator last){
}
template
bool DList::empty() const{
}
template
int DList::size() const{

There is a given code, please complete the code that is marked in BOLD in C++ code language, than you!

Please provide an answer only for that is marked in BOLD!!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions