Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------STOS---------------------------------------------
- #include <iostream>
- #include <time.h>
- using namespace std;
- struct stos
- {
- int data;
- stos* next;
- };
- bool is_Empty(stos* st){
- if(st == nullptr) cout << " Stos jest pusty" << endl;
- else{
- cout << " W stosie cos jednak bylo" << endl;
- return st == nullptr;
- }
- }
- int srednia(stos* &st, int *ilosc)
- {
- stos* st_2 = nullptr;
- *ilosc=0;
- float suma =0;
- float sre=0;
- while(st != nullptr)
- {
- suma =suma+st->data;
- (*ilosc)++;
- push(st_2, st->data);
- st=st->next;
- }
- while(st_2 != nullptr)
- {
- push(st, st_2->data);
- st_2=st_2->next;
- }
- float x=static_cast<float>(*ilosc);
- sre=suma/x;
- return sre;
- }
- void push (stos* &st, int new_data){
- cout << new_data << endl;
- stos* dodana_wartosc = new stos;
- dodana_wartosc -> data = new_data;
- dodana_wartosc -> next = st;
- st = dodana_wartosc;
- }
- void pop(stos* &st){
- if(st != nullptr){
- stos* temp = st;
- st = st -> next;
- delete temp;
- cout << "Usuwanie powiodlo sie" << endl;
- }
- else
- cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl;
- }
- void take_element(stos* st){
- if (st == 0)
- cout << "stos jest pusty" << endl;
- else{
- cout << "na gorze stosu jest liczba :" << st -> data << endl;
- }
- }
- int main()
- {
- float sre;
- int *ilosc;
- ilosc= new int;
- int w;
- stos* st = nullptr;
- srand(time(NULL));
- while (w!=6){
- cout << " 1 - sprawdzenie czy stos jest pusty" << endl;
- cout << " 2 - dodanie elementu na stos" << endl;
- cout << " 3 - usuniecie ze stosu" << endl;
- cout << " 4 - pobranie elementu ze stosu" << endl;
- cout << " 5 - obliczyc srednia elementow" << endl;
- cout << " 6 - wyjscie z programu" << endl;
- cout << " Co chcesz zrobic?" << endl;
- cin >> w;
- switch (w)
- {
- case 1: is_Empty(st); break;
- case 2: push (st,rand()%20); break;
- case 3: pop(st); break;
- case 4: take_element(st); break;
- case 5: sre=srednia(st ,ilosc);
- cout<< "wartosc srednia wynosi :" << sre << endl;
- break;
- case 6: cout << "juz nic nie mozesz zrobic " << endl; break;
- default: cout << " Bledny wybor" << endl;
- }
- }
- return 0;
- }
- -------------------------------------KOLEJKA-----------------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element {
- int x;
- element* nast;
- };
- struct kolejka {
- element* poczatek;
- element* koniec;
- };
- kolejka stworz_kolejke ()
- {
- kolejka nowa_kolejka;
- nowa_kolejka.poczatek = nullptr;
- nowa_kolejka.koniec = nullptr;
- return nowa_kolejka;
- }
- void dodaj_element (kolejka &q, int rd)
- {
- cout << rd << endl;
- element* nowy = new element;
- nowy -> x = rd;
- nowy -> nast = nullptr;
- if(q.koniec != nullptr) q.koniec -> nast = nowy;
- q.koniec=nowy;
- if(q.poczatek == nullptr) q.poczatek = nowy;
- }
- void usun_elem_pocz (kolejka &q)
- {
- if (q.poczatek == nullptr) return;
- element* temp = q.poczatek;
- q.poczatek = q.poczatek -> nast;
- delete temp;
- if(q.poczatek == nullptr) q.koniec == nullptr;
- }
- void wyswietl_kolejke(kolejka &q)
- {
- while (q.poczatek != nullptr)
- {
- cout << q.poczatek -> x << endl;
- usun_elem_pocz(q);
- }
- }
- int main()
- {
- kolejka q;
- int wybor;
- srand(time(NULL));
- do
- {
- cout << "co zrobic?" << endl;
- cout << "1. Stworz nowa, pusta kolejke" << endl;
- cout << "2. Dodaj losowy element do kolejki" << endl;
- cout << "3. Usuna element z kolejki" << endl;
- cout << "4. wyswietl i usun cala kolejke" << endl;
- cout << "5. Wyjscie" << endl;
- cin >> wybor;
- switch(wybor){
- case 1: q=stworz_kolejke(); break;
- case 2: dodaj_element (q, rand()); break;
- case 3: usun_elem_pocz (q); break;
- case 4: wyswietl_kolejke(q); break;
- case 5: break;
- default : cout << "bledny wybor" << endl; break;
- }
- }
- while (wybor != 5);
- return 0;
- }
- ----------------------------------------LISTA JEDNOKIERUNKOWA----------------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element {
- int data;
- element* next;
- };
- struct lista {
- element* head;
- element* tail;
- int el_count;
- };
- lista create_list (){
- lista new_list;
- new_list.head = nullptr;
- new_list.tail = nullptr;
- new_list.el_count = 0;
- return new_list;}
- void add_tail(lista &l, int r_d){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- element* n_tail = new element;
- n_tail -> data = r_d;
- n_tail -> next = nullptr;
- if(l.tail != nullptr)
- l.tail -> next = n_tail;
- else
- l.head = n_tail;
- l.tail = n_tail;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void add_head(lista &l, int r_d){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- element* n_head = new element;
- n_head -> data = r_d;
- n_head -> next = l.head;
- l.head = n_head;
- if(l.tail == nullptr)
- l.tail == n_head;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void add_position(lista &l, int r_d, int posit){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- cout << "Numer ktory zostanie dodany: " << r_d << endl;
- if(posit <0 or posit > l.el_count) return;
- if(posit == 0)
- {
- add_head(l, r_d);
- return;
- }
- if(posit == l.el_count)
- {
- add_tail(l, r_d);
- return;
- }
- element* temp = l.head;
- for(int i=0; i < posit -1; i++)
- temp = temp -> next;
- element* n_e = new element;
- n_e -> data = r_d;
- n_e -> next = temp -> next;
- temp -> next = n_e;
- l.el_count ++;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_tail(lista &l){
- cout << "Lista biezaca:" << endl;
- show_list(l);
- if(l.tail == nullptr) return;
- element* temp = l.tail;
- if(l.el_count == 1)
- {
- l.tail == nullptr;
- l.head == nullptr;
- }
- else{
- element * tempor = l.head;
- for (int i =1; i< l.el_count -1; i++)
- {
- tempor = tempor -> next;
- }
- l.tail = tempor;
- l.tail -> next = nullptr;
- }
- l.el_count --;
- delete temp;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_head(lista &l){
- if(l.tail == nullptr) return;
- cout << "Lista biezaca:" << endl;
- show_list(l);
- element* temp = l.head;
- l.head = l.head -> next;
- delete temp;
- if(l.head == nullptr)
- l.tail = nullptr;
- cout << "Element z poczatku zostal usuniety" << endl;
- l.el_count--;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_position(lista &l, int posit){
- if (posit < 0 or posit > l.el_count)
- {
- cout << "Podano niewlasciwa pozycje" << endl;
- return;
- }
- cout << "Lista biezaca:" << endl;
- show_list(l);
- if (posit == 0)
- {
- delete_head(l);
- return;
- }
- if(posit == l.el_count)
- {
- delete_tail(l);
- return;
- }
- element* temp = l.head;
- for (int i =1; i< l.el_count -1; i++)
- {
- temp = temp -> next;
- }
- element* temp_delete = temp -> next;
- temp -> next = temp_delete -> next;
- delete temp_delete;
- l.el_count --;
- cout << "Lista po zmianie:" << endl;
- show_list(l);
- }
- void delete_list(lista &l){
- while(l.head != nullptr)
- {
- delete_head(l);
- }
- }
- void show_list(lista l){
- element* temp = l.head;
- while (temp)
- {
- cout << temp -> data << endl;
- temp = temp -> next;
- }
- }
- int parzyste(lista l)
- {
- int i=0;
- element* temp = l.head;
- while (temp)
- {
- if(temp->data%2==0) i++;
- temp = temp -> next;
- }
- return i;
- }
- void show_on_position(lista l, int posit)
- {
- int c = 0;
- if (posit < 0 or posit > l.el_count)
- {
- cout << "Podano niewlasciwa pozycje" << endl;
- return;
- }
- element* temp = l.head;
- while(l.el_count != 0){
- c++;
- if (c == posit) cout << temp -> data << endl;
- temp = temp -> next;
- l.el_count --;
- }
- }
- int main()
- {
- lista l = create_list();
- int wybor, posit, x;
- cout << "Podaj pozycje" << endl;
- cin >> posit;
- srand(time(NULL));
- do{
- cout << "Podaj wybor:" << endl;
- cout << "1. Dodawanie elementu o losowej wartosci na koniec listy " << endl;
- cout << "2. Dodawanie elementu o losowej wartosci na poczatek listy " << endl;
- cout << "3. Dodawanie elementu o losowej wartosci na pozycje o podanym numerze " << endl;
- cout << "4. Usuniecie elementu z konca listy " << endl;
- cout << "5. Usuniecie elementu z poczatku listy" << endl;
- cout << "6. Usuniecie elementu z podanej pozycji" << endl;
- cout << "7. Wyswietlenie elementu o podanym numerze" << endl;
- cout << "8. Wyswietlenie calej listy" << endl;
- cout << "9. Usuniecie wszystkich elementow listy wraz ze zwolnieniem pamieci" << endl;
- cout << "10. Ilosc przystych" << endl;
- cout << "11. Wyjscie" << endl;
- cout << "Odpowiedz:" << endl;
- cin >> wybor;
- switch(wybor){
- case 1: add_tail(l, rand()%10); break;
- case 2: add_head(l, rand()); break;
- case 3: add_position(l, rand(), posit); break;
- case 4: delete_tail(l); break;
- case 5: delete_head(l); break;
- case 6: delete_position(l, posit); break;
- case 7: show_on_position(l, posit); break;
- case 8: show_list(l); break;
- case 9: delete_list(l); break;
- case 10:
- x=parzyste(l);
- cout << "liczba przystych : " << x <<endl; break;
- case 11: cout << "Wyszedles z progrmau" << endl; break;
- default : cout << "bledny wybor" << endl; break;
- }
- } while (wybor != 11);
- return 0;
- }
- ---------------------------------------------LISTA DWUKIERUNKOWA------------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- struct element {
- int data;
- element* next;
- element* prev;
- };
- struct dlist {
- element* head;
- element* tail;
- int el_count;
- };
- dlist create_empty_dlist(){
- dlist dl;
- dl.head = nullptr;
- dl.tail = nullptr;
- dl.el_count = 0;
- return dl;
- }
- element* create_element(int n_d){ // nie dodaje, tworzy element
- element* n_e = new element;
- n_e -> data = n_d;
- n_e -> next = nullptr;
- n_e -> prev = nullptr;
- return n_e;
- }
- void add_tail(int n_d, dlist &dl){
- element* n_e = create_element(n_d);
- cout << "Numer ktory zostanie dodany: " << n_d << endl;
- if (dl.el_count == 0)
- dl.head = n_e;
- else{
- dl.tail -> next = n_e;
- n_e -> prev = dl.tail;
- }
- dl.tail = n_e;
- dl.el_count ++;
- }
- void add_head(int n_d, dlist &dl){
- element* n_e = create_element(n_d);
- cout << "Numer ktory zostanie dodany: " << n_d << endl;
- if (dl.el_count == 0)
- dl.tail = n_e;
- else{
- dl.head -> prev = n_e;
- n_e -> next = dl.head;
- }
- dl.head = n_e;
- dl.el_count ++;
- }
- element* find_position(dlist dl, int posit){
- if (posit < 0 or posit > dl.el_count-1)
- return nullptr;
- element* temp = dl.head;
- for (int i =0; i < posit; i++)
- temp = temp -> next;
- return temp;
- }
- void add_position(int n_d, dlist &dl, int posit){
- cout << "Numer ktory zostanie dodany: " << n_d << endl;
- if (posit < 1 or posit > dl.el_count){
- cout << " Niewlasciwa pozycja" << endl;
- return;
- }
- if (posit == 1)
- {
- add_head(n_d, dl);
- return;
- }
- if( posit == dl.el_count)
- {
- add_tail(n_d, dl);
- return;
- }
- element* temp = dl.head;
- for(int i=0; i < posit -1; i++)
- temp = temp -> next;
- element* n_e = new element;
- n_e -> data = n_d;
- n_e -> next = temp -> next;
- temp -> next = n_e;
- dl.el_count ++;
- }
- void delete_head(dlist &dl){
- if (dl.el_count == 0)
- {
- cout << "Dwulista jest pusta" << endl;
- return;
- }
- else{
- element* temp = dl.head;
- if(dl.el_count == 1)
- {
- dl.head = nullptr;
- dl.tail = nullptr;
- }
- else
- {
- dl.head = dl.head -> next;
- dl.head -> prev = nullptr;
- }
- delete temp;
- dl.el_count --;
- }
- }
- void delete_tail(dlist &dl){
- if (dl.el_count == 0)
- {
- cout << " Dwulista jest pusta " << endl;
- return;
- }
- else{
- element* temp = dl.tail;
- if (dl.el_count == 1)
- {
- dl.tail = nullptr;
- dl.head = nullptr;
- }
- else
- {
- dl.tail = dl.tail -> prev;
- dl.tail -> next = nullptr;
- }
- delete temp;
- dl.el_count --;
- }
- }
- void delete_position(dlist &dl, int posit){
- if (posit < 1 or posit > dl.el_count)
- {
- cout << "Podano niewlasciwa pozycje" << endl;
- return;
- }
- if (posit == 1)
- {
- delete_head(dl);
- return;
- }
- if(posit == dl.el_count)
- {
- delete_tail(dl);
- return;
- }
- element* temp = dl.head;
- for (int i =1; i< posit -1; i++)
- {
- temp = temp -> next;
- }
- element* temp_delete = temp -> next;
- temp -> next = temp_delete -> next;
- delete temp_delete;
- dl.el_count --;
- }
- void show_list(dlist dl){
- element* temp = dl.head;
- while (temp)
- {
- cout << temp -> data << endl;
- temp = temp -> next;
- }
- }
- void delete_list(dlist &dl){
- while(dl.head != nullptr){
- delete_head(dl);
- }
- cout << "Lista zostala usunieta" << endl;
- }
- void show_reversed(dlist dl){
- element* temp = dl.tail;
- while (temp)
- {
- cout << temp -> data << endl;
- temp = temp -> prev;
- }
- }
- using namespace std;
- int main()
- {
- dlist dl = create_empty_dlist();
- int wybor, posit;
- cout << "Podaj pozycje:" << endl;
- cin >> posit;
- srand(time(NULL));
- do{
- cout << "Co chcesz zrobic?" << endl;
- cout << "1. Dodac element na koniec listy" << endl;
- cout << "2. Dodac element na poczatek listy" << endl;
- cout << "3. Dodac element na pozycje" << endl;
- cout << "4. Usunac z konca" << endl;
- cout << "5. Usunac z poczatku" << endl;
- cout << "6. Usunac z pozycji" << endl;
- cout << "7. Usunac calosc" << endl;
- cout << "8. Wyswietlic calosc" << endl;
- cout << "9. Wyswietlic calosc od konca" << endl;
- cout << "10. Juz nic nie chcesz" << endl;
- cin >> wybor;
- switch(wybor){
- case 1: add_tail( rand(),dl); break;
- case 2: add_head( rand(),dl); break;
- case 3: add_position( rand(), dl, posit); break;
- case 4: delete_tail(dl); break;
- case 5: delete_head(dl); break;
- case 6: delete_position(dl, posit); break;
- case 7: delete_list(dl); break;
- case 8: show_list(dl); break;
- case 9: show_reversed(dl); break;
- case 10: cout << " Zakonczyles program"; break;
- default : cout <<" Zly numer, sprobuj jeszcze raz" << endl;
- }
- }
- while (wybor != 10);
- return 0;
- }
- -----------------------------------------------LISTA JEDNOKIERUNKOWA CYKLICZNA---------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element
- {
- int x;
- element* nast;
- };
- struct lista_cykliczna
- {
- element* biezacy;
- };
- lista_cykliczna stworz_liste ()
- {
- lista_cykliczna nowa_lista;
- nowa_lista.biezacy = nullptr;
- return nowa_lista;
- }
- void dodaj_element (lista_cykliczna &q, int rd)
- {
- cout << rd << endl;
- element* nowy = new element;
- nowy -> x = rd;
- if(q.biezacy != nullptr)
- {
- nowy -> nast = q.biezacy -> nast;
- q.biezacy -> nast = nowy;
- }
- else nowy -> nast = nowy;
- q.biezacy = nowy;
- }
- void dodaj_element_za_element (lista_cykliczna &q, int istniejacy, int wstaw)
- {
- short kontrolka=0;
- element* nowy = new element;
- nowy -> x = wstaw;
- element* szukany = new element;
- szukany -> x = istniejacy;
- element* p = new element;
- p = q.biezacy;
- if(p!=nullptr)
- do
- {
- if(p -> x == szukany -> x)
- {
- nowy -> nast = p -> nast;
- p -> nast = nowy;
- kontrolka=1;
- }
- p = p -> nast;
- }
- while(p != q.biezacy);
- else
- {
- nowy -> nast = szukany;
- q.biezacy = nowy;
- szukany -> nast = nowy;
- kontrolka=1;
- }
- if(kontrolka==0)
- {
- nowy -> nast = q.biezacy -> nast;
- q.biezacy -> nast = nowy;
- szukany -> nast = q.biezacy -> nast;
- q.biezacy -> nast = szukany;
- }
- }
- void wyswietl(lista_cykliczna &q)
- {
- element* nowy;
- nowy = q.biezacy;
- if(q.biezacy!=nullptr)
- do
- {
- cout << nowy->x << endl;
- nowy = nowy->nast;
- }
- while(nowy != q.biezacy);
- }
- void szukanko(lista_cykliczna &q, int istniejacy)
- {
- int kontrolka=0;
- element* szukany = new element;
- szukany -> x = istniejacy;
- element* p = new element;
- p = q.biezacy;
- if(p!=nullptr)
- do
- {
- if(p -> x == szukany -> x)
- {
- cout << "Szukany element\n" << "Dane: " << p -> x << "\nAdres nastepnego elementu: " << p -> nast << "\nWartosc nastepnego elementu: " << p -> nast -> x << endl;
- kontrolka=1;
- }
- p = p -> nast;
- }
- while(p != q.biezacy);
- if(kontrolka==0) cout << "Elementu nie ma w kolejce lub kolejka jest pusta" << endl;
- }
- int main()
- {
- lista_cykliczna q;
- int wybor;
- int istniejacy, wstaw, istniejacy_2;
- srand(time(NULL));
- do
- {
- cout << "Co zrobic?" << endl;
- cout << "1. Stworz nowa, pusta liste" << endl;
- cout << "2. Dodaj losowy element do listy" << endl;
- cout << "3. Dodaj nowy element za istniejacy element" << endl;
- cout << "4. Wyszukaj element" << endl;
- cout << "5. Wyswietl liste" << endl;
- cout << "6. Wyjscie" << endl;
- cin >> wybor;
- cout << endl << endl;
- switch(wybor){
- case 1: q=stworz_liste(); break;
- case 2: dodaj_element (q, rand()); break;
- case 3:
- {
- cout << "Podaj element istniejacy" << endl;
- cin >> istniejacy;
- cout << endl << endl;
- cout << "Podaj nowy elemeny" << endl;
- cin >> wstaw;
- cout << endl << endl;
- dodaj_element_za_element (q, istniejacy, wstaw);
- break;
- }
- case 4:
- {
- cout << "Podaj element szukany" << endl;
- cin >> istniejacy_2;
- cout << endl << endl;
- szukanko(q, istniejacy_2);
- break;
- }
- case 5: wyswietl(q); break;
- case 6: break;
- default : cout << "bledny wybor" << endl; break;
- }
- }
- while (wybor != 6);
- return 0;
- }
- ----------------------------------------------LISTA DWUKIERUNKOWA CYKLICZNA-------------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element
- {
- int data;
- element* next;
- element* prev;
- };
- struct dlist
- {
- element* current;
- int el_count;
- };
- dlist create_empty_dlist()
- {
- dlist dl;
- dl.current = nullptr;
- dl.el_count = 0;
- return dl;
- }
- element* create_element(int n_d)
- { // nie dodaje, tworzy element
- element* n_e = new element;
- n_e -> data = n_d;
- n_e -> next = nullptr;
- n_e -> prev = nullptr;
- return n_e;
- }
- void add_new(int n_d, dlist &dl)
- {
- element* n_e = create_element(n_d);
- cout << "Numer ktory zostanie dodany: " << n_d << endl;
- if (dl.current == nullptr)
- {
- n_e -> next = n_e;
- n_e -> prev = n_e;
- }
- else
- {
- n_e -> prev = dl.current;
- n_e -> next = dl.current ->next;
- dl.current ->next ->prev = n_e;
- dl.current -> next = n_e;
- }
- dl.current = n_e;
- dl.el_count ++;
- }
- void delete_element(dlist &dl, int value)
- {
- bool kontrolka = false;
- if (dl.el_count == 0)
- {
- cout << "Dwulista jest pusta" << endl;
- return;
- }
- else
- {
- element* temp = dl.current;
- if(temp != nullptr)
- do
- {
- if(temp -> data == value)
- {
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- kontrolka=true;
- }
- temp = temp -> next;
- }
- while(temp != dl.current and kontrolka == false);
- delete temp;
- dl.el_count --;
- }
- if(kontrolka==false) cout << "Elementu nie ma w liscie" << endl;
- }
- void show_list(dlist &dl)
- {
- element* temp = dl.current;
- //for(int i = 0; i < dl.el_count; i++)
- if(dl.current != nullptr)
- do
- {
- cout << temp -> data << endl;
- temp = temp -> prev;
- }
- while(temp != dl.current);
- }
- int main()
- {
- dlist dl = create_empty_dlist();
- int wybor, value;
- srand(time(NULL));
- do{
- cout << "Co chcesz zrobic?" << endl;
- cout << "1. Dodac element" << endl;
- cout << "2. Usunac element o podanej wartosci" << endl;
- cout << "3. Wyswietlic od prawej do lewej" << endl;
- cout << "4. Juz nic nie chcesz" << endl;
- cin >> wybor;
- switch(wybor)
- {
- case 1: add_new(rand()%50, dl); break;
- case 2:
- cout << "Podaj wartosc" << endl;
- cin >> value;
- delete_element(dl, value);
- break;
- case 3: show_list(dl); break;
- case 4: cout << " Zakonczyles program"; break;
- default : cout <<" Zly numer, sproboj jeszcze raz" << endl;
- }
- }
- while (wybor != 4);
- return 0;
- }
- ------------------------------------------DRZEWO------------------------------------------------
- #include <iostream>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct element
- {
- int x;
- element* lewy;
- element* prawy;
- element* gora;
- };
- struct drzewo
- {
- element* r;
- };
- drzewo stworz_drzewo ()
- {
- drzewo nowe_drzewo;
- nowe_drzewo.r = nullptr;
- return nowe_drzewo;
- }
- void dodaj_element (drzewo &root, int rd)
- {
- element *p;
- element *nowy_element = new element;
- nowy_element -> gora = nullptr;
- nowy_element -> lewy = nullptr;
- nowy_element -> prawy = nullptr;
- nowy_element -> x = rd;
- cout << rd << endl;
- p = root.r;
- if(!p) root.r = nowy_element;
- else
- {
- while (1)
- {
- if (rd < p -> x)
- {
- if (!p -> lewy)
- {
- p -> lewy = nowy_element;
- break;
- }
- else p = p -> lewy;
- }
- else
- {
- if (!p -> prawy)
- {
- p -> prawy = nowy_element;
- break;
- }
- else p = p -> prawy;
- }
- }
- nowy_element -> gora = p;
- }
- }
- int wys_drzewo(element *root)
- {
- if(root==nullptr) return 0;
- wys_drzewo(root->lewy);
- cout<< root->x <<" ";
- wys_drzewo(root->prawy);
- }
- bool czy_istnieje(element *root, int szukana)
- {
- if(root==nullptr) return false;
- if(root->x==szukana) return true;
- else
- {
- if (root->x < szukana) czy_istnieje(root->prawy, szukana);
- else czy_istnieje(root->lewy, szukana);
- }
- }
- int main()
- {
- drzewo d;
- int wybor, szukana;
- bool istnieje;
- d=stworz_drzewo();
- srand(time(NULL));
- do
- {
- cout << "Co zrobic?" << endl;
- cout << "1. Dodaj losowy element do drzewa" << endl;
- cout << "2. Wyswietl drzewo " << endl;;
- cout << "3. Sprawdz czy element istnieje w drzewie" << endl;
- cout << "4. Wyjscie" << endl;
- cin >> wybor;
- cout << endl << endl;
- switch(wybor){
- case 1: dodaj_element (d, rand()); break;
- case 2: wys_drzewo(d.r); break;
- case 3:
- {
- cout << "Podaj szukana" << endl;
- cin >> szukana;
- istnieje=czy_istnieje(d.r, szukana);
- if(istnieje==true)cout << "element istnieje" << endl;
- else cout << "element nie istnieje" << endl;
- break;
- }
- case 4: break;
- default : cout << "bledny wybor" << endl; break;
- }
- }
- while (wybor != 4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment