您的位置:首页 > 其它

链表的拆分

2016-07-27 14:36 344 查看
#include<stdio.h>

#include<malloc.h>

struct  node

{

    int data;

    struct node *next;

};

int m,b;

struct node *creat(int n)

{

    struct node *head,*p;

    head=(struct node*)malloc(sizeof(struct node));

    head->next=NULL;

    for(int i=0;i<n;i++)

    {

        p=(struct node*)malloc(sizeof(struct node));

        scanf("%d",&p->data);

        p->next=NULL;

        p->next=head->next;

        head->next=p;

    }

    return head;

}

void print(struct node *head)

{

    struct node *p=head->next;

    while(p!=NULL)

    {

        if(p->next!=NULL)

        {

            printf("%d ",p->data);

        }

        else

            printf("%d\n",p->data);

            p=p->next;

    }

}

struct node *chaifen(struct node *head)

{

    struct node *head1,*p,*q;

    head1=(struct node*)malloc(sizeof(struct node));

    head1->next=NULL;

    p=head->next;

    head->next=NULL;

    q=p->next;

    while(p!=NULL)

    {

        if(p->data%2==0)

        {

           p->next=head->next;

           head->next=p;

           b++;

        }

        else

        {

            p->next=head1->next;

            head1->next=p;

            m++;

        }

        p=q;

        if(q!=NULL)

        {

            q=q->next;

        }

    }

    return head1;

}

int main()

{

    int n;

    struct node *head1,*head;

    scanf("%d",&n);

    head=creat(n);

    head1=chaifen(head);

    printf("%d %d\n",b,m);

    print(head);

    print(head1);

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: