void list::erase()//Deletes a node
{
cout<< "Enter a position: “;
cin>> pos;
if(pos==1)
{
current=head;
head=head->ptr;
}
else
{
current=head;
for(i=0; i<pos-1; i++)
{
temp=current;
current=current->ptr;
}
temp->ptr=current->ptr;
}
cout<< “Erased string: " << current->content<<”\t”;
current=head;
while(current!=NULL)
{
cout<<current->content<<"\t";
current=current->ptr;
}
}
I need to display a message in case I delete a string that is not in the list: There is no such string. What should I do?
You are deleting the string by the position, not by matching the string content of the node. For displaying the message by specifying position,
something like this:
n= total number of nodes in the list
cin>>pos;
if(pos>n)
cout<<"You can't delete\n";
else
{
// your above same function here
}
If you want to delete by the string content:
char str[100];
cin>>str; // input string you want to delete in the list
prev=NULL;
curr=head;
while(curr!=NULL && (strcmp(curr->info,str)!=0))
{
prev=curr;
curr=curr->next; // or curr= curr->ptr;
}
if(curr==NULL)
{
cout<<"No matching string exists\n";
}
else
{
prev->next= curr->next;
free(curr);
}
Sorry for misleading. I applied the first part. Thanks for either methods.