Monday, December 29, 2014

Modify the All Even Numbers in c pgm



Modify the Linked List such that All Even Numbers appear before all the Odd Numbers in the Modified Linked List

#include 
#include 
#include 
 
struct node
{
    int num;
    struct node *next;
};
 
void create(struct node **);
void generate_evenodd(struct node *, struct node**);
void release(struct node **);
void display(struct node *);
 
int main()
{
    struct node *p = NULL, *q = NULL;
    int key, result;
 
    printf("Enter data into the list\n");
    create(&p);
    printf("Displaying the nodes in the list:\n");
    display(p);
    generate_evenodd(p, &q);
    printf("Displaying the list with even and then odd:\n");
    display(q);
    release(&p);
 
    return 0;
}
 
void generate_evenodd(struct node *list, struct node **head)
{
    struct node *even = NULL, *odd = NULL, *temp;
    struct node *reven, *rodd;
    while (list != NULL)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = list->num;
        temp->next = NULL;
        if (list->num % 2 == 0)
        {
            if (even == NULL)
            {
                even = temp;
            }
            else
            {
                reven->next = temp;
            }
            reven = temp;
        }
        else
        {
            if (odd == NULL)
            {
                odd = temp;
            }
            else
            {
                rodd->next = temp;
            }
            rodd = temp;
        }
        list = list->next;
    }
    reven->next = odd;
    *head = even;
}
 
void create(struct node **head)
{
    int c, ch;
    struct node *temp, *rear;
 
    do
    {
        printf("Enter number: ");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = c;
        temp->next = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->next = temp;
        }
        rear = temp;
        printf("Do you wish to continue [1/0]: ");
        scanf("%d", &ch);
    } while (ch != 0);
    printf("\n");
}
 
void display(struct node *p)
{
    while (p != NULL)
    {
        printf("%d\t", p->num);
        p = p->next;
    }
    printf("\n");
}
 
void release(struct node **head)
{
    struct node *temp = *head;
    *head = (*head)->next;
    while ((*head) != NULL)
    {
        free(temp);
        temp = *head;
        (*head) = (*head)->next;
    }
}

Output
 
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

No comments:

Post a Comment