且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

1052. Linked List Sorting (25) 22'

更新时间:2022-08-16 11:11:20

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
 题目大意:对结构体进行排序
 分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表,
 */
struct node{
    int adress, data, next;
} nodes[100001];

bool cmp(node a, node b){
    return a.data < b.data;
}

int main(){
    int n, s;
    cin >> n >> s;
    vector<node> v;
    for (int i = 0; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        nodes[a] = {a, b, c};
    }
    int head = s;
    while (head != -1) {
        v.push_back(nodes[head]);
        head = nodes[head].next;
    }

    sort(v.begin(), v.end(), cmp);

    if (v.size() == 0) {
        cout << "0 -1\n";
    }else{
        cout << v.size() << ' ' << v[0].adress << endl;
        for (int i = 0; i < v.size(); i++) {
            if(i == 0) printf("%05d %d ", v[i].adress, v[i].data);
            else printf("%05d\n%05d %d ", v[i].adress, v[i].adress, v[i].data);
        }
        cout << "-1\n";
    }

    return 0;
}