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

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. Python 小练习二 数据库MySQL、Redis

    import pymysql,redis def op_mysql(host,user,password,db,sql,port=3306,charset='utf8'): conn = pymysq ...

  2. [转载]C语言 宏

    当然宏定义非常重要的,它可以帮助我们防止出错,提高代码的可移植性和可读性等. 1,防止一个头文件被重复包含 #ifndef COMDEF_H#define COMDEF_H //头文件内容 …#end ...

  3. 模仿慕课网一步步发布一个开源库到 JCenter

    H:\common\-common-25.2.2\upload.gradle // Bintray /* Properties properties = new Properties() proper ...

  4. 算法描述》关于LIS的nlogn方法

    上次TYVJ有一道裸LIS,然而我当时直接打了一个N^2暴力就草草了事,然后就ZZ了,只拿了60分,其实NlogN的LIS和N^2的差的不多,只是没有N^2,好想罢了,鉴于某学弟的要求,所以就重现一下 ...

  5. 张超超OC基础回顾01_类的创建,申明属性,以及本质

    一. 类的声明和实现&规则 1.如何编写类的声明 以@interface开头 , 以@end结尾, 然后再class name对应的地方写上 事物名称, 也就是类名即可 注意: 类名的首字符必 ...

  6. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  7. 434. Number of Segments in a String 字符串中的单词个数

    [抄题]: Count the number of segments in a string, where a segment is defined to be a contiguous sequen ...

  8. 在Linux中监视IO性能

    dd命令 iostat命令 理解iostat的各项输出 iostat的应用实例 附:在Windows中监视IO性能 延伸阅读 dd命令 dd其实是工作于比较低层的一个数据拷贝和转换的*nix平台的工具 ...

  9. tensorflow 中 feed的用法

    Feed 上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作 ...

  10. redis内存优化方法

    先来认识2个redis配置参数 hash-max-ziplist-entries : hash内部编码压缩列表的最大值,默认512 hash-max-zipmap-value : hash内部编码压缩 ...