By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,219 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

what is the error

P: n/a
jw
//a single link list,the nodes contain an integer value called value
void handler::delete(){
int del;
cout<<"which number do u want to delete"<<endl;
cin>>del;
node *previous;//always behind the sth node

for(node *sth=head;sth!=NULL;sth=sth->next){
if(sth->value==del)
{
if(sth==head)
{
node *deleted=head;
head=head->next;
delete deleted;
}

else
{
node *deleted=sth;
previous->next=deleted->next;
delete deleted;
}
previous=sth;
}
}

}

Dec 8 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
jw wrote:
//a single link list,the nodes contain an integer value called value
void handler::delete(){
int del;
cout<<"which number do u want to delete"<<endl;
cin>>del;
node *previous;//always behind the sth node

for(node *sth=head;sth!=NULL;sth=sth->next){
Drop the last statement in the parentheses:

for (node *sth = head; sth != NULL;) {
if(sth->value==del)
{
if(sth==head)
{
node *deleted=head;
head=head->next;
delete deleted;
If you want to delete all elements with value 'del', then you should stay
here and make sure you check all elements that can become heads:

sth = head;
continue;

If you only want to delete the very first one, you should bail out:

break;
}

else
{
node *deleted=sth;
previous->next=deleted->next;
delete deleted;
Same question: do you want to continue looking for value 'del'? Decide,
and then add the necessary things here.
}
previous=sth;
Add
sth = previous->next;
}
}

}


V
Dec 8 '05 #2

P: n/a
jw

Victor Bazarov wrote:
jw wrote:
//a single link list,the nodes contain an integer value called value
void handler::delete(){
int del;
cout<<"which number do u want to delete"<<endl;
cin>>del;
node *previous;//always behind the sth node

for(node *sth=head;sth!=NULL;sth=sth->next){
Drop the last statement in the parentheses:

i want to delete a node whose value is del so.. for (node *sth = head; sth != NULL;) { this statement searches the list the node which ll be deleted might be
at the head or somewhere else
if(sth->value==del)
{
if(sth==head)
{
node *deleted=head;
head=head->next;
delete deleted;


If you want to delete all elements with value 'del', then you should stay
here and make sure you check all elements that can become heads:

sth = head;
continue;

If you only want to delete the very first one, you should bail out:

break;
}

else
{
node *deleted=sth;
previous->next=deleted->next;
delete deleted;


Same question: do you want to continue looking for value 'del'? Decide,
and then add the necessary things here.
}
previous=sth;


Add
sth = previous->next;
}
}

}


V


Dec 8 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.