我在制作复制构造函数时遇到问题。考虑下面的代码:
在 List.h 中
template <class T>
struct ListNode
{
T value;
ListNode<T> *next;
ListNode<T> *prev;
ListNode(T theVal)
{
this->value = theVal;
this->next = NULL;
this->prev = NULL;
}
};
template <class T>
class List
{
ListNode<T> *head;
public:
List();
List(const List<T>& otherList); // Copy Constructor.
~List();
};
在 list.cpp 中
template <class T>
List<T>::List()
{
head=NULL;
}
template <class T>
List<T>::~List()
{
}
template <class T>
List<T>::List(const List<T>& otherList)
{
}
//我有谷歌的问题。概念很简单。创建一个新的头并为其节点分配旧列表节点的//值。// 所以因为我已经尝试了以下。
ListNode<T> *old = head; // pointer to old list.
ListNode<T> *new;// pointer to new head.
while (old->next!=NULL){
new->value = old->value;
old = old->next;
}
// 唯一的问题是如何创建一个指向我的新复制列表的新头。