当前位置: 代码迷 >> 综合 >> 1052 Linked List Sorting (25分)
  详细解决方案

1052 Linked List Sorting (25分)

热度:73   发布时间:2024-01-25 18:29:04.0

1 题目

1052 Linked List Sorting (25分)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (<10^5) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by ?1.

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

Address Key Next

where Address is the address of the node in memory, Key is an integer in [?10^?5 ,10^?5
?? ], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.

Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.

Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1

2 思路分析

  • 题意:把链表按key值从小到大进行排序形成新的链表,输出新的链表
  • 分析:
    • 定义静态链表
      • 地址、数值、下一个结点的地址、结点性质(是否为有效结点【即,在链表上】,false表示无效)
      • 初始化,把链表中的结点性质初始化为false;
      • 由题目给出头结点,遍历整条链表,并把链表上的结点性质改为false;同时记录有效结点的个数
      • 对结点进行排序
        • 如果两个参数结点,只要有一个flag= false,就按flag从大到小排序,把有效结点排在数组左端【因为有效结点的flag为true,即1,大于false的0】
        • 否则,则按参数结点的key值,从小到大排序
      • 按顺序输出有效结点
    • 注意??:
      • 输出-1时候,单独输出,不能用%05d
      • 数据里均为无效(结点均不在题目给出的首地址的链表上)时,特判输出 0 -1

3 参考代码

#include <cstdio>
#include <algorithm>using std::sort;const int MAXN = 100010;struct Node
{int m_address;int m_data;int m_next;bool m_flag;
}node[MAXN];//1 创建静态链表bool cmp(Node a, Node b){if(a.m_flag == false || b.m_flag == false){return a.m_flag > b.m_flag;}else{return a.m_data < b.m_data;}
}int main(int argc, char const *argv[])
{for (int i = 0; i != MAXN; ++i)//2 初始化链表{node[i].m_flag = false;}int n,head;scanf("%d%d", &n, &head);int address;while(n--){scanf("%d",&address);scanf("%d%d", &node[address].m_data, &node[address].m_next);node[address].m_address = address;}int count = 0;for (int i = head; i != -1; i = node[i].m_next){//3 枚举链表,对flag进行标记node[i].m_flag = true;count++;}if(count == 0){//特判,新链表中没有有效结点printf("0 -1\n");}else{//4 筛选有效结点sort(node, node + MAXN, cmp);//5 输出结果//防止-1 被%5d化printf("%d %05d\n", count, node[0].m_address);for (int i = 0; i != count; ++i){if(i != count - 1){printf("%05d %d %05d\n", node[i].m_address, node[i].m_data, node[i + 1].m_address);}else{printf("%05d %d -1\n",  node[i].m_address, node[i].m_data);}}}return 0;
}
  相关解决方案