1133 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 (≤10​5​​) which is the total number of nodes, and a positive K (≤10​3​​). The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

  1. Address Data Next

where Address is the position of the node, Data is an integer in [−10​5​​,10​5​​], 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:

  1. 00100 9 10
  2. 23333 10 27777
  3. 00000 0 99999
  4. 00100 18 12309
  5. 68237 -6 23333
  6. 33218 -4 00000
  7. 48652 -2 -1
  8. 99999 5 68237
  9. 27777 11 48652
  10. 12309 7 33218

Sample Output:

  1. 33218 -4 68237
  2. 68237 -6 48652
  3. 48652 -2 12309
  4. 12309 7 00000
  5. 00000 0 99999
  6. 99999 5 23333
  7. 23333 10 00100
  8. 00100 18 27777
  9. 27777 11 -1

题目大意:给出一个链表,和一个数K,将<0的数都按发现的顺序放在开头,<=k的数放在负数后,并且也是按原来的顺序,>k的按顺序在最后。

//很简答的题目,一开始遍历的方式错了,不过改了过来,

AC代码:

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdio>
  4. using namespace std;
  5. struct Node{
  6. int data,next;
  7. }vn[];
  8.  
  9. int main() {
  10. int from,n,k;
  11. cin>>from>>n>>k;
  12. // vector<Node> vn(n);
  13. int add,da,to;
  14. for(int i=;i<n;i++){
  15. cin>>add>>da>>to;
  16. //vn[add].addr=add;
  17. vn[add].data=da;
  18. vn[add].next=to;
  19. }
  20. vector<int> re;
  21. //本次找出是负数的,并且编号存储。
  22. // for(int i=0;i<n;i++){
  23. // if(vn[i].data<0)
  24. // re.push_back(i);
  25. // }
  26. // for(int i=0;i<n;i++){
  27. // if(vn[i].data>=0&&vn[i].data<=k)
  28. // re.push_back(i);
  29. // }
  30. // for(int i=0;i<n;i++){
  31. // if(vn[i].data>k)
  32. // re.push_back(i);
  33. // }这样去遍历链表是不对的,并不能分出来次序啊。
  34. for(int i=from;i!=-;i=vn[i].next){
  35. if(vn[i].data<)
  36. re.push_back(i);
  37. }
  38. for(int i=from;i!=-;i=vn[i].next){
  39. if(vn[i].data>=&&vn[i].data<=k){
  40. re.push_back(i);
  41. }
  42. }
  43. for(int i=from;i!=-;i=vn[i].next){
  44. if(vn[i].data>k)
  45. re.push_back(i);
  46. }
  47. for(int i=;i<re.size();i++){
  48. if(i==re.size()-){
  49. printf("%05d %d -1",re[i],vn[re[i]].data);
  50. }//cout<<re[i]<<" "<<vn[re[i]].data<<"-1";
  51. else{
  52. printf("%05d %d %05d\n",re[i],vn[re[i]].data,re[i+]);
  53. }//cout<<re[i]<<" "<<vn[re[i]].data<<" "<<re[i+1]<<'\n';
  54. }
  55.  
  56. return ;
  57. }

1.链表遍历的主要就是使用数组下标表示链表地址,根据这个对链表进行遍历,其他的问题就不大了。

2。从前往后遍历链表3次,按顺序放入地址,之后再按顺序输出即可!

PAT 1133 Splitting A Linked List[链表][简单]的更多相关文章

  1. PAT 1133 Splitting A Linked List

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  2. PAT A1133 Splitting A Linked List (25 分)——链表

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  3. PAT A1133 Splitting A Linked List (25) [链表]

    题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...

  4. 1133 Splitting A Linked List

    题意:把链表按规则调整,使小于0的先输出,然后输出键值在[0,k]的,最后输出键值大于k的. 思路:利用vector<Node> v,v1,v2,v3.遍历链表,把小于0的push到v1中 ...

  5. PAT1133:Splitting A Linked List

    1133. Splitting A Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PAT_A1133#Splitting A Linked List

    Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...

  7. PAT-1133(Splitting A Linked List)vector的应用+链表+思维

    Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...

  8. PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)

    1074 Reversing Linked List (25 分)   Given a constant K and a singly linked list L, you are supposed ...

  9. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

随机推荐

  1. PHP多进程(4) :内部多进程

    说的都是只兼容unix 服务器的多进程,下面来讲讲在window 和 unix 都兼容的多进程(这里是泛指,下面的curl实际上是通过IO复用实现的). 通过扩展实现多线程的典型例子是CURL,CUR ...

  2. Toad 补充与培训 & 常用菜单

    Toad 常用菜单 新版本 toad 软件中, 比较有用的菜单 (toad10.6 版本) 下边菜单, 在日常工作中出现过的, 显示为 粉色 , 蓝色 表示次一级的重要 session 菜单 new ...

  3. Reveal Jquery 模式对话框插件

    /* * jQuery Reveal Plugin 1.0 * www.ZURB.com * Copyright 2010, ZURB * Free to use under the MIT lice ...

  4. Discretized Streams, 离散化的流数据处理

    Discretized Streams: An Efficient and Fault-Tolerant Model for Stream Processing on Large Clusters   ...

  5. 006杰信—factory更新数据

    本博客的资源全部来源于传智播客. factroy更新的执行流程和003杰信-在jsp页面输入数据,然后在oracle数据库中插入factory数据,当字段允许为空时要特殊处理差不多, 1.在jFact ...

  6. php -- ziparchive::open创建zip压缩文件

    语法: mixed ZipArchive::open ( string $filename [, int $flags ] ) 参数: filename:创建的zip的文件名 flags: ZIPAR ...

  7. ssh免密码登录的几个注意事项

    1, authorized_keys文件中每个公钥占一行,不能分成多行. 2,文件夹默认权限为600 3,如果遇到奇怪的问题,可以把.ssh/文件全部删掉,重新用ssh-keygen生成.

  8. gomobile build

    You need to set the NDK path in gomobile init using the -ndk flag - if you follow these instructions ...

  9. 比较难的sql面试题--记录下来晚上做

    一组通话记录(总共500万条):ID 主叫号码 被叫号码 通话起始时间   通话结束时间           通话时长1  98290000 0215466546656 2007-02-01 09:4 ...

  10. 目标检测YOLO算法-学习笔记

    算法发展及对比: 17年底,mask-R CNN YOLO YOLO最大的优势就是快 原论文中流程,可以检测出20类物体. 红色网格-张量,在这样一个1×30的张量中保存的数据 横纵坐标中心点缩放到0 ...