c++ - Insertion at a specific location in a singular linked list -


actually trying insert node @ specific location errors when run insertloc() funcion

the problem in insertloc() function

void linked :: insertloc() {     int loc;   cout<<"\nenter location want insert , starting 1";   cin>>loc;   node *temp=new node;   node *temp1;   cout<<"\nenter info part";   cin>>temp->info;   loc-=1;     if(loc==0)   {     temp->next=start;     start=temp;   }   else   {     temp1=start;     for(int i=0;i<=loc-1;i++)     {       temp1=temp1->next;       cout<<temp->info<<"   ";     }     temp->next=temp1->next;     temp1->next=temp;   } 

}

i think problem not check whether temp1->next equal null in code snippet

else {

temp1=start; for(int i=0;i<=loc-1;i++) {   temp1=temp1->next;   cout<<temp->info<<"   "; } 

also condition of loop invalid. has i < loc-1

i write following way

temp1=start; int = 0;  while ( ++i < loc && temp1->next ) temp1 = temp1->next;  if ( == loc ) {   temp->next = temp1->next;   temp1->next = temp; } 

also declare function as

bool linked :: insertloc( size_t n ); 

and code asks enter location exclude function

  int loc;   cout<<"\nenter location want insert , starting 1";   cin>>loc;   node *temp=new node;   node *temp1;   cout<<"\nenter info part";   cin>>temp->info; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -