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
Post a Comment