c++ - Making a copy constructor more flexible for ADT queue -
i have constructed copy constructor adt queue. copy constructor works fine. want improve code, don't know how shorten make more flexible. code given below:
template <typename t> queue <t>::queue(const queue & other) { if (other.first == nullptr) { first = nullptr; nrofelements = 0; } else { node* savefirst; node* walker; first = other.first; walker = new node(first->data); savefirst = walker; while (first->next != nullptr) { walker->next = new node(first->next->data); walker = walker->next; first = first->next; } walker->next = nullptr; first = savefirst; } this->nrofelements = other.nrofelements; }
the class queue
contains inner private node
class contains pointers first
, next
, etc:
private: int nrofelements; class node { public: node* next; t data; node(t data) { this->data = data; } }; node* first;
so, appreciate suggestions/examples of how copy constructor code above improved, i'm bit lost on task.
Comments
Post a Comment