1052 Linked List Sorting (25 分)

链表的重组排序问题,其中在测试点三和测试点四卡的时间较久
坑点如下
1,读入链表时注意将无效节点排除,本题我用的是order排序的方法,另有flag排序的方法,注意灵活运用
2,开始显示测试点三答案错误,后来经排查发现是由于第一个地址没有%05d输出造成的,修改后成功通过
3,测试点四考察有效节点数为0的情况,此时需单独判定

完整代码如下

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100500;
struct Node{
    int data;
    int address;
    int next;
    int order;
}node[maxn];

bool cmp(Node a,Node b){
  return a.order<b.order;
    
}
bool cmp1(Node a,Node b){
    return a.data<b.data;
}
int main(){
    int n,first;
    scanf("%d%d",&n,&first);
    for(int i=0;i<maxn;i++)
    {
        node[i].order=maxn;
    
    }
    int ad;
    for(int i=0;i<n;i++)
    {
    
        scanf("%d",&ad);
        scanf("%d%d",&node[ad].data,&node[ad].next);
        node[ad].address=ad;
    }
    int p=first,count=0;
    while(p!=-1){
        node[p].order=count++;
        p=node[p].next;  
      
    }
    if(count==0) {printf("0 -1");}
    
    else
        
  {   sort(node,node+maxn,cmp);
      sort(node,node+count,cmp1);
      printf("%d %05d\n",count,node[0].address);
    for(int i=0;i<count;i++)
    { if(i!=count-1)
      {  printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);}
       
        else {printf("%05d %d -1\n",node[i].address,node[i].data);}
    }
        
    }
    return 0;
    
}

另一种解法

#include<bits/stdc++.h>
using namespace std;
struct Node{
int data,ad,next,order;
    }node[100100];
bool cmp(Node a,Node b){
    return a.order!=b.order?a.order>b.order:a.data<b.data;
}
int main(){
   int n,f1,ad,data,ne;
   cin>>n>>f1;
   for(int i=0;i<n;i++){
    scanf("%d",&ad);
    node[ad].ad=ad;
    scanf("%d%d",&node[ad].data,&node[ad].next);
   }
   int now=f1,cnt=0;
    while(now!=-1){
      node[now].order++;
      now=node[now].next;
      cnt++;
    }
    sort(node,node+100100,cmp);
    
    for(int i=0;i<=cnt-2;i++){
      node[i].next=node[i+1].ad;
    }
    if(cnt==0)printf("0 -1\n");
    else
 {     printf("%d %05d\n",cnt,node[0].ad);
    for(int i=0;i<=cnt-2;i++){
       printf("%05d %d %05d\n",node[i].ad,node[i].data,node[i].next);
    }
    printf("%05d %d -1\n",node[cnt-1].ad,node[cnt-1].data);
    }
}


  • 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、付费专栏及课程。

余额充值