好的,所以我正在尝试为链表创建一个复制构造函数。我知道如何为数组复制构造函数,但不知道如何为链表复制构造函数。有人可以告诉我如何做到这一点,谢谢。
class node
{
public :
double data;
node *next; /// pointer that points to next elemnt
node () { next = NULL; data = 0; }
node (double val ) { next = NULL; data = val; }
private:
};
队列头
class linked_queue
{
public :
linked_queue() { front = NULL; back = 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
};
编辑:这就是我想出的
链接队列::链接队列(常量链接队列&其他){
ctr = 0;
front = NULL;
back = NULL;
node *p = other.front;
while ( p != NULL )
{
_push( p->data);
p = p->next;
}
}