Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Stworzyć klasę(ewentualnie odpowiednią strukturę i funkcje do jej obsługi) implementującą
- kolejkę priorytetową w postaci kopca Fibonacciego
- – klasa powinna zawierać:
- definicje odpowiednich struktur danych i metod do ich obsługi.
- Napisać prosty program pokazujący użycie kolejki priorytetowej zrobionej z wykorzystaniem
- stworzonej klasy – kilka wywołań pokazujących jak kolejka powstaje i jak wykonywane na niej są
- kolejne operacje.
- */
- #include <iostream>
- using namespace std;
- struct node {
- int n;
- int degree;
- node* parent;
- node* child;
- node* left;
- node* right;
- char mark;
- char C;
- };
- class queue
- {
- public:
- node* CreateQueue();
- node* Create_node(int);
- node* Insert(node*, node*);
- int Display(node*);
- node* Find(node*, int);
- private:
- int nH;
- node* H;
- };
- node* queue::CreateQueue() {
- node* np;
- np = nullptr;
- return np;
- }
- node* queue::Create_node(int value) {
- node* x = new node;
- x->n = value;
- return x;
- }
- node* queue::Insert(node* H, node* x) {
- x->degree = 0;
- x->parent = nullptr;
- x->child = nullptr;
- x->left = x;
- x->right = x;
- x->mark = 'F';
- x->C = 'N';
- if (H != nullptr) {
- (H->left)->right = x;
- x->right = H;
- x->left = H->left;
- H->left = x;
- if (x->n < H->n)
- H = x;
- }
- else {
- H = x;
- }
- nH = nH + 1;
- return H;
- }
- int queue::Display(node* H) {
- node* p = H;
- if (p == nullptr) {
- cout << "Empty Heap" << endl;
- return 0;
- }
- cout << "Root Nodes: " << endl;
- do {
- cout << p->n;
- p = p->right;
- if (p != H) {
- cout << "-->";
- }
- } while (p != H && p->right != nullptr);
- cout << endl;
- }
- node* queue::Find(node* H, int k) {
- node* x = H;
- x->C = 'Y';
- node* p = nullptr;
- if (x->n == k) {
- p = x;
- x->C = 'N';
- return p;
- }
- if (p == nullptr) {
- if (x->child != nullptr)
- p = Find(x->child, k);
- if ((x->right)->C != 'Y')
- p = Find(x->right, k);
- }
- x->C = 'N';
- return p;
- }
- int main()
- {
- queue queue;
- node* H;
- node* p;
- //Tworzenie kolejki
- H = queue.CreateQueue();
- //Dodawanie węzła
- p = queue.Create_node(6);
- //Wstawianie węzła do kolejki
- H = queue.Insert(H,p);
- //kolejne implementacje w celu wypełnienia
- p = queue.Create_node(5);
- H = queue.Insert(H, p);
- p = queue.Create_node(4);
- H = queue.Insert(H, p);
- p = queue.Create_node(3);
- H = queue.Insert(H, p);
- p = queue.Create_node(2);
- H = queue.Insert(H, p);
- p = queue.Create_node(1);
- H = queue.Insert(H, p);
- p = queue.Create_node(9);
- H = queue.Insert(H, p);
- p = queue.Create_node(15);
- H = queue.Insert(H, p);
- //Wyświetlenie kolejki
- queue.Display(H);
- cout <<"Sprawdz czy znaleziono liczbe " << endl;
- if (queue.Find(H, 3)) cout << "Znaleziono liczbe" << endl;
- else cout << "Nie znaleziono liczby" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement