Advertisement
avr39ripe

stackOverListFw

Jul 19th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <typename ElemT>
  4. class ListFw
  5. {
  6. public:
  7.     class Node
  8.     {
  9.     public:
  10.         ElemT data;
  11.         Node* next;
  12.         Node(const ElemT& dataP, Node* nextP = nullptr)
  13.             :data{ dataP }, next{ nextP }{};
  14.  
  15.     };
  16. private:
  17.     Node* head;
  18.     Node* tail;
  19.     size_t size;
  20. public:
  21.     ListFw() : head{ nullptr }, tail{ nullptr }, size{ 0 } {}
  22.     ElemT& front() { return head->data; };
  23.     const ElemT& front()const { return head->data; };
  24.     ElemT& back() { return tail->data; };
  25.     const ElemT& back()const { return tail->data; };
  26.     const size_t getSize() const { return size; };
  27.     bool empty() const { return size == 0; };
  28.     void push_back(const ElemT& data);
  29.     void push_front(const ElemT& data);
  30.     bool pop_back();
  31.     bool pop_front();
  32.     void clear();
  33.     ~ListFw();
  34. };
  35.  
  36. template <typename ElemT>
  37. void ListFw<ElemT>::push_back(const ElemT& data)
  38. {
  39.     if (!head)
  40.     {
  41.         head = new Node{ data };
  42.         tail = head;
  43.     }
  44.     else
  45.     {
  46.         tail->next = new Node{ data };
  47.         tail = tail->next;
  48.     }
  49.     ++size;
  50. }
  51.  
  52. template <typename ElemT>
  53. void ListFw<ElemT>::push_front(const ElemT& data)
  54. {
  55.     head = new Node{ data, head };
  56.     ++size;
  57.  
  58.     if (!tail)
  59.     {
  60.         tail = head;
  61.     }
  62. }
  63.  
  64. template <typename ElemT>
  65. bool ListFw<ElemT>::pop_front()
  66. {
  67.     if (head)
  68.     {
  69.         Node* tmp{ head };
  70.         head = head->next;
  71.         delete tmp;
  72.         --size;
  73.         return true;
  74.     }
  75.     return false;
  76. }
  77.  
  78. template <typename ElemT>
  79. bool ListFw<ElemT>::pop_back()
  80. {
  81.     if (tail)
  82.     {
  83.         if (tail == head)
  84.         {
  85.             delete tail;
  86.             size = 0;
  87.             tail = nullptr;
  88.             head = nullptr;
  89.             return true;
  90.         }
  91.         for (Node* curr{ head }; curr; curr = curr->next)
  92.         {
  93.             if (curr->next == tail)
  94.             {
  95.                 delete tail;
  96.                 curr->next = nullptr;
  97.                 tail = curr;
  98.                 --size;
  99.                 return true;
  100.             }
  101.         }
  102.     }
  103.     return false;
  104. }
  105.  
  106. template <typename ElemT>
  107. void ListFw<ElemT>::clear()
  108. {
  109.     while (tail)
  110.     {
  111.         std::cout << "Clear for: " << front() << '\n';
  112.         pop_front();
  113.     }
  114. }
  115.  
  116. template <typename ElemT>
  117. ListFw<ElemT>::~ListFw()
  118. {
  119.     while (head)
  120.     {
  121.         std::cout << "Destruct for: " << front() << '\n';
  122.         pop_front();
  123.     }
  124. }
  125.  
  126.  
  127. template <typename ElemT>
  128. class Stack
  129. {
  130.     ListFw<ElemT> storage;
  131. public:
  132.     Stack() : storage{} {};
  133.     bool empty() { return !storage.getSize(); };
  134.     size_t size() { return storage.getSize(); };
  135.     ElemT top() { return storage.front(); };
  136.     void push(const ElemT& elem) { storage.push_front(elem); };
  137.     void pop() { storage.pop_front(); };
  138. };
  139.  
  140. int main()
  141. {
  142.     Stack<int> stack;
  143.    
  144.     for (int i{ 0 }; i < 10; ++i)
  145.     {
  146.         stack.push(i);
  147.     };
  148.  
  149.     while (!stack.empty())
  150.     {
  151.         std::cout << stack.top() << '\n';
  152.         stack.pop();
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement