< Summary

Information
Class: MoreStructures.Queues.LinkedListQueue<T>
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/Queues/LinkedListQueue.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_Value()100%1100%
.ctor(...)100%1100%
get_Next()100%1100%
get_Head()100%1100%
get_Last()100%1100%
get_Count()100%1100%
Peek()100%2100%
Dequeue()100%4100%
Enqueue(...)100%2100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/Queues/LinkedListQueue.cs

#LineLine coverage
 1namespace MoreStructures.Queues;
 2
 3/// <summary>
 4/// A <see cref="IQueue{T}"/> implementation based on a singly-linked linked list of items.
 5/// </summary>
 6/// <typeparam name="T"><inheritdoc cref="IQueue{T}"/></typeparam>
 7/// <remarks>
 8///     <para id="advantages">
 9///     ADVANTAGES AND DISADVANTAGES
 10///     <br/>
 11///     - The advantages and disadvantages of using a linked list over an array-based implementation are the same
 12///       as the ones described in <see cref="Stacks.LinkedListStack{T}"/>.
 13///     </para>
 14/// </remarks>
 15public class LinkedListQueue<T> : IQueue<T>
 16{
 86317    private sealed record Node(T Value, Node? Next)
 28618    {
 83619        public Node? Next { get; set; } = Next;
 28620    }
 21
 147022    private Node? Head { get; set; } = null;
 86323    private Node? Last { get; set; } = null;
 24
 25    /// <inheritdoc path="//*[not(self::remarks)]"/>
 26    /// <remarks>
 27    /// Stored and updated in constant time at each <see cref="Enqueue(T)"/> and <see cref="Dequeue"/>.
 28    /// <br/>
 29    /// Time and Space Complexity are O(1).
 30    /// </remarks>
 122831    public int Count { get; private set; } = 0;
 32
 33    /// <inheritdoc path="//*[not(self::remarks)]"/>
 34    /// <remarks>
 35    /// Checks the front of the underlying linked list.
 36    /// <br/>
 37    /// Time and Space Complexity are O(1).
 38    /// </remarks>
 39    public T Peek()
 940    {
 941        if (Head == null)
 342            throw new InvalidOperationException($"Cannot {nameof(Peek)} on an empty queue.");
 43
 644        return Head.Value;
 645    }
 46
 47    /// <inheritdoc path="//*[not(self::remarks)]"/>
 48    /// <remarks>
 49    /// First, it retrieves the item referenced by the front of the underlying linked list.
 50    /// <br/>
 51    /// Then it updates the reference to the head of the linked list, to point to its "next".
 52    /// The reference to the last item is also updated, if the head is set to point to null.
 53    /// <br/>
 54    /// Time and Space Complexity are O(1).
 55    /// </remarks>
 56    public T Dequeue()
 28857    {
 28858        if (Head == null)
 359            throw new InvalidOperationException($"Can't {nameof(Dequeue)} on an empty queue.");
 60
 28561        var head = Head.Value;
 28562        Head = Head.Next;
 28563        Count--;
 28564        if (Head == null)
 2065            Last = null;
 28566        return head;
 28567    }
 68
 69    /// <inheritdoc path="//*[not(self::remarks)]"/>
 70    /// <remarks>
 71    /// Creates a new node wrapping the provided <paramref name="item"/>.
 72    /// <br/>
 73    /// Then, it appends it to the back of the linked list of items, updating the reference to the last item of the
 74    /// list.
 75    /// <br/>
 76    /// It also updates the head, if the list was previously empty, to point to the newly created item.
 77    /// <br/>
 78    /// Time and Space Complexity are O(1).
 79    /// </remarks>
 80    public void Enqueue(T item)
 28681    {
 28682        var node = new Node(item, null);
 28683        if (Last == null)
 2184        {
 2185            Last = Head = node;
 2186        }
 87        else
 26588        {
 26589            Last.Next = node;
 26590            Last = node;
 26591        }
 28692        Count++;
 28693    }
 94}