Write a C Program to split linked list into Even and Odd linked lists

By | 06.04.2017

C Program to split linked list into Even and Odd lists


Write a C Program to split linked list into Even and Odd linked lists. Here’s simple Program to split linked list into Even and Odd linked lists in C Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C Program to split linked list into Even and Odd linked lists which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to split linked list into Even and Odd lists */

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

struct node
{
        int info;
        struct node *link;
};

struct node *create_list(struct node *start);
void display(struct node *start);
struct node *addatbeg(struct node *start,int data);
struct node *addatend(struct node *start,int data);
void EvenOdd(struct node *start1,struct node **start2,struct node **start3 );

int main()
{
        struct node *start1=NULL,*start2=NULL,*start3=NULL;
        start1=create_list(start1);
        EvenOdd(start1, &start2, &start3);
        printf("\nOriginal List L1 is :\n");
        display(start1);
        printf("Odd Numbers List L2 is :\n");
        display(start2);
        printf("Even Numbers List L3 is :\n");
        display(start3);

        return 0;

}/*End of main()*/

void EvenOdd(struct node *start1,struct node **start2,struct node **start3 )
{
        struct node *p1=start1, *p2=*start2, *p3=*start3;

        while(p1!=NULL)
        {
                if( (p1->info)%2 != 0 )
                {
                        if(*start2==NULL)
                                *start2 = addatbeg(*start2,p1->info);
                        else
                                *start2 = addatend(*start2,p1->info);
                }
                else
                {
                        if(*start3==NULL)
                                *start3 = addatbeg(*start3,p1->info);
                        else
                                *start3 = addatend(*start3,p1->info);
                }
                p1=p1->link;
        }
}/*End of EvenOdd()*/

struct node *create_list(struct node *start)
{
        int i,n,data;
        printf("Enter the number of nodes : ");
        scanf("%d",&n);
        start=NULL;
        if(n==0)
                return start;

        printf("\nEnter the element to be inserted : ");
        scanf("%d",&data);
        start=addatbeg(start,data);

        for(i=2;i<=n;i++)
        {
                printf("\nEnter the element to be inserted : ");
                scanf("%d",&data);
                start=addatend(start,data);
        }
        return start;
}/*End of create_list()*/

void display(struct node *start)
{
        struct node *p;
        if(start==NULL)
        {
                printf("Empty\n");
                return;
        }
        p=start;

        while(p!=NULL)
        {
                printf("%d ",p->info);
                p=p->link;
        }
        printf("\n\n");
}/*End of display() */

struct node *addatbeg(struct node *start,int data)
{
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        tmp->link=start;
        start=tmp;
        return start;
}/*End of addatbeg()*/

struct node *addatend(struct node *start,int data)
{
        struct node *p,*tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        p=start;
        while(p->link!=NULL)
                p=p->link;
        p->link=tmp;
        tmp->link=NULL;
        return start;
}/*End of addatend()*/

OUTPUT : :


/* C Program to split linked list into Even and Odd lists */

************** OUTPUT *****************

Enter the number of nodes : 8

Enter the element to be inserted : 1

Enter the element to be inserted : 2

Enter the element to be inserted : 3

Enter the element to be inserted : 4

Enter the element to be inserted : 5

Enter the element to be inserted : 6

Enter the element to be inserted : 7

Enter the element to be inserted : 8

Original List L1 is :
1 2 3 4 5 6 7 8

Odd Numbers List L2 is :
1 3 5 7

Even Numbers List L3 is :
2 4 6 8

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 2 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
alric

how tf do i change editor theme
5 * tho

Last edited 2 years ago by alric