如何随机混合在C中的链表 [英] How to Randomly Shuffle a Linked List in C

查看:117
本文介绍了如何随机混合在C中的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接列表,我想实现一个功能:

I have a Linked List and I want to implement a function:

Random_Shuffle_List(结构节点** Headptr) - 它输出一个列表,例如,每一个节点都随机地从原来的位置移动。

Random_Shuffle_List (struct node **Headptr) - which outputs a list such that every single node is randomly moved from its original position.

请帮我一个高效的算法来实现这一点。

Please help me with an efficient algorithm to achieve this.

推荐答案

我会建议朴素的办法:

  1. 在构建一个指针数组指向每个节点。
  2. 在洗牌阵列。这是方式,方法比随机链接结构更加简单。
  3. 在重新线列表中,通过在数组的顺序节点步进。

本使用额外的内存,当然,相对点点,但我认为这是在时间来执行(和理解),可能还运行时间方面更加高效,比方法直接链表上工作。

This uses a relatively tiny bit of extra memory, of course, but I think it's more "efficient" in terms of time to implement (and understand) and probably also run-time, than approaches working directly on the linked list.

这篇关于如何随机混合在C中的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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