链表的拆分
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;
}
#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;
}
相关文章推荐
- js实现sleep功能
- 好玩的注解和反射
- js 加密
- Design Support library简介
- css position属性
- Android_NewBird之Asynctask的基本使用(1)
- android studio shape使用
- 将HTML5 Canvas的内容保存为图片借助toDataURL实现
- HTTP_CLIENT_IP、HTTP_X_FORWARDED_FOR、REMOTE_ADDR
- mysql字段长度是什么意思
- Ubuntu 14.04 下通过 XKB 修改键盘映射, 实现自定义按键
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
- mac 常用终端命令与使用
- Bag-of-words model
- 小议安全威胁情报
- linux中编译安装log4cpp
- PyGobject(二十三)布局容器之AspectFrame
- Spring Framework Artifacts
- 正则表达式
- Windows 安装ELK