Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------STOS------------------------------------------
- struct stos{ int data; stos* next;};
- bool is_Empty(stos* st){
- if(st == nullptr) cout << " Stos jest pusty" << endl;
- else{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){
- 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;}
- 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;}}
- -------------------------------------KOLEJKA-----------------------------------------------
- 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)
- {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);}}
- ----------------------------------------LISTA JEDNOKIERUNKOWA----------------------------------------------
- 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){ 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 ++;}
- void add_head(lista &l, int r_d){
- 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 ++;}
- void add_position(lista &l, int r_d, int posit){ 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 ++;}
- void delete_tail(lista &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;}
- void delete_head(lista &l){if(l.tail == nullptr) return; element* temp = l.head; l.head = l.head -> next;
- delete temp; if(l.head == nullptr) l.tail = nullptr; l.el_count--;}
- void delete_position(lista &l, int posit){
- if (posit < 0 or posit > l.el_count){cout << "Podano niewlasciwa pozycje" << endl;return;}
- 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 --;}
- 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) {return;}
- element* temp = l.head;
- while(l.el_count != 0){c++; if (c == posit) cout << temp -> data << endl; temp = temp -> next; l.el_count --;}}
- ---------------------------------------------LISTA DWUKIERUNKOWA------------------------------------------
- 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){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);
- 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 ++;} 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){if (posit < 1 or posit > dl.el_count){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){ 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) {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){ 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);}}
- ------------------------------------------DRZEWO------------------------------------------------
- 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;
- 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);}}
- ---------------------------SORTOWANIE PRZEZ WYBIERANIE-----------------------------
- int find_max_el(int *arr, int n){int maxel = arr[0]; int imax = 0;
- for (int i =0; i<n; i++) if(arr[i] > maxel){ maxel= arr[i]; imax = i;} return imax;}
- void selectionsort(int *arr, int n){ int max_in; for(int i = n; i>1; i--){
- max_in=find_max_el(arr, i); swap(arr[i-1], arr[max_in]);}}
- -----------------BĄBELKOWE-------------------
- void bublesort(int *arr, int n){ for(int i = n-1; i>0; i-- ){ for( int j = 0; j<i; j++){
- if(arr[j]>arr[j+1]) swap(arr[j], arr[j+1]);}}}
- -----------------SORTOWANIE PRZEZ WSTAWIANIE--------------
- void insertionsort(int *arr, int n){ int temp, j; for(int i=n-2; i>=0; i--){ j = i; temp = arr[j];
- while(temp > arr[j+1] and j < n-1){ arr[j] = arr[j+1]; j++;} arr[j] = temp;}}
- ---------------FLAGA POLSKA----------------
- int flagapolska(struct student x[], short n){ int p=0, k=n-1; int ind;
- while(p<k) {while(p<n-1 && x[p].ocena>=10) p++; while(k>=0 && x[k].ocena < 10) k--;
- if(p<k){ swap(x[p],x[k]); ind = k;}
- if(x[p].ocena > 10) ind=p; else ind=p-1;} return k;}
- void wys (struct student* x, int p, int k)
- {for(int i=p; i<k; i++){ cout << "imie: " << x[i].imie << " ocena = " << x[i].ocena << endl;}}
- ------------FLAGA FFRANCUSKA---------------
- int flagafrancuska(struct student x[], short n, int* ko){int p=-1,s=0 ,k=n;
- while(s<k){if(x[s].ocena%3 == 0){p++; swap(x[p], x[s]); s++;}
- else{ if(x[s].ocena%3==2){ k--; swap(x[k], x[s]);} else s++;}}
- *ko=k; return p;}
- ------------WYSZUKIWANIE LINIOWE------------
- int linsearch(int *arr, int n, int klucz){ for(int i=0; i<n; i++){ if(arr[i]==klucz)
- return i;} return -1;}
- ---------BISEKCJA Z PĘTLA---------------
- int bis_p(int *arr, int n, int klucz){ int l=0; int p=n-1; int s;
- while(p>=l){ s=(l+p)/2; if(klucz>arr[s]) l=s+1; else{ p=s-1;} if(arr[s]==klucz){ return s;}}return -1;}
- ------------BISEKCJA REKURENCYJNA--------------
- int bis_rek(int *arr, int a, int b, int klucz){ int srodek=(a+b)/2;
- if(b>a){ if(arr[srodek] != klucz){ if(klucz < arr[srodek])
- return bis_rek(arr, a, srodek-1, klucz); else{ return bis_rek(arr, srodek+1, b, klucz);}}
- else{ return srodek;}} else { return -1; }}
- ---------------KMP-----------------
- int KMP (int n, int m, int *ind, string tekst, string wzorzec)
- {int *tab; tab = new int[m]; tab[0]=0; tab[1]=0; int t=0; int x=0;
- for(int i=2; i<=m; i++){ while(t>0 and wzorzec[t]!=wzorzec[i-1]) t=tab[t];
- if(wzorzec[t]==wzorzec[i-1])t++; tab[i]=t;}
- int i=1; int j=0; while(i<=n-m+1){ while(wzorzec[j]==tekst[i+j-1] and j<m) j++;
- if(j==m){ ind[x]=i-1; x++; j=m;} i=i+max(1,j-tab[j]); j=tab[j];} return x;}
- ---------NAIWNY---------------
- int naiwny(int n, int m, string tekst, string wzorzec, int *tab)
- {int x=-1, j=0; for(int i = 0; i < n-1; i++){
- if (wzorzec == tekst.substr(i,m)){tab[j]=i; j++;}} return j;}
Add Comment
Please, Sign In to add comment