我来说一下思路,首先要创建好结构体,特别注意输入时的顺序,我定义了个字段,表示输入顺序。然后,判断输入的是否在链表中,然后对链表分类,然后输出。
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <math.h>
#include<algorithm>
using namespace std;
const int maxa=100010;
struct Note{
int data; //数据
int address; //地址
int next; //下一个地址
int flag; //是否在链表上
int range; //范围分类
int fx; //输入顺序
Note(){
flag=0;
}
}notes[maxa];
bool cmp(Note n1,Note n2){
if(n1.flag<1||n2.flag<1){
return n1.flag>n2.flag;
}else{
if(n1.range!=n2.range)return n1.range<n2.range;
else return n1.fx<n2.fx;
}
}
int main(){
int action ,n,k;
cin>>action>>n>>k;
while(n--){
int address,date,next;
cin>>address>>date>>next;
notes[address].address=address;
notes[address].data=date;
notes[address].next=next;
}
int p=action;
int cont=0;
while(p!=-1){
notes[p].flag=1;
if(notes[p].data<0) notes[p].range=-1;
else if(notes[p].data>k) notes[p].range=1;
else notes[p].range=0;
notes[p].fx=cont++;
p=notes[p].next;
}
if(cont==0) return 0;
sort(notes,notes+maxa,cmp);
for(int i=0;i<cont-1;i++){
printf("%05d %d %05d\n",notes[i].address,notes[i].data,notes[i+1].address);
}
printf("%05d %d -1\n",notes[cont-1].address,notes[cont-1].data);
return 0;
}