题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052
题目描述:
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 aNext 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 (< 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:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], 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 12345Sample Output:
5 12345 12345 -1 00001 00001 0 11111 11111 100 22222 22222 1000 33333 33333 100000 -1
注意点:
(1)题目中可能存在多条链表。我们只要其中一条。
(2)如果给出的起始节点的地址为-1,则输出“0 -1”。
(3)如果根据给出的起始节点的地址无法找到一条链表,则输出“0 起始节点地址”。
参考代码:
// 1052.cpp : 定义控制台应用程序的入口点。 #include<iostream> #include<functional> #include<algorithm> #include<cstdio> #include<vector> using namespace std; typedef struct node { int address; int key; int next; bool exit; node(){exit = false;} bool operator < (const node &N)const { return key < N.key; } }node; #define max 1000000 int main() { int N;//the total number of nodes in memory int address; int i; node temp; scanf("%d%d",&N,&address); vector<node> Node(max); for(i=0; i<N; i++) { scanf("%d%d%d",&temp.address,&temp.key,&temp.next); temp.exit = true; Node[temp.address] = temp; } int num = 0; int curAddress = address; //根节点 vector<node> result; while(curAddress > 0 && Node[curAddress].exit) { result.push_back(Node[curAddress]); if(Node[curAddress].next == -1) break; else curAddress = Node[curAddress].next; } if(address == -1) { printf("0 -1\n"); return 0; } if(result.size() == 0) { printf("0 %05d\n",address); return 0; } sort(result.begin(),result.end()); //sort会默认按照我们定义好的比较规则来从小到大排序 for(i=0; i < result.size()-1 ; i++) result[i].next = result[i+1].address; printf("%d %05d\n",result.size(),result[0].address); for(i=0; i<result.size() - 1; i++) printf("%05d %d %05d\n",result[i].address,result[i].key,result[i].next); printf("%05d %d -1\n",result[i].address,result[i].key,result[i].next); return 0; }
另外写了个比较清晰的:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; struct node{ int addr, key, next; }; vector<node> vec(100000); bool cmp(node a, node b){ return a.key< b.key; } int main(int argc, char** argv) { int N, start; scanf("%d%d",&N,&start); int i, a, val, b; int root; for(i=0; i<N; i++){ scanf("%d%d%d",&a,&val,&b); vec[a].addr = a; vec[a].next = b; vec[a].key = val; } int addr = start; vector<node> result; while(addr != -1){ result.push_back(vec[addr]); addr = vec[addr].next; } sort(result.begin(), result.end(), cmp); if(start==-1){ cout<<"0 -1"<<endl; return 0; } cout<<result.size()<<" "; printf("%05d\n",result[0].addr); for(i=0; i<result.size(); i++){ if(i==0){ printf("%05d %d",result[i].addr, result[i].key); }else{ printf(" %05d\n%05d %d", result[i].addr, result[i].addr, result[i].key); } } printf(" -1\n"); return 0; }