PAT:1052. Linked List Sorting (25) 部分错误

本文探讨了排序算法的应用,并通过实例展示了如何使用链表结构实现特定的数据组织和操作。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
  int address;
  int data;
  int next;
  bool tag;
}Node[100066];

bool cmp(node a,node b)
{
  return a.data<b.data;
}

int main()
{
  memset(Node,0,sizeof(Node));
  int n,front;
  scanf("%d%d",&n,&front);
  for(int i=0 ; i<n ; ++i) //记录节点
  {
    int now,data,next;
    scanf("%d%d%d",&now,&data,&next);
    Node[now].address=now;
    Node[now].data=data;
    Node[now].next=next;
  }
  int cnt=0,add; //记录有相关节点个数

  for(add=front ; add!=-1 ; add=Node[add].next) //找到所有属于头结点链表的相关节点
  {
    ++cnt;
    Node[add].tag=1;
  }
  //输出
  if(cnt==0)        //特判没有节点的时候

  {
    printf("0 -1");
    return 0;
  }
  sort(Node,Node+100066,cmp);
  for(int i=0 ; i<100066 ; ++i)
  {
    if(Node[i].tag==1) //按data升序,大的一定在后面,要遍历到100065,因为memset之后所有初始data都是0
    {
      --cnt;
      if(cnt==0)
      {
        printf("%05d %d -1\n",Node[i].address, Node[i].data);
        break;
      }
      else
        printf("%05d %d %05d\n",Node[i].address, Node[i].data,Node[i+1].address);
    }
  }
return 0;
}

转载于:https://www.cnblogs.com/Evence/p/4315466.html

[I 2025-09-07 22:53:35.380 ServerApp] Extension package panel.io.jupyter_server_extension took 1.7906s to import [I 2025-09-07 22:53:35.380 ServerApp] jupyter_lsp | extension was successfully linked. [I 2025-09-07 22:53:35.384 ServerApp] jupyter_server_terminals | extension was successfully linked. [I 2025-09-07 22:53:35.427 ServerApp] jupyterlab | extension was successfully linked. [I 2025-09-07 22:53:35.431 ServerApp] notebook | extension was successfully linked. [I 2025-09-07 22:53:35.765 ServerApp] notebook_shim | extension was successfully linked. [I 2025-09-07 22:53:35.765 ServerApp] panel.io.jupyter_server_extension | extension was successfully linked. [I 2025-09-07 22:53:35.799 ServerApp] notebook_shim | extension was successfully loaded. [I 2025-09-07 22:53:35.801 ServerApp] jupyter_lsp | extension was successfully loaded. [I 2025-09-07 22:53:35.801 ServerApp] jupyter_server_terminals | extension was successfully loaded. [I 2025-09-07 22:53:35.804 LabApp] JupyterLab extension loaded from D:\anaconda\Lib\site-packages\jupyterlab [I 2025-09-07 22:53:35.804 LabApp] JupyterLab application directory is D:\anaconda\share\jupyter\lab [I 2025-09-07 22:53:35.804 LabApp] Extension Manager is 'pypi'. [I 2025-09-07 22:53:36.030 ServerApp] jupyterlab | extension was successfully loaded. [I 2025-09-07 22:53:36.035 ServerApp] notebook | extension was successfully loaded. [I 2025-09-07 22:53:36.035 ServerApp] panel.io.jupyter_server_extension | extension was successfully loaded. [I 2025-09-07 22:53:36.036 ServerApp] Serving notebooks from local directory: C:\Users\hp [I 2025-09-07 22:53:36.036 ServerApp] Jupyter Server 2.15.0 is running at: [I 2025-09-07 22:53:36.036 ServerApp] http://localhost:8888/tree?token=8933ab18e189a7d5e7a41755474c11031f215680fc292ecb [I 2025-09-07 22:53:36.036 ServerApp] http://127.0.0.1:8888/tree?token=8933ab18e189a7d5e7a41755474c11031f215680fc292ecb [I 2025-09-07 22:53:36.036 ServerApp] Use Control-C to stop this server and shut down all kernels
09-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值