Done with List&Stack

Added TargetCity and initialized it by the constructor in Sleigh.cs
This commit is contained in:
MarcUs7i 2024-12-19 16:32:46 +01:00
parent a9e467a1e2
commit d3e489b99c
4 changed files with 62 additions and 15 deletions

View file

@ -11,32 +11,44 @@ public sealed class List<TValue>
/// </summary> /// </summary>
public const int InitialCapacity = 4; public const int InitialCapacity = 4;
// TODO field private const float GrowthFactor = 1.5f; //no need to use double here
private TValue[] _data;
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="List{TValue}" /> with the <see cref="InitialCapacity" /> capacity /// Creates a new instance of the <see cref="List{TValue}" /> with the <see cref="InitialCapacity" /> capacity
/// </summary> /// </summary>
public List() public List()
{ {
// TODO _data = new TValue[InitialCapacity];
} }
/// <summary> /// <summary>
/// Gets the number of items currently stored in the list /// Gets the number of items currently stored in the list
/// </summary> /// </summary>
// TODO public int Count { get; private set; }
/// <summary> /// <summary>
/// Allows to access and item in the list by its index. /// Allows to access and item in the list by its index.
/// If an invalid index is supplied the default value for the <see cref="TValue" /> type is returned. /// If an invalid index is supplied the default value for the <see cref="TValue" /> type is returned.
/// </summary> /// </summary>
/// <param name="index">Index of the element to access; zero based</param> /// <param name="index">Index of the element to access; zero based</param>
// TODO public TValue this[int index]
{
get
{
if (index < 0 || index > Count)
{
return default!;
}
return _data[index];
}
}
/// <summary> /// <summary>
/// Gets the current capacity of the list /// Gets the current capacity of the list
/// </summary> /// </summary>
// TODO public int Capacity => _data.Length;
/// <summary> /// <summary>
/// Adds a new item to the list. Automatically increases capacity of the list if required. /// Adds a new item to the list. Automatically increases capacity of the list if required.
@ -46,6 +58,19 @@ public sealed class List<TValue>
/// <param name="value">Value to add to the list</param> /// <param name="value">Value to add to the list</param>
public void Add(TValue value) 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;
} }
} }

View file

@ -0,0 +1,7 @@
namespace SantaClausInc.Collections;
public sealed class Node<TValue>(TValue? value)
{
public TValue? Data { get; } = value;
public Node<TValue>? Next { get; set; }
}

View file

@ -8,24 +8,24 @@ namespace SantaClausInc.Collections;
/// <typeparam name="TValue">Type of the values stored in the stack</typeparam> /// <typeparam name="TValue">Type of the values stored in the stack</typeparam>
public sealed class Stack<TValue> public sealed class Stack<TValue>
{ {
// TODO _head field private Node<TValue>? _head;
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="Stack{TValue}" /> /// Creates a new instance of the <see cref="Stack{TValue}" />
/// </summary> /// </summary>
public Stack() public Stack()
{ {
// TODO _head = null;
} }
/// <summary> /// <summary>
/// Gets the number of items on the stack /// Gets the number of items on the stack
/// </summary> /// </summary>
// TODO public int Count { get; private set; }
// uncomment this once the fields are implemented to avoid redundant nullability warnings // uncomment this once the fields are implemented to avoid redundant nullability warnings
//[MemberNotNullWhen(false, nameof(_head))] [MemberNotNullWhen(false, nameof(_head))]
private bool IsEmpty => false; // TODO actual implementation private bool IsEmpty => _head == null;
/// <summary> /// <summary>
/// Adds an item on top of the stack /// Adds an item on top of the stack
@ -33,7 +33,10 @@ public sealed class Stack<TValue>
/// <param name="value">Value to add</param> /// <param name="value">Value to add</param>
public void Push(TValue value) public void Push(TValue value)
{ {
// TODO var newNode = new Node<TValue>(value);
Count++;
newNode.Next = _head; //get to root node
_head = newNode; //make the new node the head (the root node)
} }
/// <summary> /// <summary>
@ -41,12 +44,23 @@ public sealed class Stack<TValue>
/// If the stack is empty the default value for the <see cref="TValue" /> type is returned. /// If the stack is empty the default value for the <see cref="TValue" /> type is returned.
/// </summary> /// </summary>
/// <returns>Top element of the stack or default</returns> /// <returns>Top element of the stack or default</returns>
// TODO public TValue? Peek() => _head != null ? _head.Data : default;
/// <summary> /// <summary>
/// Gets and removes the top element of the stack. /// Gets and removes the top element of the stack.
/// If the stack is empty the default value for the <see cref="TValue" /> type is returned. /// If the stack is empty the default value for the <see cref="TValue" /> type is returned.
/// </summary> /// </summary>
/// <returns>Top element of the stack or default</returns> /// <returns>Top element of the stack or default</returns>
// TODO public TValue? Pop()
{
if (_head == null)
{
return default;
}
Count--;
var data = _head.Data;
_head = _head.Next;
return data;
}
} }

View file

@ -15,13 +15,14 @@ public sealed class Sleigh
/// <param name="targetCity">City this sleigh will go to during the delivery tour</param> /// <param name="targetCity">City this sleigh will go to during the delivery tour</param>
public Sleigh(int capacity, string targetCity) public Sleigh(int capacity, string targetCity)
{ {
TargetCity = targetCity;
// TODO // TODO
} }
/// <summary> /// <summary>
/// Gets the target city of this sleigh /// Gets the target city of this sleigh
/// </summary> /// </summary>
// TODO public string TargetCity { get; }
/// <summary> /// <summary>
/// Loads a pile of <see cref="Parcel" /> on the sleigh. /// Loads a pile of <see cref="Parcel" /> on the sleigh.