• 数据结构专题——链表处理A1097.Deduplication on a Linked List(25) (静态链表)


    #include <bits/stdc++.h>
    #include<math.h>
    #include <string>
    using namespace std;
    const int maxn = 100005;
    const int TABLE = 1000010;
    struct Node{//定义静态链表
        int data;
        int address;
        int next;
        int order;
    }node[maxn];
    bool isExist[TABLE] = {false};
    bool cmp(Node a,Node b){
        return a.order < b.order;
    }
    int main(){
        memset(isExist,false,sizeof(isExist));
        for(int i=0;i<maxn;++i){//初始化
            node[i].order = 2 * maxn;//表示初始时均为无效节点
        }
        int n,begin,address;
        scanf("%d%d",&begin,&n);
        for(int i=0;i<n;++i){
            scanf("%d",&address);
            scanf("%d%d",&node[address].data,&node[address].next);
            node[address].address = address;
        }
        //未删除的有效节点个数和已删除的有效节点个数
        int countValid = 0, countRemoved = 0,p = begin;
        while(p != -1){
            if(!isExist[abs(node[p].data)]){
                isExist[abs(node[p].data)] = true;
                node[p].order = countValid++;
            }else{
                node[p].order = maxn+ countRemoved++;//被删除,编号从maxn开始
            }
            p = node[p].next;
        }
        sort(node,node+maxn,cmp);
        //输出结果
        int count = countValid + countRemoved;
        for(int i= 0;i<count;++i){
            if(i != countValid -1 && i != count -1){//非最后一个节点
                printf("%05d %d %05d
    ",node[i].address,node[i].data,node[i+1].address);
            }else{
                printf("%05d %d -1
    ",node[i].address,node[i].data);
            }
        }
        system("pause");
        return 0;
    } 
  • 相关阅读:
    PS选区认识
    移动工具
    PS认识及新建文件
    第02组 Alpha冲刺(3/4)
    第02组 Alpha冲刺(2/4)
    第02组 Alpha冲刺(1/4)
    第02组 团队Git现场编程实战
    第二次结对编程作业
    团队项目-需求分析报告
    团队项目-需求分析报告
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12214696.html
Copyright © 2020-2023  润新知