we just started studying about linked lists
and i wrote a simple code to build up a link list.
than post the sum of all the numbers inside the list
my problem is that i need to delete all even numbers
and i cant figure out what is wrong with the code i wrote.
its going in an inifinite loop inside the display function

[2}
(the reason for temp is to save the head pointer to first node
any other way of doing so instead of declaring new temp node for it?)]

void delete_pairs(struct Node *&head)
{ 
   struct Node *temp,*step ; 

   if (head==NULL)
      return;

   while(head!=NULL && head->_data%2==0)
   {   
            temp = head ; 
            head = head -> _next ; 
            delete temp ;  
   }

   step=head;
   
   while (step!=NULL)
   {
      if (step-> _data %2==0)
      { 
         temp = step ; 
         step = step -> _next ; 
         delete temp ; 
      }       
      else
         step = step -> _next ; 
   }

}

rest of the program (because maybe something just went on the dark side there):

/////////////

#include <iostream> 
#include <cstdlib>
#include <fstream>

///////////////////

using std::cin;
using std::cout;
using std::endl;
using std::cerr;

//////////////////

struct Node
{ 
   int _data ; 
   struct Node *_next ; 
}; 

/////////////////////////

void display_list_sum(struct Node *head);
void read_list(struct Node *&head);
void delete_pairs(struct Node *&head);

//////////////////

int main()
{
     struct Node *head=NULL;

     read_list(head);
     
     display_list_sum(head);
     
     delete_pairs(head);
     
     display_list_sum(head);
 
}

//////////////////////////////////////

void display_list_sum(struct Node *head)
{ 
   int sum=0;
   struct Node *temp;
   temp=head;
   
   while (temp != NULL)
   { 
      sum+= temp-> _data ; 
      temp = temp-> _next ; 
   }
   cout<< sum<< endl;
}
 
///////////////////

void read_list(struct Node *&head)
{
     struct Node *temp;
     int num;
     
     cin >> num ; 
     while (num!=0)
     { 
        temp = new (std::nothrow) struct Node ;
        if (temp == NULL)  
           exit(EXIT_FAILURE);
        temp -> _data = num ; 
        temp -> _next = head ;
        head = temp ; 
        cin >> num ; 
     }
}

Recommended Answers

All 2 Replies

Your deletion routine is not correct - you are leaving the pointer from the previous entry in the list dangling. Example: if you enter the values 3, 2, 1 in your list, you will have a list in order from head of 1 - 2 - 3.

When you go to remove the even numbers, you will start at 1 (head) and see that it doesn't need to be removed, so you move to 2. That should be removed. You set "step" to point to the 3, then remove the 2 - but the pointer from 1 - it's still pointing to where 2 was (it has an address in it, though it is invalid).

One way to do it is to have a "trailing" pointer (call it "prev") and set it to NULL. Each time you increment "step" WITHOUT deleting a node, you set "prev" equal to "step" BEFORE you increment "step". Then in the deletion logic, you set "prev->next" equal to "step->next", so the pointer from the previous node now points to the node after the one just deleted.

so confusing :)
got it working thanks alot !

void delete_evens(struct Node *&head)
{ 
   struct Node *temp,*step,*prev ; 

   if (head==NULL)
      return;

   while(head!=NULL && head->_data%2==0)
   {   
            temp = head ; 
            head = head -> _next ; 
            delete temp ;  
   }

   step=head;
   
   while (step!=NULL)
   {
      if (step-> _data %2==0)
      { 
         temp = step ; 
         step = step -> _next ; 
         prev->_next=step;
         delete temp ; 
      }       
      else
      {
         prev=step;
         step = step -> _next ; 
      }
   }
   step=NULL;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.