PAT甲级——A1133 Splitting A Linked List【25】
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address
is the position of the node, Data
is an integer in [, and Next
is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237 68237 -6 48652 48652 -2 12309 12309 7 00000 00000 0 99999 99999 5 23333 23333 10 00100 00100 18 27777 27777 11 -1
【题解】
新建3条链表,分别存负数,[0,k]的数以及大于k的数。然后将三条链表窜起来
当然,更省事的,就直接使用3个queue分别记录三组数据,然后输出,
两个方法差不多,容器的更方便,但链表的不用额外空间
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 struct Node 5 { 6 int val, next; 7 }List[100010]; 8 int head, n, k; 9 int main() 10 { 11 cin >> head >> n >> k; 12 while (n--) 13 { 14 int address, data, next; 15 cin >> address >> data >> next; 16 List[address].val = data; 17 List[address].next = next; 18 } 19 int head1 = 100001, head2 = 100002, head3 = 100003;//分别是负数、中间数、>k数的链表 20 int p = head, p1 = head1, p2 = head2, p3 = head3; 21 while (p != -1) 22 { 23 if (List[p].val < 0) 24 { 25 List[p1].next = p; 26 p1 = p; 27 } 28 else if (List[p].val > k) 29 { 30 List[p3].next = p; 31 p3 = p; 32 } 33 else 34 { 35 List[p2].next = p; 36 p2 = p; 37 } 38 p = List[p].next; 39 } 40 //这里的顺序千万不要反了,因为next不是地址,要先改变,再赋值 41 List[p3].next = -1; 42 List[p2].next = List[head3].next; 43 List[p1].next = List[head2].next; 44 p = List[head1].next; 45 while (List[p].next != -1) 46 { 47 printf("%05d %d %05d\n", p, List[p].val, List[p].next); 48 p = List[p].next; 49 } 50 printf("%05d %d %d\n", p, List[p].val, List[p].next); 51 return 0; 52 }
原文地址:https://www.cnblogs.com/zzw1024/p/11488848.html
- SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)
- Java操作MongoDB
- 初识MongoDB分片
- Linux上安装Redis
- SpringBoot+WebSocket实现在线聊天(二)
- SpringBoot中使用Freemarker构建邮件模板
- SpringBoot中发送QQ邮件
- SpringBoot中使用POI,快速实现Excel导入导出
- 代理技术 | 重磅,代理服务器背后的故事(正向、反向代理)
- SpringBoot中自定义参数绑定
- ElementUI中tree控件踩坑记
- 一个隐马尔科夫模型的应用实例:中文分词
- 使用MyBatis轻松实现递归查询与存储过程调用
- Config Server——使用Spring Cloud Bus自动刷新配置
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- [Oracle 11g RAC安装]Oracle安装
- [Oracle 11g RAC安装]UDEV设置
- Oracle参数(Undo_Retention)
- Oracle基本概念(Undo空间)
- [AWR报告]Buffer Hit %
- [AWR报告]Library Hit %
- [Oracle 9i安装]Redhat 4.8的配置
- [Oracle 9i安装]Oracle软件的安装
- [AWR报告]Latch Hit %
- [Python运维]Python3.6的安装
- [Python运维]cx_Oracle模块的安装
- C#中抽象类与抽象方法的作用与实例
- C++ 基础扫盲(1)
- [Python运维]使用cx_Oracle连接Oracle(高级篇)
- [Python运维]使用Python发送邮件