1052 Linked List Sorting

1052 Linked List Sorting

题目大意

将读入的结点根据是否有效(是否在链表上)和key值的大小排序后,输出所有有效结点(是否在链表上)的address key 和 当前结点的下一个结点的address。(排序后结点的顺序发生了变化,当前结点的next不再是下一个结点的adress)

核心思路

思路借鉴于柳神,链接如下:

https://blog.csdn.net/liuchuo/article/details/52144537

建立结构体数组,按照首地址开始的顺序(直到-1)遍历一遍链表,将在链表上的结点标记为true,并统计个数cnt(有效结点的个数)。(无效结点不在链表中,flag为false)
然后根据链表结点的flag和key进行排序,有效结点在吴小姐点之前,key更小的在key更大的之前
输出前cnt个结点。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100005;
//结点类型
struct Node{
    int address;
    int key;
    int next;
    bool flag;//表示该节点是否是有效结点(在链表上为有效,flag为true;不在链表上为无效,flag为false)
}node[maxn];
//排序函数
bool cmp(Node a,Node b){
    if(a.flag==false||b.flag==false){
        return a.flag>b.flag;//有效的结点放在前面,无效结点放在后面
    }
    else{
        return a.key<b.key;//key值更小的在前面,key值大的在后面
    }
}

int main(){
    //读入结点数量,初始结点的address
    int n,s;
    scanf("%d%d",&n,&s);
    //给每个结点赋值
    for(int i=0;i<n;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        node[a]={a,b,c,false};//flag全部初始化为false,表示所有结点均为无效结点
    }
    //遍历链表的每一个结点,在链表上的结点都是有效结点,同时记录有效结点的个数
    int cnt=0;//有效结点的个数
    for(int p=s;p!=-1;p=node[p].next){
        node[p].flag=true;
        cnt++;
    }
    //根据结点的flag和key进行排序,按结点编号(0~cnt-1~n-1)输出前cnt个结点的address和key和next(
    //根据flag和key排序后,结点的next是会变化的,变成下一个结点的address)
     if(cnt == 0) {
        printf("0 -1");
    } else {
        sort(node, node + maxn, cmp);//对结点进行排序
        printf("%d %05d\n", cnt, node[0].address);//输出结点个数和第一个结点的address
        for(int i = 0; i < cnt; i++) {
            printf("%05d %d ", node[i].address, node[i].key);//输出第i个结点的address和key
            if(i != cnt - 1)//i不是最后一个结点,该结点的next为下一个结点的address
                printf("%05d\n", node[i+1].address);
            else//i是最后一个结点,该结点的下一个结点为-1
                printf("-1\n");
        }
    }
    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、付费专栏及课程。

余额充值