How to remove odd numbers from a linked list in C++

In this program, we will see how to remove odd numbers from a linked list in C++.

This is a very interesting program where we will learn how to remove odd numbers from a linked list. Now if you don’t know what a linked list is or can’t make one please visit this page:¬†Singly Linked Lists in C++ This contains a very easy to understand method of making a linked list.

So when you know how to use the linked list we will learn how to remove odd numbers from it. (Note that the program done here is done without using recursion for further simplicity).

Steps to be followed to remove odd numbers from a Linked list in C++

For removing odd number we first find out the position of the “head“. The head is the starting node in a linked list.

  1. Note that the original starting node might not be even so we will be looking for the first even number in the list and take it as “head”.
  2. Now we will find the rest of the linked list by a simple method.
  3. Since nodes of a linked list are connected by the pointers, we will change the pointers of the current node to the next to next node if the next node isn’t even.
  4. We shall continue this process until we encounter a NULL pointer which signifies the end of the list.

 

C++ Program to remove odd numbers from linked list

#include<iostream>
using namespace std;
 struct Node         //We are defining a node  here
    {
        int data;       //data of node here
        Node *next;      //next refers to the address of the next node
        
    };
int main()
    {
       
        struct Node* newnode1 = (struct Node*) malloc(sizeof(struct Node)); //making objects or nodes of the linked list
        struct Node* newnode2 = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* newnode3 = (struct Node*) malloc(sizeof(struct Node));  
        struct Node* newnode4 = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* newnode5 = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* newnode6 = (struct Node*) malloc(sizeof(struct Node));  
        struct Node* newnode7 = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* tempnode = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* head = (struct Node*) malloc(sizeof(struct Node)); 
        struct Node* tmp = (struct Node*) malloc(sizeof(struct Node)); 

        newnode1->data=12;                //inserting data into new node
        newnode2->data=23;
        newnode3->data=32;
        newnode4->data=47;
        newnode5->data=52;
        newnode6->data=64;
        newnode7->data=70;
   
        newnode1->next=newnode2;       //here we are linking the previous node to the next node of the list 
        newnode2->next=newnode3;
        newnode3->next=newnode4;
        newnode4->next=newnode5;      
        newnode5->next=newnode6;
        newnode6->next=newnode7;
        newnode7->next=NULL;
        
        tmp=newnode1;
        cout<<"\nThe original linked list is ::";     //printing the original list  
        while(1)
        { 
               
           cout<< tmp->data <<" "; 
            tmp=tmp->next;
            if(tmp->next==NULL)
            {
                cout<< tmp->data <<" "; 
                break;
                
            }
           
        }
       
        head=newnode1;                      //here were finding the head or the starting position of the node
        while(1)
        {
            
            if((head->data)%2!=0)   
                head=head->next;
            else
                break;
        }
        tmp=head;
        
        while(1)                //now modifying the rest of the list
        { 
            tempnode=head->next;  
           
           if((tempnode->data)%2!=0)
            {
                head->next=tempnode->next;
            }
            else
             head=tempnode;
            if((tempnode->next)==NULL)
                break;
            
        }
        cout<<"\nThe modified linked list is ::";                 //printing the new list 
        while(1)
        { 
               
           cout<< tmp->data <<" "; 
            tmp=tmp->next;
            if(tmp->next==NULL)
            {
                cout<< tmp->data <<" "; 
                break;
                
            }
           
        }
}

The output of the above program:

How to remove odd numbers from a linked list in C++

 

This is a very simple program. I tried my best to do it in the easiest way possible. Hope you like it. If you have any doubts, please comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *