diff --git a/SantaClausInc/Collections/List.cs b/SantaClausInc/Collections/List.cs index a7de2ce..3993c3a 100644 --- a/SantaClausInc/Collections/List.cs +++ b/SantaClausInc/Collections/List.cs @@ -11,32 +11,44 @@ public sealed class List /// public const int InitialCapacity = 4; - // TODO field + private const float GrowthFactor = 1.5f; //no need to use double here + private TValue[] _data; /// /// Creates a new instance of the with the capacity /// public List() { - // TODO + _data = new TValue[InitialCapacity]; } /// /// Gets the number of items currently stored in the list /// - // TODO + public int Count { get; private set; } /// /// Allows to access and item in the list by its index. /// If an invalid index is supplied the default value for the type is returned. /// /// Index of the element to access; zero based - // TODO + public TValue this[int index] + { + get + { + if (index < 0 || index > Count) + { + return default!; + } + + return _data[index]; + } + } /// /// Gets the current capacity of the list /// - // TODO + public int Capacity => _data.Length; /// /// Adds a new item to the list. Automatically increases capacity of the list if required. @@ -46,6 +58,19 @@ public sealed class List /// Value to add to the list public void Add(TValue value) { - // TODO + if (Capacity == Count) + { + Grow(); + } + + _data[Count++] = value; + } + + private void Grow() + { + var newCapacity = Math.Round(Capacity * GrowthFactor); + var newData = new TValue[(int)newCapacity]; + Array.Copy(_data, newData, Count); + _data = newData; } } diff --git a/SantaClausInc/Collections/Node.cs b/SantaClausInc/Collections/Node.cs new file mode 100644 index 0000000..875a7b8 --- /dev/null +++ b/SantaClausInc/Collections/Node.cs @@ -0,0 +1,7 @@ +namespace SantaClausInc.Collections; + +public sealed class Node(TValue? value) +{ + public TValue? Data { get; } = value; + public Node? Next { get; set; } +} \ No newline at end of file diff --git a/SantaClausInc/Collections/Stack.cs b/SantaClausInc/Collections/Stack.cs index 724c8c7..eba5465 100644 --- a/SantaClausInc/Collections/Stack.cs +++ b/SantaClausInc/Collections/Stack.cs @@ -8,24 +8,24 @@ namespace SantaClausInc.Collections; /// Type of the values stored in the stack public sealed class Stack { - // TODO _head field + private Node? _head; /// /// Creates a new instance of the /// public Stack() { - // TODO + _head = null; } /// /// Gets the number of items on the stack /// - // TODO + public int Count { get; private set; } // uncomment this once the fields are implemented to avoid redundant nullability warnings - //[MemberNotNullWhen(false, nameof(_head))] - private bool IsEmpty => false; // TODO actual implementation + [MemberNotNullWhen(false, nameof(_head))] + private bool IsEmpty => _head == null; /// /// Adds an item on top of the stack @@ -33,7 +33,10 @@ public sealed class Stack /// Value to add public void Push(TValue value) { - // TODO + var newNode = new Node(value); + Count++; + newNode.Next = _head; //get to root node + _head = newNode; //make the new node the head (the root node) } /// @@ -41,12 +44,23 @@ public sealed class Stack /// If the stack is empty the default value for the type is returned. /// /// Top element of the stack or default - // TODO + public TValue? Peek() => _head != null ? _head.Data : default; /// /// Gets and removes the top element of the stack. /// If the stack is empty the default value for the type is returned. /// /// Top element of the stack or default - // TODO + public TValue? Pop() + { + if (_head == null) + { + return default; + } + + Count--; + var data = _head.Data; + _head = _head.Next; + return data; + } } diff --git a/SantaClausInc/Core/Sleigh.cs b/SantaClausInc/Core/Sleigh.cs index 8b78805..90fd4ee 100644 --- a/SantaClausInc/Core/Sleigh.cs +++ b/SantaClausInc/Core/Sleigh.cs @@ -15,13 +15,14 @@ public sealed class Sleigh /// City this sleigh will go to during the delivery tour public Sleigh(int capacity, string targetCity) { + TargetCity = targetCity; // TODO } /// /// Gets the target city of this sleigh /// - // TODO + public string TargetCity { get; } /// /// Loads a pile of on the sleigh.