• 1133 Splitting A Linked List


    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 (≤105) which is the total number of nodes, and a positive K (≤103). 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:

    Address Data Next
    

    where Address is the position of the node, Data is an integer in [−105,105], 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
    1. #include
    2. #include
    3. using namespace std;
    4. int n, k, t, addr, num, n1, n2, n3;
    5. int Data[100010], Next[100010], List[100010], N1[100010], N2[100010], N3[100010];
    6. int main() {
    7. cin >> addr >> n >> k;
    8. for (int i = 0; i < n; i++) {
    9. cin >> t;
    10. cin >> Data[t] >> Next[t];
    11. }
    12. t = addr;
    13. while (t != -1) {
    14. List[num++] = t;
    15. t = Next[t];
    16. }
    17. for (int i = 0; i < num; i++) {
    18. if (Data[List[i]] < 0) {
    19. N1[n1++] = List[i];
    20. } else if (Data[List[i]] >= 0 && Data[List[i]] <= k) {
    21. N2[n2++] = List[i];
    22. } else {
    23. N3[n3++] = List[i];
    24. }
    25. }
    26. num = 0;
    27. for (int i = 0; i < n1; i++) {
    28. List[num++] = N1[i];
    29. }
    30. for (int i = 0; i < n2; i++) {
    31. List[num++] = N2[i];
    32. }
    33. for (int i = 0; i < n3; i++) {
    34. List[num++] = N3[i];
    35. }
    36. for (int i = 0; i < num - 1; i++) {
    37. cout << setw(5) << setfill('0') << List[i] << ' ' << Data[List[i]] << ' ' << setw(5) << setfill('0') << List[i + 1] <<
    38. endl;
    39. }
    40. cout << setw(5) << setfill('0') << List[num - 1] << ' ' << Data[List[num - 1]] << ' ' << -1;
    41. return 0;
    42. }
  • 相关阅读:
    JVM优化案例实战-手动模拟Young GC
    2022牛客多校10 I-Yet Another FFT Problem?(鸽巢原理)
    xargs命令
    Redis面临的挑战
    2022 各互联网大厂面经及总结 + 大厂 Java 岗面试真题解析(进大厂必看攻略)
    Flutter最新稳定版3.16 新特性介绍
    postgresql用户与权限管理
    springboot基础(32):整合Mongodb
    阿里云 Serverless 异步任务处理系统在数据分析领域的应用
    接口测试常用工具及测试方法
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127916669