public class My_LinkedList<T>
{
private class Node
{
public T Data;
public Node Next;
public Node(T data)
{
Data = data;
Next = null;
}
}
private Node head;
private int count;
public My_LinkedList()
{
head = null;
count = 0;
}
public bool IsEmpty()
{
// ****** CODE HERE ******
return count == 0;
// ***********************
}
public void AddFirst(T item)
{
// ****** CODE HERE ******
Node newNode = new Node(item);
newNode.Next = head;
head = newNode;
count++;
// ***********************
}
public void Insert(int n, T item)
{
if (n < 0 || n > count)
{
throw new ArgumentOutOfRangeException($"{n} is not vaild");
}
// ****** CODE HERE ******
if (n == 0)
{
AddFirst(item);
}
else
{
Node newNode = new Node(item);
Node current = head;
for (int i = 1; i < n; i++)
{
current = current.Next;
}
newNode.Next = current.Next;
current.Next = newNode;
count++;
}
// ***********************
}
public T RemoveFirst()
{
// ****** CODE HERE ******
if (IsEmpty())
{
throw new InvalidOperationException("List is empty");
}
Node nodeToRemove = head;
head = head.Next;
count--;
return nodeToRemove.Data;
// ***********************
}
public T Remove(int n)
{
// ****** CODE HERE ******
if (n < 0 || n >= count)
{
throw new ArgumentOutOfRangeException($"{n} is not vaild");
}
if (n == 0)
{
return RemoveFirst();
}
Node current = head;
for (int i = 1; i < n; i++)
{
current = current.Next;
}
Node nodeToRemove = current.Next;
current.Next = nodeToRemove.Next;
count--;
return nodeToRemove.Data;
// ***********************
}
public int Count()
{
// ****** CODE HERE ******
return count;
// ***********************
}
public void PrintAllNodes()
{
Node current = head;
while (current != null)
{
Console.Write(current.Data + " ");
current = current.Next;
}
Console.WriteLine();
}
}
// 구현 확인을 위한 코드입니다.
class Program
{
static void Main(string[] args)
{
My_LinkedList<int> list = new My_LinkedList<int>();
list.AddFirst(3);
list.AddFirst(2);
list.AddFirst(1);
list.Insert(2, 4);
list.Insert(3, 5);
Console.WriteLine("Linked List:");
list.PrintAllNodes();
Console.WriteLine("Remove First: " + list.RemoveFirst());
Console.WriteLine("Remove at 2: " + list.Remove(2));
Console.WriteLine("Linked List:");
list.PrintAllNodes();
Console.WriteLine("Count: " + list.Count());
}
}
public class Bullet
{
public static int BulletCount = 0;
public Bullet() => ID = BulletCount++;
~Bullet() => BulletCount--;
public int ID { get; private set; }
public (float, float) Position { get; set; }
}
public class ObjectPool<T>
{
private LinkedList<T> pool;
private Func<T> createFunc;
private Action<T> resetAction;
public ObjectPool(int size, Func<T> createFunc, Action<T> resetAction = null)
{
this.pool = new LinkedList<T>();
this.createFunc = createFunc ?? throw new ArgumentNullException(nameof(createFunc));
this.resetAction = resetAction;
for (int i = 0; i < size; i++)
pool.AddFirst(createFunc());
}
public T GetObject()
{
// ****** CODE HERE ******
if (pool.Count == 0)
{
return createFunc();
}
else
{
T obj = pool.First.Value;
pool.RemoveFirst();
return obj;
}
// ***********************
}
public void ReleaseObject(T obj)
{
// ****** CODE HERE ******
resetAction?.Invoke(obj);
pool.AddFirst(obj);
// ***********************
}
public int Count()
{
return pool.Count;
}
}
// 구현 확인을 위한 코드입니다.
class Program
{
static void Main(string[] args)
{
ObjectPool<Bullet> bulletPool = new ObjectPool<Bullet>(
10,
() => new Bullet(),
bullet => { bullet.Position = (0, 0); }
);
// 현재 풀에 있는 객체 수를 출력합니다.
Console.WriteLine($"Objects in pool: {bulletPool.Count()}");
// 여러 개의 Bullet 객체를 풀에서 얻습니다.
for (int i = 1; i <= 5; i++)
{
Bullet bullet = bulletPool.GetObject();
bullet.Position = (i * 10.0f, i * 20.0f);
Console.WriteLine($"Got bullet with ID: {bullet.ID}, Position: {bullet.Position}");
}
// 풀에서 객체를 다시 얻어서 출력했다가 반환합니다. 객체는 초기화된 상태여야 합니다.
for (int i = 1; i <= 3; i++)
{
Bullet bullet = bulletPool.GetObject();
Console.WriteLine($"Got bullet with ID: {bullet.ID}, Position: {bullet.Position}");
bulletPool.ReleaseObject(bullet);
}
// 몇 개의 객체를 풀에 반환합니다.
for (int i = 6; i <= 10; i++)
{
Bullet bullet = bulletPool.GetObject();
bullet.Position = (i * 10.0f, i * 20.0f);
bulletPool.ReleaseObject(bullet);
}
// 최종적으로 풀에 있는 객체 수를 출력합니다.
Console.WriteLine($"Final objects in pool: {bulletPool.Count()}");
}
}