| A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer Input Specification:Each input file contains one test case. For each case, the first line contains a positive N (<105) 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: where 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. |
- 5 00001
- 11111 100 -1
- 00001 0 22222
- 33333 100000 11111
- 12345 -1 33333
- 22222 1000 12345
- 5 12345
- 12345 -1 00001
- 00001 0 11111
- 11111 100 22222
- 22222 1000 33333
- 33333 100000 -1
题目大意
给你一个链表,每个data都有对应的adress,请你按data从小到大排序后依次输出
思路
pair
adress + data,sort直接根据data排序即可
- #include
- using namespace std;
- bool cmp(pair<int,int> x, pair<int,int> y){
- return x.second < y.second;
- }
- int main()
- {
- int N, head, num,data[100001],next[100001];
- cin >> N >> head;
- while (N--) {
- cin >> num;
- cin >> data[num] >> next[num];
- }
- vector
int,int>> result; - while (head!=-1){
- result.emplace_back(head,data[head]);
- head = next[head];
- }
-
- sort(result.begin(),result.end(),cmp);
- cout << result.size() << " ";
- for(auto x:result) printf("%05d\n%05d %d ",x.first,x.first,x.second);
- cout << "-1";
- return 0;
- }
