1052. Linked List Sorting (25) -- 易犯错
来源:互联网 发布:linux系统打开命令窗口 编辑:程序博客网 时间:2024/05/27 09:45
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 (< 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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1
提交代码
http://www.patest.cn/contests/pat-a-practise/1052
主要存在特殊情况(链表为空的情况),这个需要单独处理下
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <map>#include <stack>using namespace std;#define N 100000struct node{ int addr ; int key ; int next ;};int n , firAddr ;node a[N] ;vector<node> v ;bool cmp(node n1 , node n2){ return n1.key < n2.key ;}int main(){ //freopen("in.txt" , "r" , stdin) ; cin >> n >> firAddr ; int i ; int add,ke,ne; bool flag = false ; for(i = 0 ; i < n ; i++) { cin >> add >> ke >> ne ; a[add].addr = add ; a[add].key = ke ; a[add].next = ne ; if(add == firAddr ) flag = true ; } if(flag == false) { printf("0 -1\n"); return 0 ; } int tmp = firAddr ; int len = 0 ; while(tmp != -1) { len ++ ; v.push_back(a[tmp]) ; tmp = a[tmp].next ; } if(len > 1) sort(v.begin() , v.end() , cmp) ; printf("%d %05d\n" , len , v[0].addr) ; printf("%05d %d" ,v[0].addr,v[0].key) ; for(i = 1 ; i < len ;i++) { printf(" %05d\n%05d %d" ,v[i].addr,v[i].addr,v[i].key) ; } printf(" -1\n") ; return 0;}
- 1052. Linked List Sorting (25) -- 易犯错
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 在app内打开自己app的专用设置界面
- 横表转纵表小例子
- 图解使用telnet程序手工接收邮件及其过程
- Codeforces Round #334 (603A) Alternative Thinking [贪心]
- WebRTC 建立点对点连接(日志分析)
- 1052. Linked List Sorting (25) -- 易犯错
- PHP中数组转JSon和JSon转数组的实现
- iOS 保持界面流畅的技巧优化
- 【php中的curl】使用curl完成POST数据给飞信接口
- 四层负载均衡与七层负载均衡
- android屏幕适配(二)
- App图标适配
- UISwitch控件的常用属性
- App推广中,筛选优质渠道的5个方法