Advertisement
Karolina99

FIFO queue - singly linked list

Nov 18th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Elem
  6. {
  7. private:
  8.     int v; //key, data
  9.     Elem* next_elem; //wskaznik na nastepny element
  10. public:
  11.     Elem(int x, Elem* n); //konstruktor, tworzy element przechowujacy x i posiadajacy wskaznik na nastepny element
  12.     int value(); //zwraca key
  13.     Elem* next(); //zwraca next_elem
  14.     void setNext(Elem* p); //ustawia next_elem
  15. };
  16.  
  17. Elem::Elem(int x, Elem* n)
  18. {
  19.     v = x;
  20.     next_elem = n;
  21. }
  22.  
  23. int Elem::value()
  24. {
  25.     return v;
  26. }
  27.  
  28. Elem* Elem::next()
  29. {
  30.     return next_elem;
  31. }
  32.  
  33. void Elem::setNext(Elem* p)
  34. {
  35.     next_elem = p;
  36. }
  37.  
  38. class LFIFO
  39. {
  40. private:
  41.     Elem* head; //wskaznik na poczatek kolejki
  42.     Elem* tail; //wskaznik na koniec kolejki
  43. public:
  44.     LFIFO(); //konstruktor, tworzy pusta kolejke
  45.     bool empty(); //zwraca true, jesli kolejka jest pusta, false w przeciwnym razie
  46.     void enqueue(int x); //dodaje element na koniec kolejki
  47.     int dequeue();      //usuwa i zwraca danΔ… z kolejki (usuwa z poczatku kolejki)
  48.     void clear();       //usuwa cala kolejke
  49.     friend ostream& operator<<(ostream& out, LFIFO& q); //wyswietla kolejke na ekranie konsoli
  50. };
  51.  
  52. LFIFO::LFIFO()
  53. {
  54.     head = tail = NULL;
  55. }
  56.  
  57. bool LFIFO::empty()
  58. {
  59.     if (head == NULL)
  60.         return true;
  61.     return false;
  62. }
  63.  
  64. void LFIFO::enqueue(int x)
  65. {
  66.     if (empty())
  67.     {
  68.         Elem* p = new Elem(x, NULL);
  69.         head = tail = p;
  70.     }
  71.     else
  72.     {
  73.         Elem* p = new Elem(x, NULL);
  74.         tail->setNext(p);
  75.         tail = p;
  76.     }
  77. }
  78.  
  79. int LFIFO::dequeue()
  80. {
  81.     if (empty())
  82.         return NULL;
  83.     else
  84.     {
  85.         Elem* p = head;
  86.         int wartosc = head->value();
  87.         head = head->next();
  88.         delete p;
  89.         return wartosc;
  90.     }
  91. }
  92.  
  93. void LFIFO::clear()
  94. {
  95.     int a = dequeue();
  96.     while (a != NULL)
  97.     {
  98.         dequeue();
  99.         a = dequeue();
  100.     }
  101. }
  102.  
  103. ostream& operator<<(ostream& out, LFIFO& q)
  104. {
  105.     Elem* temp = q.head;
  106.     while (temp != NULL)
  107.     {
  108.         out << temp->value() << " ";
  109.         temp = temp->next();
  110.     }
  111.     return out;
  112. }
  113.  
  114. int main()
  115. {
  116.     LFIFO kolejka = LFIFO();
  117.  
  118.     kolejka.enqueue(1);
  119.     kolejka.enqueue(2);
  120.     kolejka.enqueue(3);
  121.     kolejka.enqueue(4);
  122.     kolejka.enqueue(5);
  123.     kolejka.enqueue(6);
  124.     cout << kolejka << endl;
  125.  
  126.     kolejka.dequeue();
  127.     cout << kolejka << endl;
  128.  
  129.     kolejka.clear();
  130.     cout << kolejka;
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement