Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- class kulka
- {
- public:
- int v1;
- int v2;
- kulka*next=NULL;
- kulka*right=NULL;
- kulka*left=NULL;
- kulka(int v)
- {
- v1=rand()%100;
- v2=v;
- }
- void addlist(kulka *nowa) {
- if (next) {
- if (nowa->v1 < next->v1) {
- nowa->next = next;
- next = nowa;
- }
- else next->addlist(nowa);
- }
- else next = nowa;
- }
- void addtree(kulka*nowa)
- { if(nowa->v1 < v1)
- {if (left) left->addtree(nowa);
- else left=nowa;
- } else
- { if(right) right->addtree(nowa);
- else right=nowa;
- }
- }
- void showlist()
- {
- cout<<v1<<" ";
- if(next) next->showlist();
- }
- void showtree()
- {
- if (right) right->showtree();
- cout<<v1<<" ";
- if(left) left-> showtree();
- }
- };
- int main()
- {
- kulka* nowa=NULL;
- kulka* lista=NULL;
- kulka* drzewo=NULL;
- bool flag=true;
- int licznik=0;
- while(flag)
- {
- nowa= new kulka(15);
- if (nowa->v1==nowa->v2) flag=false;
- if(nowa->v1>nowa->v2)
- { if(!lista) lista=nowa;
- else
- if(lista->v1>nowa->v1)
- {nowa->next=lista;
- lista=nowa;
- }else lista->addlist(nowa);
- }
- else
- {if(!drzewo) drzewo=nowa;
- else drzewo->addtree(nowa);
- }
- licznik++;
- }
- cout<<"Wykonano " << licznik << "losowań"<<endl;
- cout<<"LISTA"<<endl;
- lista->showlist();
- cout<<endl<<"DRZEWKO " <<endl;
- drzewo->showtree();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement