c++ - assignment operator in double directed circular list adding elements in wrong order -


i have problem assignment operator in double directed circular list. when have list content , assign list content numbers jumbled up. input use 5 20 10 when print list output 5 10 20. code looks this:

#ifndef cddlist_h #define cddlist_h  template <typename t> class circulardoubledirectedlist<t>{ public:      static enum direction{ forward, backward };     circulardoubledirectedlist<t>& operator= (const circulardoubledirectedlist<t>& obj);     void addatcurrent(const t& data);  private:     class node{     public:         t data;         node *next;         node *previous;          node(const t& data){             this->data = data;             this->next = nullptr;             this->previous = nullptr;         };         node(){             this->data = null;             this->next = nullptr;             this->previous = nullptr;         };         ~node(){};     };     node *current;     direction currentdirection;     int numberofelements;   };  template <typename t> circulardoubledirectedlist<t>& circulardoubledirectedlist<t>::operator= (const circulardoubledirectedlist<t>& obj){     if (this !=&obj){         this->currentdirection = obj.currentdirection;         this->current = nullptr;         this->numberofelements = 0;         node* walker = obj.current;         (int = 0; < obj.numberofelements; i++){             walker = walker->previous;             addatcurrent(walker->data);         }     }     return *this; } template <typename t> void circulardoubledirectedlist<t>::addatcurrent(const t& data){     if (this->numberofelements == 0){         node *node = new node(data);         this->current = node;         node->next = node;         node->previous = node;         this->numberofelements++;     }     else{         node *node = new node(data);         node->previous = this->current;         node->next = this->current->next;         this->current->next = node;         this->current = node;         this->current->next->previous=this->current;         this->numberofelements++;     }  } #endif 

i have tried use 2 walkers, changed direction of walker(s), moved walker(s) first , added data second, moved 1 walker backwards , other forwards, etc.

your assignment code adding elements of obj this in reverse order, because it's stepping through previous pointers instead of next. change

walker = walker->previous; 

to

walker = walker->next; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -