我正在制作一个双向链表,它只存储一个数据类型(通用),我需要一个复制构造函数。我以为我做对了,但是当我使用导师提供的文件对其进行测试时,它不起作用.当我尝试调试它时,问题似乎出在我刚刚设置head = NULL的链表类的构造函数中。这是一个正确的构造函数
//this is the struct i am using
struct ListItem
{
T value;
ListItem<T> *next;
ListItem<T> *prev;
ListItem(T theVal)
{
this->value = theVal;
this->next = NULL;
this->prev = NULL;
}
};
template <class T>
List<T>::List()
{
head=NULL;
}
template <class T>
List<T>::List(const List<T>& otherList)
{
ListItem<T> *Headold=otherList.getHead();
if (Headold==NULL)
{
head=NULL; //if otherlist head is NULL,new list head=0
}
else
{
head=new ListItem<T>(Headold->value); //initializing head to the string value
//storing in temporary pointers
ListItem<T> *oldnode=Headold;
ListItem<T> *newnode=head;
while (temp->next!=NULL)
{
oldhead=oldhead->next;
//making new node every instance
newnode->next=new ListItem<T>(oldhead->next->value);
ListItem<T> *newnodenext=newnode->next;
newnodenext->prev=newnode; //setting the previous pointer of the new node
}
}
}