各ノードに右ポインタと下位ポインタがある2-d単一リンクリストを作成しようとしています。私はコピーコンストラクタの実装に多くの問題を抱えていました。なぜなら、再帰が行く方法であると確信しています。再帰ではかなり悪いです。あなたが見ているものを変更する必要はありますか、それとも大丈夫ですか?ここで再帰を使用した2-dリンクリストコピーコンストラクタ
はヘッダで宣言です:
typedef int elementType;
struct node;
typedef node* nodePtr;
struct node
{
elementType elt;
nodePtr right = NULL;
nodePtr below = NULL;
};
class LinkedList
{
protected:
nodePtr head, tail;
int size;
public:
LinkedList();
LinkedList(const LinkedList &list); // copy constructor
void recursiveCreate(nodePtr ptr);
}
これは私のcppファイルでアップフロントの大きなもの
LinkedList::LinkedList(const LinkedList &list)
{
nodePtr current = NULL;
if (list.head == 0)
head == 0;
else
{
current = list.head;
recursiveCreate(current);
}
}
void LinkedList::recursiveCreate(nodePtr ptr)
{
nodePtr n = new node; //create new node
n->elt = ptr->elt; //copy value into that new node
n->right = ptr->right; //move right n pointer
n->below = n->below; //move right below pointer
recursiveCreate(ptr->right); //call on right node
recursiveCreate(ptr->below); //call on below node
}