In my project I am trying to make it so I can make a copy of a linked list then display its contents. Currently I have it working but every time I try to exit the program crashes. I removed the instance of the copy constructor being used in the main part of the program so the problem seems to be coming from there. Here is the code:
struct ListNode{
int num;
struct ListNode *next;
};
ListNode *head;
List::List( const List& org){
ListNode *copy=org.head;
ListNode *temp;
if(copy==NULL){
head=NULL;
}
else{
head=copy;
while(copy!=NULL){
temp=new ListNode;
temp->num=copy->num;
temp=temp->next;
copy=copy->next;
}
}
}
Please note that I know that some of the brackets {} are a little off the program itself works up until I try to exit so I'm wonder how I would prevent the program from crashing?
or, in C++11 this is defined as||, there are a few of these (I forget the name)s in C++<---notice the absence of 11, in C++ in general; like:[is a way of typing{or something, I've never used them, but they're there!head? Copy constructor has to change the change the state ofthis.ListNodeobjects, but you don't actually store pointers to them anywhere. At the end, you havethis->head == org->headand a bunch of leaked objects.