PAT A1052 Linked List Sorting

Description:

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 (< 1 0 5 10^5 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 A d d r e s s \red{Address} Address is the address of the node in memory, K e y \orange{Key} Key is an integer in [ − 1 0 5 -10^5 105​​ , 1 0 5 10^5 105 ], and N e x t \red{Next} 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

注意点:

  1. 起始地址为-1时,直接输出。
  2. 题目所给的结点不一定都是链表上的结点,先由N初始化所给N个结点,flag=0;之后用start遍历所有链表上的结点,并且flag赋值为1,flag的作用用于将有效结点提到数组最前面。
  3. 排序之后,对前count个结点的下地址进行更改,之后输出,注意输出格式,地址非负数输出5位,高位不足补0,地址为-1则直接输出

AC代码:

#include <stdio.h>
#include <stdlib.h>
#define max 100010
typedef struct
{
    int address;
    int key;
    int next;
    int flag;
}Node;

Node table[max];

void init(Node a[],int n)
{
    for(int i=0;i<n;i++)
    {
        a[i].flag=0;
    }
}//初始化flag=0;

int compare(const void*a,const void*b)
{
    Node *pa=(Node *)a;
    Node *pb=(Node *)b;
    if(pa->flag!=pb->flag)return pb->flag>pa->flag?1:-1;//按flag从大到小排序
    else return pa->key>pb->key?1:-1;//从小到大排序
}


int main()
{
    //freopen("input.txt","r",stdin);
    int N,start;
    int i,a,b,c;
    int count=0;//计算链表中元素的个数
    scanf("%d %d",&N,&start);
    if(start==-1)
    {
        printf("0 -1");
        return 0;
    }//特殊情况,直接输出
    for(i=0;i<N;i++)
    {
        scanf("%d %d %d",&a,&b,&c);
        table[a].address=a;
        table[a].key=b;
        table[a].next=c;
    }
    while(start!=-1)
    {
        count++;
        table[start].flag=1;//这里才令flag=1,上面for循环中的结点不一定是链表的结点
        start=table[start].next;
    }//令链表中的结点flag=1
    qsort(table,100010,sizeof(Node),compare);//排序
    printf("%d %05d\n",count,table[0].address);//输出总结点和链表起始地址
    for(i=0;i<count-1;i++)
    {
        table[i].next=table[i+1].address;
    }//更改链表结点的next
    table[count-1].next=-1;
    for(i=0;i<count;i++)
    {
        printf("%05d %d",table[i].address,table[i].key);
        if(table[i].next!=-1)printf(" %05d",table[i].next);
        else printf(" -1");
        if(i<count-1)printf("\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值