what is the error

Discussion in 'C++' started by jw, Dec 8, 2005.

  1. jw

    jw Guest

    //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;
    }
    }



    }
    jw, Dec 8, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 8, 2005
    #2
    1. Advertising

  3. jw

    jw Guest

    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
    jw, Dec 8, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,657
  2. JavaQueries
    Replies:
    1
    Views:
    3,651
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,092
  4. Bishop
    Replies:
    1
    Views:
    771
    Bishop
    Feb 24, 2007
  5. juvi
    Replies:
    3
    Views:
    1,048
    Alexey Smirnov
    Jan 22, 2009
Loading...

Share This Page