I am just learning about C++ templates and generic types, I decided it would be nice to try to create a generic container class as a challenge and test my knowledge in the process. Here is what I have come up with.
#include <iostream>
#include <cstdlib>
#include <cassert>
template <typename __T>
class List{
private:
struct node{
__T data;
node* next;
node* prev;
};
typedef long long unsigned int c_size;
node* head;
node* tail;
node* current;
c_size m_S;
public:
List()
:head{NULL},tail{NULL},current{NULL},m_S{0}
{}
List(std::initializer_list<__T> __l)
:head{NULL},tail{NULL},current{NULL},m_S{0}
{
for(auto const& elem:__l){
insert_back(elem);
}
}
void insert_front(__T _d){
++m_S;
node* new_node{new node};
new_node->data = _d;
new_node->next = head;
new_node->prev = NULL;
if (head != NULL){
head->prev = new_node;
}
else
{
tail = new_node;
}
head = new_node;
}
void print(){
if (m_S == 0) return;
node *current = head;
while (current->next != NULL){
std::cout << current->data << ", ";
current = current->next;
}
std::cout << current->data << '\n';
}
void insert_back(__T _d){
++m_S;
node* new_node = new node;
new_node->data = _d;
new_node->next = NULL;
node* last = head;
if (head == NULL) {
new_node->prev = NULL;
head = new_node;
tail = new_node;
}
else{
tail->next = new_node;
new_node->prev = tail;
tail = new_node;
}
}
constexpr __T first(){
assert(head != NULL && "Calling .front() on an empty list");
return head->data;
}
constexpr __T last(){
assert(tail != NULL && "Calling. last() on an empty list");
return tail->data;
}
constexpr c_size size(){return m_S;}
void remove_all(){
current = head;
while(current->next != NULL){
current = current->next;
delete current;
}
head = NULL;
tail = NULL;
current = NULL;
}
};
Thanks in advance !
__T
double underscores as prefix for a symbol are reserved for compiler internal implementations. \$\endgroup\$List
class with that of the standardstd::list
. \$\endgroup\$