Sicily 1934 移动小球

本文解析了一道链表操作的算法题,通过C语言实现,重点介绍了如何进行节点插入和删除的操作。参考了秋叶飘零的博客,并提供了一份完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址 http://sicily.3322.org/problem_status.php?pid=1934&p=1

此题参考了秋叶飘零的博客 http://adonis0147.blog.163.com/blog/static/162553854201061410316118/ 所以也就没什么好说的了, 秋叶同学做得很好呀~

代码

   
#include < stdio.h >
#define MAXN 500010
#define MAXM 100000

int main()
{
int t, n, m;
int i, counter, ptr;
int cmd, from, to;
int prev[MAXN], next[MAXN];

scanf(
" %d " , & t);
while (t -- )
{
scanf(
" %d%d " , & n, & m);
for (i = 0 ; i <= n; i ++ )
{
prev[i]
= i - 1 ;
next[i]
= i + 1 ;
}

while (m -- )
{
scanf(
" %d%d%d " , & cmd, & from, & to);

next[prev[from]]
= next[from];
prev[next[from]]
= prev[from];

if (cmd == 1 )
{
prev[from]
= prev[to];
next[from]
= to;

prev[to]
= next[prev[to]] = from;
}
else
{
prev[from]
= to;
next[from]
= next[to];

next[to]
= prev[next[to]] = from;
}
}

i
= 0 ;
ptr
= next[ 0 ];
while (i < n)
{
printf(
" %d " , ptr);
ptr
= next[ptr];
i
++ ;
}
printf(
" \n " );
}
return 0 ;
}

 

转载于:https://www.cnblogs.com/platero/archive/2010/11/14/1877080.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值