SDUT OJ 数据结构实验之链表五:单链表的拆分
数据结构实验之链表五:单链表的拆分
Problem Description
Input
第二行依次输入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 数据结构实验之链表五:单链表的拆分的更多相关文章
- SDUT OJ 数据结构实验之图论五:从起始点到目标点的最短步数(BFS)
数据结构实验之图论五:从起始点到目标点的最短步数(BFS) Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss P ...
- SDUT OJ 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT 3344 数据结构实验之二叉树五:层序遍历
数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...
- SDUT 3402 数据结构实验之排序五:归并求逆序数
数据结构实验之排序五:归并求逆序数 Time Limit: 40MS Memory Limit: 65536KB Submit Statistic Problem Description 对于数列a1 ...
- SDUT 3377 数据结构实验之查找五:平方之哈希表
数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT OJ 数据结构实验之链表九:双向链表
数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之链表四:有序链表的归并
数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT OJ 数据结构实验之链表三:链表的逆置
数据结构实验之链表三:链表的逆置 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
随机推荐
- Python 小练习二 数据库MySQL、Redis
import pymysql,redis def op_mysql(host,user,password,db,sql,port=3306,charset='utf8'): conn = pymysq ...
- [转载]C语言 宏
当然宏定义非常重要的,它可以帮助我们防止出错,提高代码的可移植性和可读性等. 1,防止一个头文件被重复包含 #ifndef COMDEF_H#define COMDEF_H //头文件内容 …#end ...
- 模仿慕课网一步步发布一个开源库到 JCenter
H:\common\-common-25.2.2\upload.gradle // Bintray /* Properties properties = new Properties() proper ...
- 算法描述》关于LIS的nlogn方法
上次TYVJ有一道裸LIS,然而我当时直接打了一个N^2暴力就草草了事,然后就ZZ了,只拿了60分,其实NlogN的LIS和N^2的差的不多,只是没有N^2,好想罢了,鉴于某学弟的要求,所以就重现一下 ...
- 张超超OC基础回顾01_类的创建,申明属性,以及本质
一. 类的声明和实现&规则 1.如何编写类的声明 以@interface开头 , 以@end结尾, 然后再class name对应的地方写上 事物名称, 也就是类名即可 注意: 类名的首字符必 ...
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- 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 ...
- 在Linux中监视IO性能
dd命令 iostat命令 理解iostat的各项输出 iostat的应用实例 附:在Windows中监视IO性能 延伸阅读 dd命令 dd其实是工作于比较低层的一个数据拷贝和转换的*nix平台的工具 ...
- tensorflow 中 feed的用法
Feed 上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作 ...
- redis内存优化方法
先来认识2个redis配置参数 hash-max-ziplist-entries : hash内部编码压缩列表的最大值,默认512 hash-max-zipmap-value : hash内部编码压缩 ...