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.
Queue<T>
Arrays
LinkedList<T>
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.
, 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 , .
, . , C/++, std:: list - . , , , , ? , LinkedList .
, , , . , .
read-them-and-weep . .
, 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>, ?
, head , - . , / .
, [1,2,3,4,5], - 1, 6, 5 , , . 6 , [1,2,3,4,6].
, // , :
: - , ( ), . - , - ... !
LinkedList: - - ().
, ... ? , .
" " ( " " ) (, ), Linked-List, accessor .
, !
, , ... !
, ALOT . ,
,
, ? [1, 2, 3] -> [2, 3, 4] : [1, 2, 3] -> [1, 2, 4]? , LinkedList. , List <T> . , , , , . , , , , " ", .
[1, 2, 3] -> [2, 3, 4]
[1, 2, 3] -> [1, 2, 4]
Source: https://habr.com/ru/post/1755148/More articles:Sprite animation along a curve in XNA - c #Select elements that do not have specific words in their class attribute - jqueryCompilation error while compiling libyaml under windows 7 - compilationКак настроить сериализацию List в С# - c#Submit form with anchor + javascript is bad practice? - javascriptWhat is _acp-sync._tcp bonjour for? - bonjourcss file not loading - cssFancybox event does not fire - c #Simple game server - databaseПростой, единообразный и переносимый способ включения отслеживания и возврата в программу C - cAll Articles