测试点 2 4 5错误,请大神扫下代码是哪里问题?
网上大部分定义了多个数组,但是我用一个数组实现就是找不到错误在哪?
#include <iostream>
#include <string>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAX_KEY 100500 // 题目已知最大不超过10e5
typedef struct node
{
int key_value;
int next;
}LNode;
int main()
{
int start, N;
//cin >> start >> N; // cin cout处理大数据时耗时较多
scanf("%d %d", &start, &N);
LNode input[MAX_KEY];
for (int i = 0; i < N; ++i)
{
int pos, key, next;
//cin >> pos >> key >> next;
scanf("%d %d %d", &pos, &key, &next);
input[pos].key_value = key;
input[pos].next = next;
}
bool IsExist[MAX_KEY] = {false};
int prepos = start;
int workpos = input[prepos].next;
int nodekey = abs(input[prepos].key_value);
IsExist[nodekey] = true; // 第一个结点肯定不会重复,只需判断后续结点
bool delfirst(true);
int delstart(-1), delwork;
// 用一个数组实现分开两个链表,各个结点不需变动,只需更改链接
while(workpos != -1)
{
nodekey = abs(input[workpos].key_value);
if (IsExist[nodekey] == true) // 出现重复
{
input[prepos].next = input[workpos].next; // 从原始链表取下重复结点
if (delfirst) // 构造重复结点第一个结点
{
delstart = workpos;
input[delstart].next = -1;
delwork = delstart;
delfirst = false;
} else // 将重复结点挂接到重复链表
{
input[workpos].next = -1; // 接到表尾
input[delwork].next = workpos;
delwork = workpos;
}
workpos = input[prepos].next; // prepos不动,检测新的结点
} else
{
IsExist[nodekey] = true; // 标记已出现过
prepos = workpos;
workpos = input[prepos].next;
}
}
// 输出删除后的链表
workpos = start;
while (workpos != -1)
{
//cout << setw(5) << setfill('0') << workpos;
printf("%05d ", workpos);
//cout << " " << input[workpos].key_value << " " << input[workpos].next << endl;
printf("%d %d\n", input[workpos].key_value, input[workpos].next);
workpos = input[workpos].next;
}
// 输出删除的结点链表
delwork = delstart;
while(delwork != -1)
{
//cout << setw(5) << setfill('0') << delwork;
printf("%05d ", delwork);
//cout << " " << input[delwork].key_value << " " << input[delwork].next << endl;
printf("%d %d\n", input[delwork].key_value, input[delwork].next);
delwork = input[delwork].next;
}
return 0;
}