daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iterator>
  3. #include <memory>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8.     int data;
  9.     Node * prev;
  10.     Node * next;
  11.  
  12.     Node() {
  13.     }
  14. };
  15.  
  16. class List_iterator {
  17. private:
  18.     Node * it;
  19. public:
  20.     List_iterator(Node * input) {
  21.         it = input;
  22.     }
  23.     int operator*() {
  24.         return it->data;
  25.     }
  26.     List_iterator& operator++() {
  27.         it = it->next;
  28.         return *this;
  29.     }
  30.     List_iterator& operator--() {
  31.         it = it->prev;
  32.         return *this;
  33.     }
  34. };
  35.  
  36. class List {
  37. private:
  38.     Node * first;
  39.     Node * last;
  40.     size_t list_size;
  41. public:
  42.     List() {
  43.         auto fisrt = new Node;
  44.         last = first;
  45.         list_size = 0;
  46.     }
  47.     ~List() {
  48.         /*while (first) {
  49.             last = first->next;
  50.             delete first;
  51.             first = last;
  52.         }*/
  53.     }
  54.     size_t size() {
  55.         return list_size;
  56.     }
  57.     void push_back(int elem) {
  58.         Node * tmp = new Node;
  59.         if (first) {
  60.             std::cout << "wtf";
  61.         }
  62.         /*last->data = elem;
  63.         last->next = tmp;
  64.         tmp->prev = last;
  65.         tmp->next = nullptr;
  66.         last = tmp;
  67.         list_size += 1;*/
  68.     }
  69.     void push_front(int elem) {
  70.         Node * tmp = new Node;
  71.         tmp->data = elem;
  72.         tmp->prev = nullptr;
  73.         if (first != nullptr) {
  74.             tmp->next = first;
  75.             first->prev = tmp;
  76.             first = tmp;
  77.         }
  78.         else {
  79.             tmp->next = last;
  80.             first = tmp;
  81.             last->prev = tmp;
  82.         }
  83.         list_size += 1;
  84.     }
  85.     int pop_back() {
  86.         if (first != nullptr) {
  87.             int result = (last->prev)->data;
  88.             Node * tmp = last->prev->prev;
  89.             tmp->next = last;
  90.             delete last->prev;
  91.             last->prev = tmp;
  92.             list_size -= 1;
  93.             return result;
  94.         }
  95.     }
  96.     int pop_front() {
  97.         if (first != nullptr) {
  98.             int result = first->data;
  99.             Node * tmp = first->next;
  100.             tmp->prev = nullptr;
  101.             delete first;
  102.             first = tmp;
  103.             return result;
  104.             list_size -= 1;
  105.         }
  106.     }
  107.     List_iterator begin() {
  108.         return List_iterator(first);
  109.     }
  110.     List_iterator end() {
  111.         return List_iterator(last);
  112.     }
  113. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top