单链表的拆分

来源:互联网 发布:数据恢复精灵破解版 编辑:程序博客网 时间:2024/05/18 17:24
#include<stdio.h>
#include<malloc.h>
struct node
{
 int data;
 struct node *next;
};
struct node *creat(int n)
{
 struct node *head,*p,*tail;
 head=(struct node *)malloc(sizeof(struct node));
 head->next=NULL;
 tail=head;
 int i;
 for(i=0;i<n;i++)
 {
  p=(struct node *)malloc(sizeof(struct node));
  scanf("%d",&p->data);
  p->next=NULL;
  tail->next=p;
  tail=p;
 }
 return head;
}
void print(struct node *head,int n)
{
 struct node *p;
 p=head->next;
 int t=n-1;
 while(t--)
 {
  printf("%d ",p->data);
   p=p->next;
 }
 printf("%d\n",p->data);
}
void chai(struct node *head)
{
 struct node *head1,*head2,*p,*p1,*p2;
 int c=0,d=0;
 head1=(struct node *)malloc(sizeof(struct node));
 head1->next=NULL;
 p1=head1;
 head2=(struct node *)malloc(sizeof(struct node));
 head2->next=NULL;
 p2=head2;
 p=head->next;
 while(p)
 {
  if(p->data%2==0)
  {
   c++;
   p1->next=p;
   p1=p;
  }
  else
  {
   d++;
   p2->next=p;
   p2=p;
  }
  p=p->next;
 }
printf("%d %d\n",c,d);
 print(head1,c);
 print(head2,d);
}
int main()
{
 int n;
 struct node *head;
 scanf("%d",&n);
 head=creat(n);
 chai(head);
 return 0;
}

0 0
原创粉丝点击