题目:
https://www.nowcoder.com/pat/5/problem/4091
https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464
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 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Node
{
int data;
int next;
}node[100010];
bool cmp(int a,int b)
{
return node[a].data<node[b].data;
}
int main()
{
int n,begin;
scanf("%d%d",&n,&begin);
int address;
for(int i=0;i<n;i++)
{
scanf("%d",&address);
scanf("%d%d",&node[address].data,&node[address].next);
}
vector<int> v;
int count=0;
for(int i=begin;i!=-1;i=node[i].next)
{
v.push_back(i);
count++;
}
if(count==0) printf("0 -1\n");
else
{
sort(v.begin(),v.end(),cmp);
printf("%d %05d\n",count,v[0]);
for(int i=0;i<count;i++)
{
printf("%05d %d ",v[i],node[v[i]].data); //注意是node[v[i]].data
if(i!=count-1) printf("%05d\n",v[i+1]);
else printf("-1");
}
}
return 0;
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100005;
struct Node
{
int address,data,next;
bool flag;
}node[maxn];
bool cmp(Node a,Node b)
{
if(a.flag==false||b.flag==false) return a.flag>b.flag;
else return a.data<b.data;
}
int main()
{
for(int i=0;i<maxn;i++)
{
node[i].flag=false;
}
int n,begin;
scanf("%d%d",&n,&begin);
int address;
for(int i=0;i<n;i++)
{
scanf("%d",&address);
scanf("%d%d",&node[address].data,&node[address].next);
node[address].address=address;
}
//node[address].next=-1;
int p=begin;
int count=0;
while(p!=-1)
{
node[p].flag=true;
p=node[p].next;
count++;
}
if(count==0) printf("0 -1\n");
else
{
sort(node,node+maxn,cmp);
printf("%d %05d\n",count,node[0].address);
for(int i=0;i<count;i++)
{
if(i!=count-1) printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
else printf("%05d %d -1\n",node[i].address,node[i].data);
}
}
return 0;
}

本文介绍了一种基于链表结构的排序算法实现,通过读取链表节点,将节点地址存储到向量中,然后根据节点数据进行排序。排序完成后,重新输出链表,确保节点按升序排列。此算法适用于内存中链表节点的排序,保证了排序后的链表结构完整。
617

被折叠的 条评论
为什么被折叠?



