CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Dec 2011
    Posts
    10

    Unhappy How to delete all values in the linked list

    Hello.

    I have made this program for linked list, but i have one problem with it. I want to make a function that will delete all same values I inuput in the list.

    Example: if user inputs 5 I want to delete all 5 for the list.

    I have tried doing this with the loop inside regular function that erase single value.

    This is funnction that erases single value:
    Code:
    void list::Delete (int a)
    {
        node *temp=head;
    
        if (temp==NULL)
            return ;
    
        if (temp->getNext()==NULL)
        {
    
            delete temp;
            head=NULL;
        }
        else
        {
            node *prev;
    
            do
                {
                    if (temp->getBroj()==a)
                        break;
    
                    prev=temp;
                    temp=temp->getNext();
                }while (temp!=NULL);
    
                prev->setNext(temp->getNext());
    
           }
    }
    This is my attempt to make function:
    Code:
    else
        {
            bool change=false;
    
            node *prev;
    
            do
            {
                change=false;
                do
                {
                    if (temp->getBroj()==a)
                    {
                        change=true;
                        break;
                    }
    
                    prev=temp;
                    temp=temp->getNext();
                }while (temp!=NULL);
    
                prev->setNext(temp->getNext());
    
            }while (!change);
        }

    Thank you
    Last edited by depecheSoul; February 15th, 2012 at 01:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured