Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<class T> class List
- {
- private:
- enum class CtorType { Empty, Cons };
- union U
- {
- struct Cons { T item; List<T> * next; } cons;
- struct Empty {} empty;
- };
- CtorType type;
- U constructor;
- List(void)
- : type(CtorType::Empty), item(), next(nullptr)
- {}
- List(const T & item, const List<T> & next)
- : type(CtorType::Cons), item(item), next(&next)
- {}
- public:
- bool IsEmpty()
- {
- return this->type == CtorType::Empty;
- }
- T Head()
- {
- switch(this->type)
- {
- case CtorType::Ctor: return this->constructor.cons.item;
- default: assert(false, "Attempt to get Head on empty list");
- }
- }
- static List<T> CreateEmpty()
- {
- return List<T>();
- }
- static List<T> CreateCons(const T & item, const List<T> & next)
- {
- return List<T>(item, next);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement