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

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. 【原】Coursera—Andrew Ng机器学习—Week 1 习题—Linear Regression with One Variable 单变量线性回归

    Question 1 Consider the problem of predicting how well a student does in her second year of college/ ...

  2. Scala介绍

    强大的编程语言 Scala 是一门非常强大的语言,它允许用户使用命令和函数范式进行编写代码,因此,编程时你可以使用常用的命令式语句,就像我们使用 C.Java.PHP 以及很多其他语言一样,而且,你也 ...

  3. Spring总结六:AOP(面向切面编程)

    概述: AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是一种新的 ...

  4. Java 基于quartz实现定时 之一(注解方式配置)

    需要在项目里,导入quartz.jar package cn.zr.pringmvctest.trigger; import org.springframework.context.annotatio ...

  5. ROS源码解读(一)--局部路径规划

    博客转载自:https://blog.csdn.net/xmy306538517/article/details/78772066 ROS局部路径导航包括Trajectory Rollout 和 Dy ...

  6. 利用URL重写实现Session跟踪

    Servlet规范中引入了一种补充的会话管理机制,它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话.这种补充机制要求在响应消息的实体内容中必须包含下一次请求的超链接,并将会话标识号作 ...

  7. Charles常见问题

    Charles常见问题汇总 Charles是一款很好用的抓包修改工具,但是如果你不是很熟悉这个工具的话,肯定会遇到各种感觉很莫名其妙的状况,这里就来帮你一一解答下面再说说charles的一些其他常用的 ...

  8. javascript总结13:循环语句

    1 While循环 While(条件表达式){ 只要条件表达式结果为true,循环一直执行,当条件表达式结果为false的时候,循环终止 } While循环语句需现在循环体外定义变量. 2 for循环 ...

  9. POJ2513 Colored Sticks(Trie+欧拉回路)

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  10. EBS登陆界面个性化

    把完整资料贴出来 Set the profile option Local Login Mask (FND_SSO_LOCAL_LOGIN_MASK). (This profile option is ...