数据结构实验之链表五:单链表的拆分

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

输入N个整数顺序建立一个单链表,将该单链表拆分成两个子链表,第一个子链表存放了所有的偶数,第二个子链表存放了所有的奇数。两个子链表中数据的相对次序与原链表一致。

Input

第一行输入整数N;;
第二行依次输入N个整数。

Output

第一行分别输出偶数链表与奇数链表的元素个数; 
第二行依次输出偶数子链表的所有数据;
第三行依次输出奇数子链表的所有数据。

Sample Input

10
1 3 22 8 15 999 9 44 6 1001

Sample Output

4 6
22 8 44 6
1 3 15 999 9 1001

Hint

不得使用数组!

拆开,新建两条子链;

#include <stdio.h>
#include <stdlib.h> struct node
{
int data;
struct node *next;
}; int main()
{
struct node *head, *tail, *p;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
int i,n,x=0,y=0;
scanf("%d",&n);
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;
} struct node *head1, *head2, *tail1, *tail2, *p1, *p2;
head1 = (struct node *)malloc(sizeof(struct node));
head2 = (struct node *)malloc(sizeof(struct node));
head1->next = NULL;
head2->next = NULL;
tail1 = head1;
tail2 = head2; p = head->next;
while(p){
if(!(p->data%2)){
p1 = (struct node *)malloc(sizeof(struct node));
p1->data = p->data;
p1->next = NULL;
tail1->next = p1;
tail1 = p1;
x++;
}
else{
p2 = (struct node *)malloc(sizeof(struct node));
p2->data = p->data;
p2->next = NULL;
tail2->next = p2;
tail2 = p2;
y++;
}
p = p->next;
} printf("%d %d\n",x,y);
p1 = head1->next;
while(p1->next){
printf("%d ",p1->data);
p1 = p1->next;
} printf("%d\n",p1->data); p2 = head2->next;
while(p2->next){
printf("%d ",p2->data);
p2 = p2->next;
} printf("%d\n",p2->data); return 0;
}

SDUT OJ 数据结构实验之链表五:单链表的拆分的更多相关文章

  1. SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)

    数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...

  2. SDUT OJ 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

  3. SDUT 3344 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  4. SDUT 3402 数据结构实验之排序五:归并求逆序数

    数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...

  5. SDUT 3377 数据结构实验之查找五:平方之哈希表

    数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...

  6. SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除

    数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  7. SDUT OJ 数据结构实验之链表九:双向链表

    数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  8. SDUT OJ 数据结构实验之链表四:有序链表的归并

    数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

  9. SDUT OJ 数据结构实验之链表三:链表的逆置

    数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

随机推荐

  1. 14-EasyNetQ之用Future Publish发布预定中事件

    很多商业流程需要事件在未来的时间按照预定时间发布.例如,在初次与客户接触后,可以在未来某个时间去电话回访客户.EasyNetQ可以用它的Future Publish功能帮你实现这个功能.举例:这里我们 ...

  2. Spring Cloud Zuul 1(API 网关服务)

    API网关是一个更为智能的应用服务器,它的存在就像是整个微服架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤. 它实现的功能包括:请求路由.负载均衡.校验过滤等功能. Spring ...

  3. Python三元运算和lambda表达式

    一.三元运算 1.定义:三元运算是if-else 语句的快捷操作,也被称为条件运算. 2.结构: [on_true]  if  [expression]  else  [on_false] 3.示例: ...

  4. centos7使用frabric自动化部署LNMP

    1.创建lnmp.py文件 $ vim lnmp.py ------------------------> #!/usr/bin/env python from fabric.colors im ...

  5. 因采用 Flask 原生 WSGI 出现 "Broken pipe" 报错的故障处理

    :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...

  6. 深入剖析SolrCloud(一)

    作者:洞庭散人 出处:http://phinecos.cnblogs.com/ 本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由 ...

  7. 4-1 线程安全性-原子性-atomic-1

    我们发现在不做任何同步的情况下,我们计算的累加结果是错误的. com.mmall.concurrency.example.count.CountExample2 C:\Users\ZHONGZHENH ...

  8. codeforce452DIV2——F. Letters Removing

    题意:给一个字符串和m个操作,每次给出l,r,c,把字符串中l-r这段区间的字符为c的字符删掉,求最后的字符串.(n,m<=2e5)线段树.注意这个区间修改和普通区间修改的区别. 他们都是用树状 ...

  9. Mysql 设置外部访问

    mysql> use mysql; mysql> GRANT ALL ON *.* TO user@' WITH GRANT OPTION; mysql -h172. -uuser -p1 ...

  10. 关于PHP的一个坑爹问题(页面刷新)

    最近在用PHP做一个服务端和一个客户端,在快要完工的时候,出现了一个重大问题---- 当在客户端手动输入IP和端口的时候,一按连接,OK,连接成功,嘻嘻,就在我自以为大功告成的时候,来了个晴天霹雳,一 ...