#include <stdio.h>
#include <stdlib.h>
/* a node of the singly linked list */
struct Node
{
    int data;
    struct Node *next;
};
   void segregateEvenOdd(struct Node **head_ref)
{
    struct Node *end = *head_ref;
    struct Node *prev = NULL;
    struct Node *curr = *head_ref;
       /* Get pointer to the last node */
    while (end->next != NULL)
        end = end->next;
       struct Node *new_end = end;
       /* Consider all odd nodes before the first even node
       and move then after end */
    while (curr->data %2 != 0 && curr != end)
    {
        new_end->next = curr;
        curr = curr->next;
        new_end->next->next = NULL;
        new_end = new_end->next;
    }
       // 10->8->17->17->15
    /* Do following steps only if there is any even node */
    if (curr->data%2 == 0)
    {
        /* Change the head pointer to point to first even node */
        *head_ref = curr;
           /* now current points to the first even node */
        while (curr != end)
        {
            if ( (curr->data)%2 == 0 )
            {
                prev = curr;
                curr = curr->next;
            }
            else
            {
                /* break the link between prev and current */
                prev->next = curr->next;
                   /* Make next of curr as NULL  */
                curr->next = NULL;
                   /* Move curr to end */
                new_end->next = curr;
                   /* make curr as new end of list */
                new_end = curr;
                   /* Update current pointer to next of the moved node */
                curr = prev->next;
            }
        }
    }
       /* We must have prev set before executing lines following this
       statement */
    else prev = curr;
       /* If there are more than 1 odd nodes and end of original list is
      odd then move this node to end to maintain same order of odd
      numbers in modified list */
    if (new_end!=end && (end->data)%2 != 0)

    {
        prev->next = end->next;
        end->next = NULL;
        new_end->next = end;
    }
    return;
}
   /* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning  */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
        (struct Node*) malloc(sizeof(struct Node));
       /* put in the data  */
    new_node->data  = new_data;
       /* link the old list off the new node */
    new_node->next = (*head_ref);
       /* move the head to point to the new node */
    (*head_ref)    = new_node;
}
   /* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
    while (node!=NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}
   /* Drier program to test above functions*/
int main()
{     /* Start with the empty list */
    struct Node* head = NULL;
       /* Let us create a sample linked list as following
      0->2->4->6->8->10->11 */
       push(&head, 11);
    push(&head, 10);
    push(&head, 8);
    push(&head, 6);
    push(&head, 4);
    push(&head, 2); <
    push(&head, 0);
       printf("\nOriginal Linked list \n");
    printList(head);
       segregateEvenOdd(&head);
       printf("\nModified Linked list \n");
    printList(head);
       return 0;
}
Output-

Original Linked list 0 2 4 6 8 10 11
Modified Linked list 0 2 4 6 8 10 11
/

Search

Project Categories

Recent Posts

Mail Management System
Posted on 2019-07-18
Online food ordering system
Posted on 2019-07-18
Library Management System
Posted on 2019-07-17
Health center system project
Posted on 2019-07-17
Gym Management System
Posted on 2019-07-17
furniture management system
Posted on 2019-07-17
Electronic shop management system
Posted on 2019-07-17
Automobile Workshop Management
Posted on 2019-07-17
Online Visa Processing System
Posted on 2019-07-17
Inventory management System
Posted on 2019-07-17
petrol-management system
Posted on 2019-07-17
Cloths management system
Posted on 2019-07-17
Society Management system
Posted on 2019-07-17
Mall management system
Posted on 2019-07-17
school management system
Posted on 2019-07-17
Sales Order Processing System
Posted on 2019-07-17
Retail sales management
Posted on 2019-07-17
Raw Materials Management
Posted on 2019-07-17
railway reservation system
Posted on 2019-07-17
purchase and sales management system
Posted on 2019-07-17
Placement Management System
Posted on 2019-07-17
Pet Shop Management System
Posted on 2019-07-17
petrol pump management system
Posted on 2019-07-17
Patient Information System
Posted on 2019-07-17
news agency system
Posted on 2019-07-17
Cinema Booking System
Posted on 2019-07-17
Medical Store System
Posted on 2019-07-17
leave management System
Posted on 2019-07-17
Laboratory Information Management System
Posted on 2019-07-17
content management system
Posted on 2019-07-17
Inventory management System
Posted on 2019-07-17
Institute Management System
Posted on 2019-07-17
Hotel management System
Posted on 2019-07-17
Gym Management System
Posted on 2019-07-17
Garage Management System
Posted on 2019-07-17
Furniture shop management system
Posted on 2019-07-17
Fisheries management  System
Posted on 2019-07-17
Fertilizer scheduling system
Posted on 2019-07-17
online eye care system
Posted on 2019-07-17
Dental Clinic Management System
Posted on 2019-07-17
Cyber Café Management
Posted on 2019-07-17
Milk Billing System
Posted on 2019-07-17
Colddrink management system
Posted on 2019-07-17
Cable management System
Posted on 2019-07-17
Beauty parlor management system
Posted on 2019-07-17
Facebook Clone
Posted on 2019-05-28
Dance Class Management System
Posted on 2019-05-24
Library Management System
Posted on 2019-05-24
Cab Management System
Posted on 2019-05-23
Blood Bank Management system
Posted on 2019-05-23
Beauty Parlour Management System
Posted on 2019-05-23
vissa proccesing system
Posted on 2019-05-23
Toll Plazza
Posted on 2019-05-23
BILLING APPLICATION
Posted on 2019-05-23
FLORICULTURE MANAGEMENT SYSTEM
Posted on 2019-05-23
Car On Rent
Posted on 2019-05-23
E-commers Shop
Posted on 2019-05-23

Sign In