C Program to Segregate Even and Odd Nodes in a Linked List

This C Program modifies the linked list such that all even numbers appear before all the odd number in the modified linked list.

Here is a source code of the C program that modifies the linked list such that all even numbers appear before all the odd number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Modify the Linked List such that All Even Numbers
  3.  * appear before all the Odd Numbers in the Modified Linked List 
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *next;
  12. };
  13.  
  14. void create(struct node **);
  15. void generate_evenodd(struct node *, struct node**);
  16. void release(struct node **);
  17. void display(struct node *);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL, *q = NULL;
  22.     int key, result;
  23.  
  24.     printf("Enter data into the list\n");
  25.     create(&p);
  26.     printf("Displaying the nodes in the list:\n");
  27.     display(p);
  28.     generate_evenodd(p, &q);
  29.     printf("Displaying the list with even and then odd:\n");
  30.     display(q);
  31.     release(&p);
  32.  
  33.     return 0;
  34. }
  35.  
  36. void generate_evenodd(struct node *list, struct node **head)
  37. {
  38.     struct node *even = NULL, *odd = NULL, *temp;
  39.     struct node *reven, *rodd;
  40.     while (list != NULL)
  41.     {
  42.         temp = (struct node *)malloc(sizeof(struct node));
  43.         temp->num = list->num;
  44.         temp->next = NULL;
  45.         if (list->num % 2 == 0)
  46.         {
  47.             if (even == NULL)
  48.             {
  49.                 even = temp;
  50.             }
  51.             else
  52.             {
  53.                 reven->next = temp;
  54.             }
  55.             reven = temp;
  56.         }
  57.         else
  58.         {
  59.             if (odd == NULL)
  60.             {
  61.                 odd = temp;
  62.             }
  63.             else
  64.             {
  65.                 rodd->next = temp;
  66.             }
  67.             rodd = temp;
  68.         }
  69.         list = list->next;
  70.     }
  71.     reven->next = odd;
  72.     *head = even;
  73. }
  74.  
  75. void create(struct node **head)
  76. {
  77.     int c, ch;
  78.     struct node *temp, *rear;
  79.  
  80.     do
  81.     {
  82.         printf("Enter number: ");
  83.         scanf("%d", &c);
  84.         temp = (struct node *)malloc(sizeof(struct node));
  85.         temp->num = c;
  86.         temp->next = NULL;
  87.         if (*head == NULL)
  88.         {
  89.             *head = temp;
  90.         }
  91.         else
  92.         {
  93.             rear->next = temp;
  94.         }
  95.         rear = temp;
  96.         printf("Do you wish to continue [1/0]: ");
  97.         scanf("%d", &ch);
  98.     } while (ch != 0);
  99.     printf("\n");
  100. }
  101.  
  102. void display(struct node *p)
  103. {
  104.     while (p != NULL)
  105.     {
  106.         printf("%d\t", p->num);
  107.         p = p->next;
  108.     }
  109.     printf("\n");
  110. }
  111.  
  112. void release(struct node **head)
  113. {
  114.     struct node *temp = *head;
  115.     *head = (*head)->next;
  116.     while ((*head) != NULL)
  117.     {
  118.         free(temp);
  119.         temp = *head;
  120.         (*head) = (*head)->next;
  121.     }
  122. }

$ gcc evenodd.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1	2	3	4	5	6	
Displaying the list with even and then odd:
2	4	6	1	3	5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.