博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1052 Linked List Sorting (25 分)
阅读量:4589 次
发布时间:2019-06-09

本文共 2133 字,大约阅读时间需要 7 分钟。

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 12345

Sample Output:

5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1 思路:   不是所有结点都在链表上的,所以需要遍历再排序。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;struct Node{ long long int key; string address; string next;};bool cmp(Node&A,Node&B){ return A.key
>n>>head; char tep[10]; scanf("%d%s",&n,tep); head=string(tep); unordered_map
mp; Node temp; for(int i=0;i
node; temp=mp[head]; node.push_back(temp); while(temp.next!="-1") { temp=mp[temp.next]; node.push_back(temp); } sort(node.begin(),node.end(),cmp); head=node[0].address; printf("%d %s\n",node.size(),head.c_str()); for(int i=0;i

 

 

转载于:https://www.cnblogs.com/zhanghaijie/p/10327575.html

你可能感兴趣的文章
c# 处理串行化对象的版本变化
查看>>
c# try 和 catch 块
查看>>
c# 简单工厂模式
查看>>
c# 串行化事件
查看>>
c# System.Array
查看>>
c# 反射
查看>>
c# ArrayList 类
查看>>
c# 抽象工厂设计模式
查看>>
c# Stack 类
查看>>
c# System.Collections接口图
查看>>
c# HashTable 类
查看>>
c# Queue 类
查看>>
c# IComparable与IComparer接口
查看>>
c# IEnumerable集合
查看>>
c# 泛型集合
查看>>
c# 类型安全
查看>>
c# 使用泛型集合List<T>
查看>>
c# Dictionary<K,V>
查看>>
c# Unicode
查看>>
c# String类
查看>>