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 () 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 [], 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

解题思路

  • 静态链表->结构体数组->清除无效节点顺便数组化->对数组进行排序

  • 此时关键字key是有序的

  • 但需要更新一下next关系

  • 输出结构体数组

  • 结构体数组化的方式:vector<node> Array

注意

  • 如果不进行数组化,那么结构体的是不能进行移动位置的,也就不能进行排序了

  • 需要对存储静态链表的结构体数组进行清除无效节点,并重新计算节点数量newN

  • 边界条件:节点全部无效的时候,需要输出0 -1

静态链表存储结构

注意包含自己的地址,这样很方便更新next

结构体定义

struct node{
    int addr;//存储自己地址,会很方便的更新next
    int key;
    int next;
}NodeList[100005] ;

更新next

//  更新next关系(当然了,不更新也行,直接输出这种)
    for(int i=0;i<newN-1;i++)
    {
        Array[i].next = Array[i+1].addr;//存储自己地址,会很方便的更新next
    }

收获与思考

我们来理一下对于静态链表题目的思路

  1. 用结构体存储,结构体定义要完整,见上面

  1. 进行清洗,删除无效节点,与此同时数组化,数组化的方式见上面

  1. 如果需要更改顺序,别忘记更新next

代码

#include <iostream>
#include <vector>
#include<algorithm>
#include <iterator>
using namespace std;

struct node{
    int addr;//存储自己地址,会很方便的更新next
    int key;
    int next;
}NodeList[100005] ;
bool cmp(node a,node b)
{
    return a.key<b.key;
}
vector<node> Array;
int main()
{
    int N,head;
    scanf("%d %d",&N,&head);
    int addr,key,next;
// 注意有一个问题,有没有可能有无效节点,这是需要去除的!
    for(int i=0;i<N;i++)
    {
        scanf("%d %d %d",&addr,&key,&next);
        NodeList[addr].addr = addr;
        NodeList[addr].key = key;
        NodeList[addr].next = next;
    }
//  对无效节点进行清除并数组化
    int newN = 0;
    while(head!=-1)
    {
         Array.push_back(NodeList[head]);
         head = NodeList[head].next;
         newN++;
    }
//  对结构体进行排序
    sort(Array.begin(),Array.end(),cmp);
//  如果全都无效(这一点很边界),那么只能输出 0 -1
    if(newN==0)
    {
        printf( "0 -1");
        return 0;
    }

//  更新next关系(当然了,不更新也行,直接输出这种)
    for(int i=0;i<newN-1;i++)
    {
        Array[i].next = Array[i+1].addr;//存储自己地址,会很方便的更新next
    }
    Array[newN-1].next = -1;
    printf("%d %05d\n",newN,Array[0].addr);
    for(int i=0;i<newN-1;i++)
    {
        printf("%05d %d %05d\n",Array[i].addr,Array[i].key,Array[i].next);
    }
    printf("%05d %d %d\n",Array[newN-1].addr,Array[newN-1].key,Array[newN-1].next);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45621688/article/details/129509144