修剪线性单链表 [英] pruning a linear singly linked list

查看:76
本文介绍了修剪线性单链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述






我有一个包含100个元素的线性单链表。我想

修剪它[即删除一些节点],使得只有用户指定的元素

(仅说原始列表中的元素1,13,78和100)修剪后生存




有人能告诉我怎么做吗?我是一名科学家,而不是一名计算机工程师/学生。这将有助于我在

数据分析中开发一个应用程序。我将非常感谢你的建议。


干杯,

Anand。

解决方案

Anando写道:


这是一个算法(编程技术)问题,而不是严格的C问题,而不是
。因此,最好在comp.programming上询问


我有一个包含100个元素的线性单链表。我想修剪它[即删除一些节点],这样只有用户指定的元素
(比如原始列表的元素1,13,78和100)才能在修剪后存活下来。


使用双向链表(这只需要另一个


100指针变量与您的示例相比)生活会更容易。但是如果你使用SLL

你将需要在列表中走两个指针,指向

当前

节点一个指向它的前任。然后砍掉你想要的那些。

寻找所需的节点(1,13,78,100)

并将它们复制到一个新的更有意义列表,然后销毁旧的。

有人可以告诉我该怎么做吗?我是一名科学家,而不是一名计算机工程师/学生。这将有助于我开发数据分析中的应用程序。我将非常感谢你的建议。



-

Nick Keighley


我们建议,相反,用户可以利用GNU C的扩展,并忽略其他编译器的限制。除了

某些超级计算机和过时的小型机器之外,使用除了

bootstrapping之外的任何其他C编译器的原因更少

。 GNU CC。

(使用和移植GNU CC)


您好


谢谢你的算法。我正在考虑复制到新列表和

也销毁旧列表 - 但是当我复制数据时它是一个浅的

副本(即结构中的数据)没被复制)。是否有可能使用memcpy或其他东西来复制 - 如果可以的话,请给一个小的

复制元素的例子,以便复制数据?
Anand。




Nick Keighley的签名包含:

我们建议用户利用GNU C的扩展并忽略其他编译器的限制。除了某些超级计算机和过时的小型机器之外,除了用于引导GNU CC之外,使用任何其他C编译器的可能性较小且理由较少。




我很想看到有关此问题的一些讨论。我一直在改变我对是否使用扩展的看法。我最近决定开始使用扩展,并用

__extension__标记它们,并且不再担心它。但我一直在担心

它。目前,我正在使用的唯一扩展是微不足道的 - 枚举

带有尾随逗号,结构中未命名的联盟(我是否正确?

称这个微不足道? ),很少有typeof关键字。我用gcc独家工作

,虽然我试图保持在

标准范围内,但我真正拥有的唯一执行机制是-pedantic,

所以我知道所有我完全标准的努力都是浪费了b $ b。我想更频繁地开始使用typeof,但我不愿意这样做。
这样做。有没有办法判断哪些扩展被认为是

更可接受(即更可能被纳入标准)

比其他扩展?



Hi,

I have a linear singly linked list of 100 elements. I would like to
prune it [i.e delete some nodes] such that only user specified elements
(say only the elements 1, 13, 78 and 100 of the original list) survive
after pruning.

Can somebody show me how to do this ? I am a scientist and not a
computer engineer/student. This will help me develop an application in
data analysis. I will be grateful for your advice.

Cheers,
Anand.

解决方案

Anando wrote:

this is an algorithm (programming technique) question rather
than a strictly C question. Therefore it would be better asked
on comp.programming

I have a linear singly linked list of 100 elements. I would like to
prune it [i.e delete some nodes] such that only user specified elements
(say only the elements 1, 13, 78 and 100 of the original list) survive
after pruning.
life would be easier with a doubly linked list (that only takes another

100 pointer variables with your example). But if you have use a SLL
you''ll need to walk two pointers down the list one to point at the
current
node one to point to its predessor. Then chop out the ones you want.
It may make more sense to seek out the required nodes (1, 13, 78, 100)
and copy them to a new list, then destroy the old.
Can somebody show me how to do this ? I am a scientist and not a
computer engineer/student. This will help me develop an application in
data analysis. I will be grateful for your advice.


--
Nick Keighley

We recommend, rather, that users take advantage of the extensions of
GNU C and disregard the limitations of other compilers. Aside from
certain supercomputers and obsolete small machines, there is less
and less reason ever to use any other C compiler other than for
bootstrapping GNU CC.
(Using and Porting GNU CC)


Hi

Thanks for your algorithm. I was thinking of the copy to new list and
destroy old list as well - but when I copied the data it was a shallow
copy (i.e the data in the structure was not copied). Is it possible to
use memcpy or something to copy - if so could you please give a small
example of copying an element such that the data is also copied ?

Many thanks,
Anand.



Nick Keighley'' signature contains:

We recommend, rather, that users take advantage of the extensions of
GNU C and disregard the limitations of other compilers. Aside from
certain supercomputers and obsolete small machines, there is less
and less reason ever to use any other C compiler other than for
bootstrapping GNU CC.



I would love to see some discussions about this. I''m constantly
changing my mind about whether or not to use extensions. I most
recently decided to start using extensions, marking them with
__extension__, and stop worrying about it. But I keep worrying about
it. At the moment, the only extensions I''m using are trivial-- enums
with trailing commas, unnamed unions within structures (Am I correct in
calling that trivial?), and very rarely the typeof keyword. I work
exclusively with gcc, and although I''m trying to remain within
standards, the only enforcement mechanism I really have is -pedantic,
so for all I know all of my efforts to be completely standard are
wasted. I''d like to start using typeof more often, but am reluctant to
do so. Is there a way to deteremine which extensions are considered
more acceptable (ie more likely to be incorporated into the standard)
than others?


这篇关于修剪线性单链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆