/// <summary> /// Removes and returns the object at the beginning of the <see cref="LinkedListQueue{T}"/>. /// </summary> /// <returns>The object that is removed from the beginning of the <see cref="LinkedListQueue{T}"/>.</returns> /// <exception cref="InvalidOperationException"> /// Thrown when the <see cref="LinkedListQueue{T}"/> is empty. /// </exception> public T Dequeue() { if (_count == 0 || _first == null) { throw new InvalidOperationException("Trying to dequeue from an empty queue."); } T removed = _first.Value; // This is the last item in the queue. if (_first == _last) { _first.Invalidate(); _first = _last = null; } else { QueueNode <T> temp = _first; _first = _first.Next; _first.Previous = null; temp.Invalidate(); temp = null; } _count--; _version++; return(removed); }
/// <summary> /// Tries to remove and return the object at the beginning of the <see cref="LinkedListQueue{T}"/>. /// </summary> /// <param name="result">The object that is removed from the beginning of the <see cref="LinkedListQueue{T}"/>.</param> /// <returns>True if the object was removed from the queue, false otherwise.</returns> public bool TryDequeue(out T result) { if (_first == null || _count == 0) { result = default(T); return(false); } else { result = _first.Value; // This is the last item in the queue. if (_first == _last) { _first.Invalidate(); _first = _last = null; } else { QueueNode <T> temp = _first; _first = _first.Next; _first.Previous = null; temp.Invalidate(); temp = null; } _count--; _version++; return(true); } }
/// <summary> /// Removes all items from the queue and resets the count. /// </summary> public void Clear() { QueueNode <T> current = _first; while (current != null) { QueueNode <T> temp = current; current = current.Next; temp.Invalidate(); } _first = _last = null; _count = 0; _version++; }