Advertisement
RynkunPokemon

sdfsdfsfdsfs

Feb 11th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. /*Stworzyć klasę(ewentualnie odpowiednią strukturę i funkcje do jej obsługi) implementującą
  2. kolejkę priorytetową w postaci kopca Fibonacciego
  3.  
  4. – klasa powinna zawierać:
  5.  
  6. definicje odpowiednich struktur danych i metod do ich obsługi.
  7.  
  8.  
  9. Napisać prosty program pokazujący użycie kolejki priorytetowej zrobionej z wykorzystaniem
  10. stworzonej klasy – kilka wywołań pokazujących jak kolejka powstaje i jak wykonywane na niej są
  11. kolejne operacje.
  12. */
  13. #include <iostream>
  14.  
  15. using namespace std;
  16. struct node {
  17.     int n;
  18.     int degree;
  19.     node* parent;
  20.     node* child;
  21.     node* left;
  22.     node* right;
  23.     char mark;
  24.     char C;
  25. };
  26. class queue
  27. {
  28.  
  29.  
  30. public:
  31.     node* CreateQueue();
  32.     node* Create_node(int);
  33.     node* Insert(node*, node*);
  34.     int Display(node*);
  35.     node* Find(node*, int);
  36. private:
  37.     int nH;
  38.     node* H;
  39. };
  40. node* queue::CreateQueue() {
  41.     node* np;
  42.     np = nullptr;
  43.     return np;
  44. }
  45. node* queue::Create_node(int value) {
  46.     node* x = new node;
  47.     x->n = value;
  48.     return x;
  49. }
  50.  
  51. node* queue::Insert(node* H, node* x) {
  52.     x->degree = 0;
  53.     x->parent = nullptr;
  54.     x->child = nullptr;
  55.     x->left = x;
  56.     x->right = x;
  57.     x->mark = 'F';
  58.     x->C = 'N';
  59.     if (H != nullptr) {
  60.         (H->left)->right = x;
  61.         x->right = H;
  62.         x->left = H->left;
  63.         H->left = x;
  64.         if (x->n < H->n)
  65.             H = x;
  66.     }
  67.     else {
  68.         H = x;
  69.     }
  70.     nH = nH + 1;
  71.     return H;
  72. }
  73. int queue::Display(node* H) {
  74.     node* p = H;
  75.     if (p == nullptr) {
  76.         cout << "Empty Heap" << endl;
  77.         return 0;
  78.     }
  79.     cout << "Root Nodes: " << endl;
  80.  
  81.     do {
  82.         cout << p->n;
  83.         p = p->right;
  84.         if (p != H) {
  85.             cout << "-->";
  86.         }
  87.     } while (p != H && p->right != nullptr);
  88.     cout << endl;
  89. }
  90. node* queue::Find(node* H, int k) {
  91.     node* x = H;
  92.     x->C = 'Y';
  93.     node* p = nullptr;
  94.     if (x->n == k) {
  95.         p = x;
  96.         x->C = 'N';
  97.         return p;
  98.     }
  99.  
  100.     if (p == nullptr) {
  101.         if (x->child != nullptr)
  102.             p = Find(x->child, k);
  103.         if ((x->right)->C != 'Y')
  104.             p = Find(x->right, k);
  105.     }
  106.  
  107.     x->C = 'N';
  108.     return p;
  109. }
  110.  
  111.  
  112. int main()
  113. {
  114.     queue queue;
  115.     node* H;
  116.     node* p;
  117.  
  118.  
  119.     //Tworzenie kolejki
  120.     H = queue.CreateQueue();
  121.     //Dodawanie węzła
  122.     p = queue.Create_node(6);
  123.     //Wstawianie węzła do kolejki
  124.     H = queue.Insert(H,p);
  125.  
  126.  
  127.  
  128.     //kolejne implementacje w celu wypełnienia
  129.     p = queue.Create_node(5);
  130.     H = queue.Insert(H, p);
  131.     p = queue.Create_node(4);
  132.     H = queue.Insert(H, p);
  133.     p = queue.Create_node(3);
  134.     H = queue.Insert(H, p);
  135.     p = queue.Create_node(2);
  136.     H = queue.Insert(H, p);
  137.     p = queue.Create_node(1);
  138.     H = queue.Insert(H, p);
  139.     p = queue.Create_node(9);
  140.     H = queue.Insert(H, p);
  141.     p = queue.Create_node(15);
  142.     H = queue.Insert(H, p);
  143.  
  144.  
  145.     //Wyświetlenie kolejki
  146.     queue.Display(H);
  147.  
  148.  
  149.  
  150.     cout <<"Sprawdz czy znaleziono liczbe   " << endl;
  151.     if (queue.Find(H, 3)) cout << "Znaleziono liczbe" << endl;
  152.     else cout << "Nie znaleziono liczby" << endl;
  153.  
  154.  
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement