00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
Sample output:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
The use of the static linked list utilizes the non-deleted existence of the container. The standard of whether to delete the existence of delete in save is that the absolute key value cannot be repeated. Then open a bool vis[N] = {false};
similar hash table and make a mapping as a mark.
Also, pay attention to the output.
First, the format is output as %05d except for the end of the chain next is -1.
Second, the next value does not need to be changed on the original node to directly output the next address.
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
const int N = 100010;
struct node
{
int address;
int value;
int next;
}link[N];
bool vis[N] = {
false};
vector<int> delet,save;
int main()
{
int cont = 0;
int begin, n;
cin >> begin >> n;
for(int i = 0; i < n; i++)
{
int add,va,next;
scanf("%d %d %d",&add,&va,&next);
link[add].address = add;
link[add].value = va;
link[add].next = next;
}
int p = begin;
while(p != -1)
{
int index = abs(link[p].value);
if(vis[index] == false)
{
save.push_back(p);
p = link[p].next;
vis[index] = true;
}
else
{
delet.push_back(p);
p = link[p].next;
}
}
for(int i = 0; i < save.size(); i++)
{
int p = save[i];
if(i < save.size()- 1)
{
printf("%05d %d %05d\n",link[p].address,link[p].value,link[save[i+1]].address);
}
else printf("%05d %d -1\n",link[p].address,link[p].value);
}
for(int i = 0; i < delet.size(); i++)
{
int p = delet[i];
if(i < delet.size() - 1) printf("%05d %d %05d\n",link[p].address,link[p].value,link[delet[i+1]].address);
else printf("%05d %d -1\n",link[p].address,link[p].value);
}
return 0;
}