[PAT] Class 1052 Linked List Sorting (25 points)

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 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


analysis

The sorted linked list node key value.

  • Structure for the storage node, flag whether the node records in the list;

    Sort by sort method, cmp method: two nodes visited the small to large order, otherwise the node is not visited put back.

  • N is the total number of input nodes in memory, not necessarily all nodes in a linked list! ! N is the total number of output nodes in the linked list.


  • Note: address node after ordering is changed.
  • Note: When the number of nodes in the list is 0, output 0 -1, and other times different.

  • Note: output in the last node address and other addresses under different output formats


Code

#include<iostream>
#include<algorithm>

using namespace std;

struct NODE {
    int addr,data,next;
    bool flag;//记录该节点是否在链表中
} node[100000];
int cmp1(NODE a,NODE b) {
    return a.flag&&b.flag?a.data<b.data:a.flag>b.flag;
}

int main() {
    int n,start;//n为总结点个数
    int count=0;//链表节点个数
    scanf("%d %d",&n,&start);

    //存放数据
    int addr,data,next;
    for(int i=0; i<n; i++) {
        scanf("%d %d %d",&addr,&data,&next);
        node[addr]= {addr,data,next,false};
    }
    //遍历链表,设置flag,计算count
    for(int i=start; i!=-1; i=node[i].next) {
        node[i].flag=true;
        count++;
    }

    if(count==0) {
        printf("0 -1");
    } else {
        //排序
        sort(node,node+100000,cmp1);

        //输出
        printf("%d %05d\n",count,node[0].addr);
        for(int i=0; i<count; i++) {
            printf("%05d %d ",node[i].addr,node[i].data);
            if(i==count-1)
                printf("-1\n");
            else
                printf("%05d\n",node[i+1].addr);
        }
    }
    return 0;
}


problem

  • count variable at the beginning of the definition and the definition of the nearest answer is not the same; int The default value is 0, but I printed a bit count output is 1, may be the residual stack data.

    Solution: After full at the beginning of the definition of variables, and their own initial value.

Guess you like

Origin www.cnblogs.com/musecho/p/12234533.html