结构体模拟链表,比较基础

题目链接 L2-002 链表去重 (25 分)

#include <bits/stdc++.h>
#define mmax 100000
using namespace std;
//值 是否出现过 注意一点是(绝对值)!!! 看清题意
int first[mmax];

struct Node{
	//对应 地址 值 下一个结点
	int key,value,next;
	
	Node(){
	}
	
	Node(int k, int v, int n){
		this->key = k;
		this->value = v;
		this->next = n;
	};
}a[mmax],b[mmax],c[mmax];

void print(Node list[], int count,int tmp = 0) {
	for(int i = 0; i < count; i++) {
		if(i == count - 1){
			printf("%05d %d -1\n", list[i].key, list[i].value);
			break;
		}else
			//这边三元运算符注意一下 是一个正序一个倒序
			printf("%05d %d %05d\n", list[i].key, list[i].value, tmp ? list[i+1].key : list[i].next);
	}
}

int main() {
	int start,num,k,v,n,tmp;
	int cont = 0,count = 0;
	
	scanf("%d %d", &start, &num);
	
	for(int i = 0; i < num; i++) {
		scanf("%d %d %d", &k, &v, &n);
		a[k] = Node(k, v, n);
	}
	
	for(Node i = a[start]; ;i = a[i.next]) {
		tmp = abs(i.value);
		if(!first[tmp]++) {
			b[count++] = i;
		}else{
			c[cont++] = i;
			b[count - 1].next = i.next;
		}
		if(i.next == -1)
			break;
	}
	
	print(b,count);
	print(c,cont,1);
	return 0;
}