생각해보니 자료 구조를 한번도 안만들어본것 같아서 리스트를 만들어봤어요
제가 제대로 짠게 맞는지, 개선 사항이 있다면 어디를 어떻게 고쳐야 하는지 알려주시면 감사하겠습니다
표준은 gnu++2a입니다
#include <string.h> #undef NODISCARD #define NODISCARD [[nodiscard]] #undef INLINE #define INLINE __attribute__((always_inline)) namespace Common { template<class T> class List { private: T *Array; T (*NullValue)(); int Capacity = 2; int Length = 0; void Resize(int size) { T *newArr = new T[size]; for (int i = 0; i < size; ++i) newArr[i] = NullValue(); memcpy(newArr, Array, size * sizeof(T)); delete[] Array; Array = newArr; Capacity = size; } public: List() = delete; List(List<T> &) = delete; explicit List(T (*null)(), int capacity = 2) : NullValue(null), Capacity(capacity) { Array = new T[Capacity]; for (int i = 0; i < Capacity; ++i) Array[i] = NullValue(); } NODISCARD INLINE int GetLength() { return Length; } NODISCARD INLINE T ElementAt(const int index) { return Array[index]; } NODISCARD INLINE bool Contains(T item) { return Find(item) != -1; } NODISCARD int Find(T item) { for (int i = 0; i < Length; ++i) { if (Array[i] == item) return i; } return -1; } void Add(T item) { if (Length == Capacity) Resize(Capacity * 2); Array[Length++ - 1] = item; } void Insert(T item, int index) { if (index > Length) index = Length; if (Length == Capacity) Resize(Capacity * 2); if (Length != 0) memmove(Array + index + 1, Array + index, sizeof(T) * (Length - index)); Array[index] = item; Length++; } void Remove(T item) { int pos = Find(item); if (pos == -1) return; RemoveAt(pos); } void RemoveAt(int pos) { Array[pos] = NullValue(); memmove(Array + pos, Array + pos + 1, sizeof(T) * (Length - pos - 1)); Array[Length-- - 1] = NullValue(); } ~List() { delete[] Array; } }; }