1052 Linked List Sorting (25 分)

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 (<10​5​​) 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 [−10​5​​,10​5​​], 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

tips

提交次数通过样例个数(5)分数tips
1321并不是所有给定节点都在链表中
2424最后一个1分测试用例:段错误
3525增加了对start地址是否存在内存中的判断
  • 像1042题一样,链表的题都是这种题型(包括给定的链表的结构),因此要注意特定的技巧,比如此题就无需真正作为一个链表“排序”,只需要对key值进行排序,则排序后的序列中,每后一个结点的地址就是前一个节点的next值
  • 这道题有几个坑:
  • 第一个坑:第一次提交,过了3/5个测试,却得了21分,以及最长的测试也通过了,因此知道未通过的样例应该是错在细节部分。虽然又读了一遍题目和代码并没有发现什么错误,看他人的博客才发现有一些小坑:并不是所有题目给定的节点都一定会在以给定的开始结点为首元结点的链表中,因此不能把所有的结点直接加入数组或vector中进行sort;而是根据首元结点依次把链表中的结点加入数组或vector后,按照key值由小到大进行排序
  • 第二个坑:即最后一个测试用例:段错误。因此检查数组等的问题,数组没有问题, 发现是链表中有可能一个结点也没有,这种情况应该输出0 -1,当给定的开始结点地址start在所有给定结点中不存在时,链表中可能一个结点也没有。
  • 以上两个坑非常不明显,题目也没有任何提示,因此需要做题时多加注意和总结

code

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100000;

struct node
{
    int address;
    int key;
    int next;
    node(){}
    node(int address, int key, int next):address(address),key(key),next(next){}
}List[maxn];


bool cmp(const node a, const node b)
{
    return a.key < b.key;
}

int main()
{
    int n, start;
    vector<node> v;
    scanf("%d%d",&n,&start);
    bool flag = false; //start地址是否存在
    for(int i = 0; i < n; i++){
        int adr, key, next;
        scanf("%d%d%d",&adr,&key,&next);
        List[adr].address = adr;
        List[adr].key = key;
        List[adr].next = next;
        if(adr == start) flag = true;
        //v.push_back(node(adr,key,next));
    }
    while(start != -1 && flag){
        v.push_back(List[start]);
        start = List[start].next;
    }
    sort(v.begin(), v.end(), cmp);
    if(v.size() != 0) //*
        printf("%d %05d\n",v.size(),v[0].address);
    else printf("0 -1\n");
    for(int i = 0; i < v.size(); i++){
        printf("%05d %d ",v[i].address,v[i].key);
        if(i != v.size() - 1) printf("%05d\n",v[i + 1].address);
        else printf("-1");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
请参考我给出的代码框架,实现对EMPLOYEE结构体为数据的双向链表的排序算法,要求按照按employeeId升序排列 typedef struct linkNode { void* data; //使用空指针使得NODE适配多种数据结构 struct linkNode* preNode; struct linkNode* nextNode; }LINKED_NODE; /*Define the struct of double linked list.*/ typedef struct { LINKED_NODE* head; LINKED_NODE* tail; size_t size; }DOUBLE_LINK_LIST; typedef struct { int employeeId; char name[20]; char ipAddress[30]; char seatNumber[20]; char group[10]; } EMPLOYEE; DOUBLE_LINK_LIST* createDoubleLinkedList() { DOUBLE_LINK_LIST* newList = (DOUBLE_LINK_LIST*)malloc(sizeof(DOUBLE_LINK_LIST)); newList->head = NULL; newList->tail = NULL; newList->size = 0; return newList; } void destroyDoubleLinkedList(DOUBLE_LINK_LIST* list) {} /*Add a new node before the head.*/ void insertHead(DOUBLE_LINK_LIST* list, void* data) // void执政适配其他data类型? {} /*Add a new node after tail.*/ void insertTail(DOUBLE_LINK_LIST* list, void* data) // 如何适配其他data类型? {} /*Insert a new node.*/ void insertNode(DOUBLE_LINK_LIST* list, void* data,int index) // 如何适配其他data类型? {} void deleteHead(DOUBLE_LINK_LIST* list) {} void deleteTail(DOUBLE_LINK_LIST* list) {} void deleteNode(DOUBLE_LINK_LIST* list, int index) {} LINKED_NODE* getNode(DOUBLE_LINK_LIST* list, int index) {} /* 遍历链表,对每个节点执行指定操作*/ void traverseList(DOUBLE_LINK_LIST* list, void (*callback)(void*)) { LINKED_NODE* currentNode = list->head; while (currentNode != NULL) { callback(currentNode->data); currentNode = currentNode->nextNode; } } void printEmployee(void* data) {}
最新发布
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值