Suggestions for improvement:
Add a default constructor to
node
so that it gets initialized properly when constructed.struct node{ node(int in = 0) : info(in), link(NULL) {} int info; node *link; };
You don't need
current
as a member ofLinked_List
. It's useful only in some functions as a function varible.Implement
Initialize_List()
usingInsert_Last
. That keeps the function cleaner. It also avoids redundant code.void Initialize_List(){ cout<<"Enter Number OF Nodes"<<endl; int num; cin>>num; for(int i =0;i<num;i++){ cout<<"Enter New Node Info :"<<endl; int info; cin >> info; this->Insert_Last(info); } }
Insert_Last
had assumptions about which is a valid pointer and which is not, which won't be true if you started using to fromInitialize_List
. It can be simplified to:void Insert_Last(int x){ count++; node* current=new node; current->info=x; if ( first == NULL ) { first = last = current; } else { last->link=current; last=current; } }
Implementation of
Copy_List
you posted deleted all items from the first argument and put them in the second argument. I am not sure that was the purpose. If you want to keep the contents of the first argument unchanged and want to copy its contents to the second argument, an alternate method is necessary. Here's what I came up with:void Copy_List (Linked_List &n,Linked_List &m){ Linked_List temp; node* current = n.first; for ( ; current != NULL; current = current->link ) { temp.Insert_Last(current->info); } current = temp.first; for ( ; current != NULL; current = current->link ) { m.Insert_Last(current->info); } }
You don't have a destructor in
Linked_List
. The default implementation provided by the compiler won't release the memory allocated by the class. In order to deallocate the memory allocated by the class, you need to implement a destructor.~Linked_List() { node* current=first; while ( current != NULL ) { node* temp = current->link; delete current; current = temp; } }