Done with List&Stack
Added TargetCity and initialized it by the constructor in Sleigh.cs
This commit is contained in:
parent
a9e467a1e2
commit
d3e489b99c
4 changed files with 62 additions and 15 deletions
|
|
@ -11,32 +11,44 @@ public sealed class List<TValue>
|
|||
/// </summary>
|
||||
public const int InitialCapacity = 4;
|
||||
|
||||
// TODO field
|
||||
private const float GrowthFactor = 1.5f; //no need to use double here
|
||||
private TValue[] _data;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="List{TValue}" /> with the <see cref="InitialCapacity" /> capacity
|
||||
/// </summary>
|
||||
public List()
|
||||
{
|
||||
// TODO
|
||||
_data = new TValue[InitialCapacity];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of items currently stored in the list
|
||||
/// </summary>
|
||||
// TODO
|
||||
public int Count { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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>
|
||||
/// Gets the current capacity of the list
|
||||
/// </summary>
|
||||
// TODO
|
||||
public int Capacity => _data.Length;
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
7
SantaClausInc/Collections/Node.cs
Normal file
7
SantaClausInc/Collections/Node.cs
Normal 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; }
|
||||
}
|
||||
|
|
@ -8,24 +8,24 @@ namespace SantaClausInc.Collections;
|
|||
/// <typeparam name="TValue">Type of the values stored in the stack</typeparam>
|
||||
public sealed class Stack<TValue>
|
||||
{
|
||||
// TODO _head field
|
||||
private Node<TValue>? _head;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the <see cref="Stack{TValue}" />
|
||||
/// </summary>
|
||||
public Stack()
|
||||
{
|
||||
// TODO
|
||||
_head = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of items on the stack
|
||||
/// </summary>
|
||||
// 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;
|
||||
|
||||
/// <summary>
|
||||
/// Adds an item on top of the stack
|
||||
|
|
@ -33,7 +33,10 @@ public sealed class Stack<TValue>
|
|||
/// <param name="value">Value to add</param>
|
||||
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>
|
||||
|
|
@ -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.
|
||||
/// </summary>
|
||||
/// <returns>Top element of the stack or default</returns>
|
||||
// TODO
|
||||
public TValue? Peek() => _head != null ? _head.Data : default;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,14 @@ public sealed class Sleigh
|
|||
/// <param name="targetCity">City this sleigh will go to during the delivery tour</param>
|
||||
public Sleigh(int capacity, string targetCity)
|
||||
{
|
||||
TargetCity = targetCity;
|
||||
// TODO
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the target city of this sleigh
|
||||
/// </summary>
|
||||
// TODO
|
||||
public string TargetCity { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads a pile of <see cref="Parcel" /> on the sleigh.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue