如何为链表创建复制构造函数
How to make a copy constructor for a Linked List
我想为链表创建一个复制构造函数。我知道如何复制构造函数的数组,但不为链表。谁能给我一个主意,我怎么能做到这一点,谢谢。
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
};
编辑:这就是我想出来的
linked_queue::linked_queue(const linked_queue&其他){
ctr = 0;
front = NULL;
back = NULL;
node *p = other.front;
while ( p != NULL )
{
_push( p->data);
p = p->next;
}
}
只需遍历列表,分配一堆具有相同值的节点,并设置next
指针。最后,设置front
和back
指针和ctr
,完成。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 复制构造函数、赋值运算符C++
- std::ofstream 作为类成员删除复制构造函数?
- 复制构造函数C++无法正确复制指针
- 关于复制构造函数的一个棘手问题
- 为什么调用复制构造函数而不是移动构造函数?
- 填充上编译器生成的复制构造函数之间的不一致
- C++ 对象指针数组的复制构造函数
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 防止在复制构造函数中隐式调用基构造函数
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 具有已删除移动和复制构造函数的类的就地构造
- 复制构造函数隐式转换问题
- 复制构造函数中的递归调用