I was wondering if someone could help me with a copy
constructor function for this linked list program. It's pretty
generic and just processes a name and age value.
Also, how would you invoke the copy constructor once it's
defined?
Any help with this would be great. Thanks for your time.
Here's the class definition:
class LL {
protected:
struct Node {
char * Name;
int Age;
Node * Next;
Node * Prev;
};
int Length;
Node * Head;
Node * Tail;
public:
LL();
LL(const LL & obj);
~LL();
void Insert(char * Name, int Age);
void InsertNode(Node * newNode);
void Delete(char * Name, int Age);
void DeleteNode(Node * delNode);
void DisplayNodes() const;
void DestroyList();
void SortList_Ascend();
void SortList_Descend();
void ReverseList();
void Search(char *Name, int Age);
void SearchNode(Node * srchNode);
};
and here's my attempt with the copy constructor...
I pretty sure this is incorrect.
LL::LL()
{
Length = 0;
Head = NULL;
Tail = NULL;
}
LL::LL(const LL & obj)
{
if ((Length = obj.Length) == 0)
{
Head = 0;
return;
}
Node * o = obj.Head;
Head = new Node;
Node * n = Head;
Node * n_prev;
n->Name = new char[strlen(o->Name) + 1];
strcpy(n->Name, o->Name);
n->Age = o->Age;
while((o = o->Next) != 0)
{
n_prev = n;
n = new Node;
n->Name = new char[strlen(o->Name) + 1];
strcpy(n->Name, o->Name);
n->Age = o->Age;
n_prev->Next = n;
}
}

New Topic/Question
Reply




MultiQuote





|