delete prev elem if..

This is a discussion on delete prev elem if.. within the C++ Programming forums, part of the General Programming Boards category; Hello I have home work in c++: write function whose delete those elements who next element is even. For example ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    delete prev elem if..

    Hello I have home work in c++: write function whose delete those elements who next element is even.

    For example i have this list: 4 2 2, the result of this function mus be: 2. other example: 4 1 2 2, result: 4 2.

    I write such function, but with this examples my function make errorin other way my function works correctly. Where can be error in this function?:

    structure:
    Code:
    struct elem 
    {
    	int n;
    	elem *next;
    };

    function:
    Code:
    void remake(elem *&first)
    {
        elem *curr=first;
        elem *prev=NULL;
        elem *pprev=NULL;
           
       while(curr!=NULL)
       {
    	   pprev=prev;
           prev=curr;
           curr=curr->next;
                     
           if(curr!=NULL)
    	   {
    		   if(curr->n%2==0)
    		   {
    			   if(pprev==NULL)
    			   {
    				   delete prev;
                                       first=curr;
                                       prev=curr;
    			   }
    			   else
    			   {
    				   pprev->next=curr;
    				   delete prev;
    				   prev=pprev;
    			   }
    		   }
    	   }
       }
    };

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Can you describe what type of error you are getting? A segmentation fault or something?

    Also, remake() is a bad name for a function. Be imaginative.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Why remake() is bad, i like it. Transform will be better?

    Not exactly error I get, but wrong result:
    for input data 4 2 2 I get output result -572662307.

    I think there is some problems with NULL's somewhere. But I dont know where..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In your case where you're deleting the head of the list, should prev not get reset to NULL (rather than curr)?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by xeniox View Post
    Why remake() is bad, i like it. Transform will be better?
    Well, it doesn't tell you anything about what the function does. remove_before_even() would be a better name, for example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21