Which should be used: array versus linked list?

I plan to implement a limited queue without using a class Queue<T>. After reading the "for" and "against" Arraysand LinkedList<T>I am more inclined to using Array functions to implement the queue. The collection will be a fixed size. I just want to add and remove items from the queue.

sort of

public class BoundedQueue<T>
{
   private T[] queue;
   int queueSize;

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

instead

public class BoundedQueue<T>
{
   private LinkedList<T> queue;
   int queueSize;

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

I chose Array because of efficiency and because the collection is a fixed size. I would like to have other opinions on this matter. Thank.

+3
source share
6 answers

, Queue<T>, , . . - , LinkedList<T>, - .

, .NET .NET Reflector. , :

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 , .

+1

, . , C/++, std:: list - . , , , , ? , LinkedList .

, , , . , .

read-them-and-weep . .

+2

, Queue<T> , , LinkedList<T> ( ). A Queue<T> . :

public class BoundedQueue<T>
{
    private Queue<T> _queue;
    private int _maxSize;

    public BoundedQueue(int maxSize)
    {
        if (maxSize <= 0)
            throw new ArgumentOutOfRangeException("maxSize");

        _queue = new Queue<T>(maxSize);
        _maxSize = maxSize;
    }

    public int Count
    {
        get { return _queue.Count; }
    }

    /// <summary>
    /// Adds a new item to the queue and, if the queue is at its
    /// maximum capacity, also removes the oldest item
    /// </summary>
    /// <returns>
    /// True if an item was dequeued during this operation;
    /// otherwise, false
    /// </returns>
    public bool EnqueueDequeue(T value, out T dequeued)
    {
        dequeued = default(T);
        bool dequeueOccurred = false;

        if (_queue.Count == _maxSize)
        {
            dequeued = _queue.Dequeue();
            dequeueOccurred = true;
        }

        _queue.Enqueue(value);

        return dequeueOccurred;
    }
}

, Queue<T>, ?

+1

, head , - . , / .

, [1,2,3,4,5], - 1, 6, 5 , , . 6 , [1,2,3,4,6].

0

, // , :

: - , ( ), . - , - ... !

LinkedList: - - ().

, ... ?   , .

" " ( " " ) (, ), Linked-List, accessor .

, !

, , ... !

, ALOT . ,

,

0

, ? [1, 2, 3] -> [2, 3, 4] : [1, 2, 3] -> [1, 2, 4]? , LinkedList. , List <T> . , , , , . , , , , " ", .

0

Source: https://habr.com/ru/post/1755148/


All Articles