C Data Structures - Linked List Other Related Topics

C - Delete even nodes of the Linked List



Deleting even nodes of a linked list requires traverse through the list and deleting even nodes one by one. It requires creating two nodes - oddNode and evenNode. If head is not null, make oddNode to first odd node of list and evenNode to first even node of the list. If both are not null, delete the evenNode and adjust links. Move both nodes to next set of odd-even nodes. Repeat the process till any or both nodes become null.

The function deleteEvenNodes is created for this purpose. It is a 4-step process.

void deleteEvenNodes(struct Node** head_ref) {
  if(*head_ref != NULL) {
  
    //1. if head is not null create nodes -
    //   evenNode and oddNode
    struct Node* oddNode = *head_ref;
    struct Node* evenNode = (*head_ref)->next;

    while(oddNode != NULL && evenNode != NULL) {
      
      //2. while oddNode and evenNode are not null
      //   make next of oddNode as next of evenNode 
      //   and free evenNode  
      oddNode->next = evenNode->next;
      free(evenNode);

      //3. and make oddNode as next of oddNode
      oddNode = oddNode->next;
      
      //4. Update oddNode and evenNode
      if(oddNode != NULL)
        evenNode = oddNode->next;
    }
  }   
} 

The below is a complete program that uses above discussed concept of deleting even nodes of a linked list.

#include <stdio.h>
#include <stdlib.h>

//node structure
struct Node {
  int data;
  struct Node* next;
};

//Add new element at the end of the list
void push_back(struct Node** head_ref, int newElement) {  
  struct Node *newNode, *temp;
  newNode = (struct Node*)malloc(sizeof(struct Node)); 
  newNode->data = newElement;  
  newNode->next = NULL;
  if(*head_ref == NULL) {
    *head_ref = newNode; 
  } else {
    temp = *head_ref;
    while(temp->next != NULL) {
      temp = temp->next;
    }    
    temp->next = newNode;
  }
}

//delete even nodes of the list
void deleteEvenNodes(struct Node** head_ref) {
  if(*head_ref != NULL) {
    struct Node* oddNode = *head_ref;
    struct Node* evenNode = (*head_ref)->next;
    while(oddNode != NULL && evenNode != NULL) {
      oddNode->next = evenNode->next;
      free(evenNode);
      oddNode = oddNode->next;
      if(oddNode != NULL)
        evenNode = oddNode->next;
    }
  }   
} 

//display the content of the list
void PrintList(struct Node* head_ref) {
  struct Node* temp = head_ref;
  if(head_ref != NULL) {
    printf("The list contains: ");
    while (temp != NULL) {
      printf("%i ",temp->data);
      temp = temp->next;  
    }
    printf("\n");
  } else {
    printf("The list is empty.\n");
  }   
}

// test the code 
int main() {
  struct Node* MyList = NULL;

  //Add five elements in the list.
  push_back(&MyList, 10);
  push_back(&MyList, 20);
  push_back(&MyList, 30);
  push_back(&MyList, 40);
  push_back(&MyList, 50);

  //Display the content of the list.
  PrintList(MyList);

  //delete even nodes of the list
  deleteEvenNodes(&MyList);

  printf("After deleting even nodes.\n");
  //Display the content of the list.
  PrintList(MyList);

  return 0; 
}

The above code will give the following output:

The list contains: 10 20 30 40 50 
After deleting even nodes.
The list contains: 10 30 50 

5