飞行的猪哼哼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include<stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int o=0,j=0;
struct node *h1,*h2;
struct node *creat(int n)
{
    int i;
    struct node *p,*h,*t;
    h=(struct node*)malloc(sizeof(struct node));  
    h->next=NULL;
    t=h;
    for  (i=1; i<=n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        t->next=p;
        t=p;
    }
    return h;
}
void print(struct node *h)  
{
    struct node *p;
    p=h->next;
    while (p->next!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("%d\n",p->data);  
}
void *split(struct node *h)  
{
    struct node *p,*t1,*t2;
    p=h->next;
    h1=(struct node *)malloc(sizeof(struct node));  
    h2=(struct node *)malloc(sizeof(struct node));  
    h1->next=NULL;
    h2->next=NULL;
    t1=h1;
    t2=h2;
    while(p!=NULL)
    {
        if (p->data%2==0)
        {
            t1->next=p;
            t1=p;
            o++;
        }
        else
        {
            t2->next=p;
            t2=p;
            j++;
        }
        p=p->next;
    }
    t1->next=NULL;
    t2->next=NULL; 
    return 0;
}

int main()
{
    int n;
    struct node *h;
    scanf("%d",&n);
    h=creat(n); 
    split(h);
    printf("%d %d\n",o,j);
    print(h1);
    print(h2);
    return 0;
}
posted on 2018-08-16 18:04  飞行的猪哼哼  阅读(15)  评论(0)    收藏  举报