mardi 3 février 2015

Shortcomings in the program (Linked Lists: Deletion and Searching)


The following pseudo-code deletes a node from the linked list.



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?


The following pseudo-code searches a node in the linked list.



void list::search()//Searches a node
{
char str[50];
cout<<"Enter a string: ";
cin>> str;
current=head;
while(current!=NULL)
{
pos++;
if(!strcmp(current->content, str))
{
cout<< "Found at "<<pos;
}
current=current->ptr;
}
}


If I search for a string that is not in the list and display a message accordingly: There is no such string. What should I do? Please help.





Aucun commentaire:

Enregistrer un commentaire