10 1 3 22 8 15 999 9 44 6 1001
4 6 22 8 44 6 1 3 15 999 9 1001
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
} Node ;
Node *creatList(int n) //逆序建表,链表的初始化
{
Node *head,*p;
int i;
head=(Node *)malloc(sizeof(Node ));
head->next=NULL;
for(i=0;i<n;i++)
{
p=(Node *)malloc(sizeof(Node ));
scanf("%d",&p->data);
p->next=head->next;
head->next=p;
}
return head;
}
Node *split(Node *head1)//链表的拆分
{
Node *head2,*p,*q;
int cnt1=0,cnt2=0;
head2=(Node *)malloc(sizeof(Node ));
head2->next=NULL;
p=head1->next;
head1->next=NULL;
q=p->next;
while(p)
{
if(p->data%2==0)
{
p->next=head1->next;
head1->next=p;
cnt1++;
}
else
{
p->next=head2->next;
head2->next=p;
cnt2++;
}
p=q;
if(q!=NULL)
q=q->next;
}
printf("%d %d\n",cnt1,cnt2);
return head2;
}
void Print(Node *head)//遍历链表
{
Node *s;
s=head->next;
while(s)
{
if(s->next==NULL)
printf("%d\n",s->data);
else
printf("%d ",s->data);
s=s->next;
}
}
void main()
{
int n;
Node *head1,*head2,*s;
scanf("%d",&n);
head1=creatList(n);
head2=split(head1);
Print(head1);
Print(head2);
}
原文:http://blog.csdn.net/lsmrsun/article/details/51331219