Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include "FifoElement.hpp"
  2.  
  3. template <typename T>
  4. class Fifo {
  5.     FifoElement<T>* head;
  6.     FifoElement<T>* tail;
  7.  
  8. public:
  9.     /*Fifo& operator<<(const T&) {
  10.         push(T);
  11.     };
  12.     Fifo& operator>>(const T&);
  13.     operator int() const;*/
  14.  
  15.  
  16.     Fifo() {
  17.         head = nullptr;
  18.         tail = nullptr;
  19.     }
  20.  
  21.     void push(T input) {
  22.         FifoElement<T>* element = new FifoElement<T>;
  23.         element->value = input;
  24.         element->next = nullptr;
  25.         if (head == nullptr) {
  26.             head = element;
  27.         }
  28.         if (tail) {
  29.             tail->next = element;
  30.         }
  31.         tail = element;
  32.     }
  33.  
  34.     T pop() {
  35.         if (!head) {
  36.             throw "Fifo Unterlauf";
  37.         }
  38.         if (tail == head) {
  39.             T tmp = tail->value;
  40.             tail = 0;
  41.             return  tmp;
  42.         }
  43.         FifoElement<T>* temp = head;
  44.         head = head->next;
  45.         T deleted = temp->value;
  46.         return deleted;
  47.     }
  48.  
  49.     void show() {
  50.         FifoElement<T>* temp = head;
  51.         do {
  52.             cout << temp->value << endl;
  53.             temp = temp->next;
  54.         } while (temp);
  55.     }
  56.  
  57. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement