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

不得使用数组!

Source

 #include <iostream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <algorithm>

 using namespace std;

 struct node
 {
     int data;
     struct node *next;
 };

 int cnt1,cnt2;

 //申请空间
 struct node *arr_mal(struct node *p)
 {
     p = (struct node *)malloc(sizeof(struct node));
     return p;
 }

 //创建链表
 void arr_create(struct node *head,int n)
 {
     struct node *p=NULL,*tail=NULL;
     tail = head;
     while(n--)
     {
         p = arr_mal(p);
         scanf("%d",&p ->data);
         tail ->next = p;
         tail = tail ->next;
     }
 }

 //输出链表
 void arr_prin(struct node *head)
 {
     struct node *p=NULL;
     p = head ->next;
     if(p != NULL)
     {
         printf("%d",p ->data);
         p = p ->next;
     }
     while(p != NULL)
     {
         printf(" %d",p ->data);
         p = p ->next;
     }
     printf("\n");
 }

 //拆分
 void arr_split(struct node *head1,struct node *head2)
 {

     struct node *p1=NULL,*p2=NULL,*tail=NULL;
     p1 = head1;
     p2 = head2;
     while(p1 ->next != NULL)
     {
         )
         {
             p1 = p1 ->next;
             cnt1++;
         }
         else
         {
             tail = p1 ->next;
             p1 ->next = tail ->next;
             tail ->next = NULL;
             p2 ->next = tail;
             p2 = p2 ->next;
             cnt2++;
         }
     }
 }

 int main()
 {
     int n;
     cnt1=cnt2=;
     struct node *head1=NULL,*head2=NULL;
     head1 = arr_mal(head1);
     head2 = arr_mal(head2);
     scanf("%d",&n);
     arr_create(head1,n);
     arr_split(head1,head2);
     printf("%d %d\n",cnt2,cnt1);
     arr_prin(head2);
     arr_prin(head1);
     ;
 }

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

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

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

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

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

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

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

  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-3402_数据结构实验之排序五:归并求逆序数

    数据结构实验之排序五:归并求逆序数 Time Limit: 50 ms Memory Limit: 65536 KiB Problem Description 对于数列a1,a2,a3-中的任意两个数 ...

  7. php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))

    php数据结构课程---2.链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的)) 一.总结 一句话总结: php是弱类型语言,变量即可表示数值,也可表示对象:链表节点的数据域的值就 ...

  8. [C++]数据结构:线性表之(单)链表

    一 (单)链表 ADT + Status InitList(LinkList &L) 初始化(单)链表 + void printList(LinkList L) 遍历(单)链表 + int L ...

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

    数据结构实验之查找五:平方之哈希表 Time Limit: 400 ms Memory Limit: 65536 KiB Problem Description 给定的一组无重复数据的正整数,根据给定 ...

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

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一个按先序输入的字符序列,如abd ...

随机推荐

  1. ChinaSys 一些心得

    这周不要脸的和老板一起去了 ChinaSys,可以说整个中国搞系统最nb的一批人的学术交流了.一圈报告听下来, 有几点心得,不多,可能也没有那么深刻. 系统领域的开源框架并不多 搞系统和搞AI,搞算法 ...

  2. Python提升“技术逼格”的6个方法

    1 列表生成式和生成器 from numpy import randoma = random.random(10000) lst = []for i in a: lst.append(i * i) # ...

  3. 《老师说的都对》- Alpha冲刺阶段博客目录

    项目小组:<老师说的都对> 项目成员:孙浩杰,谭明耀,宋自康,孙肖肖,王明鑫,王观山 Github仓库地址-PCES 一.Scrum Meeting 第六周会议记录 第七周会议记录 二.测 ...

  4. phper使用MySQL 针对千万级的大表要怎么优化?

    有需要学习交流的友人请加入交流群的咱们一起,群内都是1-7年的开发者,希望可以一起交流,探讨PHP,swoole这块的技术 或者有其他问题 也可以问,获取swoole或者php进阶相关资料私聊管理即可 ...

  5. IO测试工具 - 用于IO测试 ; linux benchmarks

    IO测试工具,用于磁盘IO测试,下面进行使用列表进行记录: iozone fio dd ioping iotop iostat bonnie++ crystalDisk Atto as-ssd-ben ...

  6. linux路径问题

    在 Linux 中,简单的理解一个文件的路径,指的就是该文件存放的位置,例如,在<Linux文件系统的层次结构>中提到的 /home/cat 就表示的是 cat 文件所存放的位置.只要我们 ...

  7. WPF应用中对WindowsFormHost内容进行裁剪

    问题1:  WPF中在使用WindowsFormsHost调用WinFrom控件时,若在WindowsFormsHost上层添加了WPF控件,该控件不会显示出来. <Grid> <W ...

  8. Java并发J.U.C学习总结

    转载自http://www.cnblogs.com/chenpi/结合自己理解稍有添加自己的理解 阅读目录 JSR 166及J.U.C Executor框架(线程池. Callable .Future ...

  9. How to: Initialize Business Objects with Default Property Values in XPO 如何:在 XPO 中用默认属性值初始化业务对象

    When designing business classes, a common task is to ensure that a newly created business object is ...

  10. Toggle the WinForms Ribbon Interface 切换 WinForms 功能区界面

    In this lesson, you will learn how to enable/disable the Ribbon User Interface in your application. ...