Facebook
From Beefy Lizard, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 239
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class Array {
  7.     unsigned int m_size;
  8.     T *arr;
  9. public:
  10.     class iterator {
  11.     private:
  12.         T *ptr;
  13.     public:
  14.         iterator(T *ptr) {
  15.             this->ptr = ptr;
  16.         }
  17.         iterator operator++(int) {
  18.             ptr++;
  19.             return *this;
  20.         }
  21.  
  22.         T operator*() {
  23.             return *ptr;
  24.         }
  25.         bool operator==(const iterator &it) {
  26.             return ptr == it.ptr;
  27.         }
  28.     };
  29.     Array(int size);
  30.     ~Array();
  31.     int size();
  32.     T& operator[](int index);
  33.     iterator begin() {
  34.         iterator it(arr);
  35.         return it;
  36.     }
  37.  
  38.     iterator end() {
  39.         iterator it(arr + m_size);
  40.         return it;
  41.     }
  42. };
  43.  
  44. template <typename T>
  45. Array<T>::Array(int size) {
  46.     this->m_size=size;
  47.     this->arr=new T[size];
  48. }
  49.  
  50. template <typename T>
  51. Array<T>::~Array() {
  52.     delete [] arr;
  53. }
  54.  
  55. template <typename T>
  56. int Array<T>::size() {
  57.     return m_size;
  58. }
  59.  
  60. template <typename T>
  61. T& Array<T>::operator[](int index) {
  62.     return arr[index];
  63. }
  64.  
  65.  
  66.  
  67. template <typename T>
  68. class LinkedList {
  69.     public:
  70.         struct Node;
  71.     private:
  72.         Node *head;
  73.         Node *tail;
  74.     public:
  75.         struct Node {
  76.             T t;
  77.             Node *next;
  78.         };
  79.  
  80.         class iterator {
  81.             private:
  82.                 Node *node;
  83.             public:
  84.                 iterator(Node *);
  85.                 iterator operator++(int);
  86.                 T operator*();
  87.                 bool operator==(const iterator &it);
  88.         };
  89.  
  90.         LinkedList();
  91.         T at(int);
  92.         T operator[](int);
  93.         void push_front(T);
  94.         void push_back(T);
  95.         void pop_front();
  96.         void pop_back();
  97.         iterator begin();
  98.         iterator end();
  99. };
  100.  
  101. template <typename T>
  102. LinkedList<T>::LinkedList() {
  103.     head = nullptr;
  104.     tail = nullptr;
  105. }
  106.  
  107. template <typename T>
  108. void LinkedList<T>::push_front(T t) {
  109.     Node *node = new Node{t, head};
  110.     head = node;
  111.  
  112.     if(tail == nullptr) {
  113.         tail = head;
  114.     }
  115. }
  116.  
  117. template <typename T>
  118. void LinkedList<T>::pop_front() {
  119.     Node *tmp = head;
  120.     head = head->next;
  121.     delete tmp;
  122. }
  123.  
  124. template <typename T>
  125. void LinkedList<T>::push_back(T t) {
  126.     Node *node = new Node{t, nullptr};
  127.     tail->next = node;
  128.     tail = node;
  129. }
  130.  
  131. template <typename T>
  132. void LinkedList<T>::pop_back() {
  133.     Node *old_tail = tail;
  134.     Node *prev = head;
  135.     while(prev->next != tail) {
  136.         prev = prev->next;
  137.     }
  138.     prev->next = nullptr;
  139.     tail = prev;
  140.     delete old_tail;
  141. }
  142.  
  143. template <typename T>
  144. T LinkedList<T>::at(int index) {
  145.     Node *node = head;
  146.     for(int i = 0; i < index; i++) {
  147.         node = node->next;
  148.     }
  149.  
  150.     return node->t;
  151. }
  152.  
  153. template <typename T>
  154. T LinkedList<T>::operator [](int index) {
  155.     return at(index);
  156.     // Mike Litoris
  157. }
  158.  
  159. template <typename T>
  160. typename LinkedList<T>::iterator LinkedList<T>::begin() {
  161.     return iterator(head);
  162. }
  163.  
  164. template <typename T>
  165. typename LinkedList<T>::iterator LinkedList<T>::end() {
  166.     return iterator(tail);
  167. }
  168.  
  169. template <typename T>
  170. bool LinkedList<T>::iterator::operator==(const LinkedList<T>::iterator &it) {
  171.     return it.node == this->node;
  172. }
  173.  
  174.  
  175. template <typename T>
  176. T LinkedList<T>::iterator::operator*() {
  177.     return this->node->t;
  178. }
  179.  
  180. template <typename T>
  181. typename LinkedList<T>::iterator LinkedList<T>::iterator::operator++(int) {
  182.     this->node = this->node->next;
  183.     return *this;
  184. }
  185.  
  186. template <typename T>
  187. LinkedList<T>::iterator::iterator(Node *n) {
  188.     node = n;
  189. }
  190.  
  191. int main() {
  192.     LinkedList<int> ll;
  193.     ll.push_front(5);
  194.     // 5
  195.     ll.push_front(10);
  196.     // 10 5
  197.     ll.push_front(15);
  198.     // 15 10 5
  199.     ll.push_back(20);
  200.     // 15 10 5 20
  201.     ll.pop_front();
  202.     // 10 5 20
  203.     cout << ll[2] << endl;
  204. //    ll[0] -> 5;
  205. //    ll.at(0) -> 5;
  206.  
  207.     for(auto it = ll.begin(); !(it == ll.end()); it++) {
  208.         cout << *it << endl;
  209.     }
  210.     return 0;
  211. }
  212.