应该使用哪个:数组vs链表?(which should be used: array vs linked list?)

编程入门 行业动态 更新时间:2024-10-28 06:22:25
应该使用哪个:数组vs链表?(which should be used: array vs linked list?)

我打算在不使用Queue<T>类的情况下实现有界队列。 在阅读了Arrays和LinkedList<T>优缺点之后,我更倾向于使用Array来实现队列功能。 该系列将是固定尺寸。 我只想添加和删除队列中的项目。

就像是

public class BoundedQueue<T> { private T[] queue; int queueSize; public BoundedQueue(int size) { this.queueSize = size; queue = new T[size + 1]; } }

代替

public class BoundedQueue<T> { private LinkedList<T> queue; int queueSize; public BoundedQueue(int size) { this.queueSize = size; queue = new LinkedList<T>(); } }

由于效率和集合是固定大小的事实,我选择了Array。 想就此得到其他意见。 谢谢。

I am planning to implement a bounded queue without using the Queue<T> class. After reading pros and cons of Arrays and LinkedList<T>, I am leaning more towards using Array to implement queue functionality. The collection will be fixed size. I just want to add and remove items from the queue.

something like

public class BoundedQueue<T> { private T[] queue; int queueSize; public BoundedQueue(int size) { this.queueSize = size; queue = new T[size + 1]; } }

instead of

public class BoundedQueue<T> { private LinkedList<T> queue; int queueSize; public BoundedQueue(int size) { this.queueSize = size; queue = new LinkedList<T>(); } }

I have selected Array because of efficiency and due to the fact that collection is fixed size. Would like to get other opinions on this. Thanks.

最满意答案

您当然应该使用Queue<T> ,但在您提出的问题中,您说您不想使用队列而是自己实现队列。 您需要首先考虑此类的用例。 如果你想快速实现某些东西,你可以使用LinkedList<T>但是对于通用库你可能想要更快的东西。

您可以使用.NET Reflector了解它是如何在.NET中实现的。 这些是它拥有的领域:

private T[] _array; private const int _DefaultCapacity = 4; private static T[] _emptyArray; private const int _GrowFactor = 200; private int _head; private const int _MinimumGrow = 4; private const int _ShrinkThreshold = 0x20; private int _size; [NonSerialized] private object _syncRoot; private int _tail; private int _version;

如您所见,它使用数组。 关于如何调整数组大小的许多领域也很复杂。 即使您正在实现有界数组,您也希望允许该数组大于容量,以避免不断地在内存中移动项目。

关于螺纹安全,两种类型都不提供任何保证。 例如,在LinkedList<T>的文档中,它说:

此类型不是线程安全的。 如果需要多个线程访问LinkedList,则需要实现自己的同步机制。

You should of course use a Queue<T>, but in the question you said that you don't want to use queue and instead implement the queue yourself. You need to consider first your use case for this class. If you want to implement something quickly you can use a LinkedList<T> but for a general purpose library you would want something faster.

You can see how it is implemented in .NET by using .NET Reflector. These are the fields it has:

private T[] _array; private const int _DefaultCapacity = 4; private static T[] _emptyArray; private const int _GrowFactor = 200; private int _head; private const int _MinimumGrow = 4; private const int _ShrinkThreshold = 0x20; private int _size; [NonSerialized] private object _syncRoot; private int _tail; private int _version;

As you can see it uses an array. It is also quite complicated with many fields concerned with how the array should be resized. Even if you are implementing a bounded array you would want to allow that the array can be larger than the capacity to avoid constantly moving items in memory.

Regarding thread safety, neither type offers any guarantees. For example in the documentation for LinkedList<T> it says this:

This type is not thread safe. If the LinkedList needs to be accessed by multiple threads, you will need to implement their own synchronization mechanism.

更多推荐

本文发布于:2023-08-06 02:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   链表   array   linked   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!