c++ - How to make a copy constructor for a Linked List -


ok i'm trying make copy constructor linked list. know how copy constructor array not linked list. can give me idea on how can , thanks.

class node  {   public :      double data;       node *next;  /// pointer points next elemnt     node () { next = null; data = 0; }     node (double val ) { next = null; data = val; }  private:    }; 

queue header

class linked_queue {   public :      linked_queue() { front = null; = null; ctr = 0;  }   /// default constructor     bool _empty();       void _size();        void _front();        void _back();        void _push(double);      void pop();       void _display();        ~linked_queue(); /// destructor     linked_queue& operator= ( const linked_queue& rhs );     linked_queue( const linked_queue& other );  private :      int  ctr; /// counter     node  *front;  /// front pointer     node *back;  ///back pointer  }; 

edit : came with

linked_queue::linked_queue( const linked_queue& other ) {

ctr = 0; front = null; = null;  node *p = other.front;  while ( p != null ) {     _push( p->data);     p = p->next; } 

}

just walk through list, allocate bunch of nodes same values, , set next pointers. finally, set front , back pointers , ctr, , you're done.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -