Last active
August 30, 2015 05:59
-
-
Save south37/2186922347a0762102dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstddef> | |
template <class T> class List { | |
public: | |
typedef std::size_t size_type; | |
typedef T value_type; | |
List() : next(nullptr), val(0) {} | |
explicit List(size_type n, const value_type& newVal = value_type()); | |
List(const List& l) { create(l); } | |
List& operator=(const List&); | |
~List() { uncreate(); } | |
bool empty() const { return next; } | |
List* next; | |
value_type val; | |
private: | |
void create(const List& l); | |
void uncreate(); | |
}; | |
template <class T> | |
List<T>::List(size_type n, const T& newVal) | |
{ | |
val = newVal; | |
if (n < 1) { | |
next = nullptr; | |
} else { | |
next = new List(n - 1, newVal); | |
} | |
} | |
template <class T> | |
List<T>& List<T>::operator=(const List& rhs) | |
{ | |
if (&rhs != this) { | |
uncreate(); | |
create(rhs); | |
} | |
return *this; | |
} | |
template <class T> | |
void List<T>::create(const List& l) | |
{ | |
val = l.val; | |
if (l.next) { | |
next = new List(*l.next); | |
} else { | |
next = nullptr; | |
} | |
} | |
template <class T> | |
void List<T>::uncreate() | |
{ | |
if (next) { | |
delete next; | |
next = nullptr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment