#include using namespace std; template class Array { unsigned int m_size; T *arr; public: class iterator { private: T *ptr; public: iterator(T *ptr) { this->ptr = ptr; } iterator operator++(int) { ptr++; return *this; } T operator*() { return *ptr; } bool operator==(const iterator &it) { return ptr == it.ptr; } }; Array(int size); ~Array(); int size(); T& operator[](int index); iterator begin() { iterator it(arr); return it; } iterator end() { iterator it(arr + m_size); return it; } }; template Array::Array(int size) { this->m_size=size; this->arr=new T[size]; } template Array::~Array() { delete [] arr; } template int Array::size() { return m_size; } template T& Array::operator[](int index) { return arr[index]; } template class LinkedList { public: struct Node; private: Node *head; Node *tail; public: struct Node { T t; Node *next; }; class iterator { private: Node *node; public: iterator(Node *); iterator operator++(int); T operator*(); bool operator==(const iterator &it); }; LinkedList(); T at(int); T operator[](int); void push_front(T); void push_back(T); void pop_front(); void pop_back(); iterator begin(); iterator end(); }; template LinkedList::LinkedList() { head = nullptr; tail = nullptr; } template void LinkedList::push_front(T t) { Node *node = new Node{t, head}; head = node; if(tail == nullptr) { tail = head; } } template void LinkedList::pop_front() { Node *tmp = head; head = head->next; delete tmp; } template void LinkedList::push_back(T t) { Node *node = new Node{t, nullptr}; tail->next = node; tail = node; } template void LinkedList::pop_back() { Node *old_tail = tail; Node *prev = head; while(prev->next != tail) { prev = prev->next; } prev->next = nullptr; tail = prev; delete old_tail; } template T LinkedList::at(int index) { Node *node = head; for(int i = 0; i < index; i++) { node = node->next; } return node->t; } template T LinkedList::operator [](int index) { return at(index); // Mike Litoris } template typename LinkedList::iterator LinkedList::begin() { return iterator(head); } template typename LinkedList::iterator LinkedList::end() { return iterator(tail); } template bool LinkedList::iterator::operator==(const LinkedList::iterator &it) { return it.node == this->node; } template T LinkedList::iterator::operator*() { return this->node->t; } template typename LinkedList::iterator LinkedList::iterator::operator++(int) { this->node = this->node->next; return *this; } template LinkedList::iterator::iterator(Node *n) { node = n; } int main() { LinkedList ll; ll.push_front(5); // 5 ll.push_front(10); // 10 5 ll.push_front(15); // 15 10 5 ll.push_back(20); // 15 10 5 20 ll.pop_front(); // 10 5 20 cout << ll[2] << endl; // ll[0] -> 5; // ll.at(0) -> 5; for(auto it = ll.begin(); !(it == ll.end()); it++) { cout << *it << endl; } return 0; }