1097 Deduplication on a Linked List

cpp 复制代码
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 100010;
struct Node{
    int address, data, next, flag;
    Node(){
        flag = 0;
    }
}nodes[maxn];
int ma[10001];
int del(int pre, int cur){
    int next = nodes[cur].next;
    nodes[pre].next = next;
    return next;
}
int main() {
    int head,n;
    scanf("%d%d", &head, &n);
    int add;
    for(int i = 0; i < n; i++){
        scanf("%d", &add);
        scanf("%d%d", &nodes[add].data, &nodes[add].next);
        nodes[add].address = add;
    }
    ma[abs(nodes[head].data)] = 1;
    int pre=head,cur=nodes[head].next,d;
    vector<int> delNodes;
    while(cur != -1){
        int d = abs(nodes[cur].data);
        if(ma[d] == 1){
            delNodes.push_back(cur);
            cur = del(pre,cur);
        }else{
            ma[abs(nodes[cur].data)] = 1;
            pre = cur;
            cur = nodes[cur].next;
        }
    }
    int p;
    for(p = head; p != -1; p = nodes[p].next){
        if(nodes[p].next != -1){
            printf("%05d %d %05d\n", nodes[p].address, nodes[p].data, nodes[p].next);
        }else{
            printf("%05d %d -1\n", nodes[p].address, nodes[p].data);
        }
    }
    for(int i = 0; i < delNodes.size(); i++){
        if(i < delNodes.size()-1){
            printf("%05d %d %05d\n", nodes[delNodes[i]].address, nodes[delNodes[i]].data, nodes[delNodes[i+1]].address);
        }else{
            printf("%05d %d -1\n", nodes[delNodes[i]].address, nodes[delNodes[i]].data);
        }
    }
    return 0;
}
相关推荐
民乐团扒谱机9 小时前
【超详细】基频音高提取算法之SWIPE 算法深度解析:频谱匹配的精度极限与工程边界
人工智能·算法
饮水桶_T9 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法
星恒随风9 小时前
从0实现C++ AI大模型接入SDK(四):大模型API接入、公共数据结构设计与日志库封装
linux·服务器·数据结构·c++·人工智能
wy_hhxx10 小时前
[Leetcode] 滑动窗口
算法·leetcode·滑动窗口
码途行者10 小时前
每天10分钟学Go:第20课 泛型
c++·算法·golang
AIgorithmGEEK12 小时前
算法·哈希表|从“逐个比”到“按 key 算位置”
数据结构·c++·算法·leetcode·hash
饮水桶_T12 小时前
HashMap笔记
开发语言·数据结构·笔记
风落青芜14 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z15 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode