数据结构实验之链表五:单链表的拆分
来源:互联网 发布:linux查看当前登录用户 编辑:程序博客网 时间:2024/05/16 13:51
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。
输入
第一行输入整数N;;
第二行依次输入N个整数。
输出
第一行分别输出偶数链表与奇数链表的元素个数;
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。
示例输入
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<string.h>#include<stdlib.h>struct node{int data;struct node *next;}*head1,*head2;int n=0,m=0;struct node *creat(int n){int i;struct node *head,*tail,*p;head=(struct node *)malloc(sizeof(struct node));head->next=NULL;tail=head;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 part(struct node *head){struct node *p1,*p2;head1=(struct node *)malloc(sizeof(struct node));head1->next=NULL;p1=head1;head2=(struct node *)malloc(sizeof(struct node));head2->next=NULL;p2=head2;while(head){if(head->data%2==0){p1->next=head;head=head->next;p1=p1->next;p1->next=NULL;n++;}else{p2->next=head;head=head->next;p2=p2->next;p2->next=NULL;m++;}}}int main(){int N;struct node *head;scanf("%d",&N);head=creat(N);head=head->next;part(head);printf("%d %d\n",n,m);head1=head1->next;while(head1->next!=NULL){printf("%d ",head1->data);head1=head1->next;}printf("%d\n",head1->data);head2=head2->next;while(head2->next!=NULL){printf("%d ",head2->data);head2=head2->next;}printf("%d\n",head2->data);return 0;}
0 0
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- 数据结构实验之链表五:单链表的拆分
- uibutton enable 设置title
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- AndroidManifest.xml中的filter
- OC学习篇之---类的定义
- MHOOK的使用
- 数据结构实验之链表五:单链表的拆分
- OC学习篇之---类的初始化方法和点语法的使用
- 持续集成 JAVA 和 C#
- 采购,接收数据收集SQL汇总(从订单->接收->INVOICE所有数据关联SQL)
- Ubuntu14.04 Caffe安装
- sublime with CRLF line terminators
- 浅谈个人SQL语句优化经验总结
- 解决android开发在大屏手机图片出现内存溢出
- OC学习篇之---类的三大特性(封装,继承,多态)