単一のデータ型 (ジェネリック) のみを格納する二重リンク リストを作成しています。そのためのコピー コンストラクターが必要です。正しいと思っていましたが、インストラクターから提供されたファイルを使用してテストしたところ、機能しませんでした。デバッグしようとすると、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
}
}
}