PAT-B1075 链表元素分类(25 分)

1075 链表元素分类(25 分)

给定一个单链表,请编写程序将链表元素进行分类排列,使得所有负值元素都排在非负值元素的前面,而 [0, K] 区间内的元素都排在大于 K 的元素前面。但每一类内部元素的顺序是不能改变的。例如:给定链表为 18→7→-4→0→5→-6→10→11→-2,K 为 10,则输出应该为 -4→-6→-2→7→0→5→10→18→11。

输入格式:

每个输入包含一个测试用例。每个测试用例第 1 行给出:第 1 个结点的地址;结点总个数,即正整数N (≤10​5​​);以及正整数K (≤10​3​​)。结点的地址是 5 位非负整数,NULL 地址用 −1 表示。

接下来有 N 行,每行格式为:

Address Data Next

其中 Address 是结点地址;Data 是该结点保存的数据,为 [−10​5​​,10​5​​] 区间内的整数;Next 是下一结点的地址。题目保证给出的链表不为空。

输出格式:

对每个测试用例,按链表从头到尾的顺序输出重排后的结果链表,其上每个结点占一行,格式与输入相同。

输入样例:

00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218

输出样例:

33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1

分析:

所给的结点有可能并不存在于链表中,所以最终链表的长度可能小于所给元素的数量。

代码:

#include <iostream>
#include <vector>

using namespace std;

typedef struct node
{
    int address, data, next;
    node(int a = 0, int d = 0, int n = 0):address(a), data(d), next(n){}
}node;
int main()
{
    int head, len, K, i;
    node lt[100050];
    vector<node> negative, z2k, btk;//zero to k, bigger than k
    scanf("%d%d%d", &head, &len, &K);
    for(i = 0; i < len; i++)
    {
        int a, d, n;
        scanf("%d%d%d", &a, &d, &n);
        lt[a] = node(a, d, n);
    }
    while(head != -1)
    {
        if(lt[head].data < 0)
            negative.emplace_back(lt[head]);
        else if(lt[head].data >= 0 && lt[head].data <= K)
            z2k.emplace_back(lt[head]);
        else
            btk.emplace_back(lt[head]);
        head = lt[head].next;
    }
    negative.insert(negative.end(), z2k.begin(), z2k.end());
    negative.insert(negative.end(), btk.begin(), btk.end());
    for(i = 0; i < negative.size(); i++)
    {
        printf("%05d %d", negative[i].address, negative[i].data);
        if(i != negative.size() - 1)
            printf(" %05d", negative[i + 1].address);
        else
            printf(" -1");
        printf("\n");

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38127801/article/details/81151245
今日推荐