PAT (Basic Level) Practice (中文) 1075 链表元素分类 (25 分)

我来说一下思路,首先要创建好结构体,特别注意输入时的顺序,我定义了个字段,表示输入顺序。然后,判断输入的是否在链表中,然后对链表分类,然后输出。

 

 

 

#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;
}

 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值