SHOW:
|
|
- or go back to the newest paste.
| 1 | - | ------------------------------------STOS--------------------------------------------- |
| 1 | + | ------------------------------------STOS------------------------------------------ |
| 2 | struct stos{ int data; stos* next;};
| |
| 3 | - | #include <iostream> |
| 3 | + | |
| 4 | - | #include <time.h> |
| 4 | + | |
| 5 | else{return st == nullptr;}}
| |
| 6 | - | using namespace std; |
| 6 | + | |
| 7 | {stos* st_2 = nullptr;
| |
| 8 | - | struct stos |
| 8 | + | *ilosc=0;float suma =0; float sre=0; |
| 9 | - | {
|
| 9 | + | |
| 10 | - | int data; |
| 10 | + | {suma =suma+st->data; (*ilosc)++; push(st_2, st->data); st=st->next;}
|
| 11 | - | stos* next; |
| 11 | + | |
| 12 | - | }; |
| 12 | + | {push(st, st_2->data); st_2=st_2->next;}
|
| 13 | float x=static_cast<float>(*ilosc); | |
| 14 | sre=suma/x;return sre;} | |
| 15 | void push (stos* &st, int new_data){
| |
| 16 | stos* dodana_wartosc = new stos; dodana_wartosc -> data = new_data; | |
| 17 | - | else{
|
| 17 | + | dodana_wartosc -> next = st; st = dodana_wartosc;} |
| 18 | - | cout << " W stosie cos jednak bylo" << endl; |
| 18 | + | void pop(stos* &st){if(st != nullptr){
|
| 19 | - | return st == nullptr; |
| 19 | + | stos* temp = st; st = st -> next; delete temp;} |
| 20 | - | } |
| 20 | + | else cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl; } |
| 21 | - | } |
| 21 | + | |
| 22 | if (st == 0) cout << "stos jest pusty" << endl; | |
| 23 | - | {
|
| 23 | + | else{ cout << "na gorze stosu jest liczba :" << st -> data << endl;}}
|
| 24 | - | stos* st_2 = nullptr; |
| 24 | + | |
| 25 | - | *ilosc=0; |
| 25 | + | |
| 26 | - | float suma =0; |
| 26 | + | struct element {int x; element* nast;};
|
| 27 | - | float sre=0; |
| 27 | + | struct kolejka {element* poczatek; element* koniec;};
|
| 28 | kolejka stworz_kolejke () | |
| 29 | - | {
|
| 29 | + | {kolejka nowa_kolejka; nowa_kolejka.poczatek = nullptr;
|
| 30 | - | suma =suma+st->data; |
| 30 | + | nowa_kolejka.koniec = nullptr; return nowa_kolejka;} |
| 31 | - | (*ilosc)++; |
| 31 | + | |
| 32 | - | push(st_2, st->data); |
| 32 | + | {element* nowy = new element; nowy -> x = rd; nowy -> nast = nullptr;
|
| 33 | - | st=st->next; |
| 33 | + | if(q.koniec != nullptr) q.koniec -> nast = nowy; q.koniec=nowy; |
| 34 | - | } |
| 34 | + | if(q.poczatek == nullptr) q.poczatek = nowy;} |
| 35 | void usun_elem_pocz (kolejka &q) | |
| 36 | - | {
|
| 36 | + | {if (q.poczatek == nullptr) return; element* temp = q.poczatek;
|
| 37 | - | push(st, st_2->data); |
| 37 | + | q.poczatek = q.poczatek -> nast; delete temp; |
| 38 | - | st_2=st_2->next; |
| 38 | + | if(q.poczatek == nullptr) q.koniec == nullptr;} |
| 39 | - | } |
| 39 | + | |
| 40 | {while (q.poczatek != nullptr)
| |
| 41 | - | sre=suma/x; |
| 41 | + | {cout << q.poczatek -> x << endl; usun_elem_pocz(q);}}
|
| 42 | - | return sre; |
| 42 | + | |
| 43 | - | } |
| 43 | + | |
| 44 | struct element {int data; element* next;};
| |
| 45 | - | cout << new_data << endl; |
| 45 | + | struct lista {element* head; element* tail; int el_count;};
|
| 46 | - | stos* dodana_wartosc = new stos; |
| 46 | + | lista create_list (){lista new_list; new_list.head = nullptr; new_list.tail = nullptr;
|
| 47 | - | dodana_wartosc -> data = new_data; |
| 47 | + | new_list.el_count = 0; return new_list;} |
| 48 | - | dodana_wartosc -> next = st; |
| 48 | + | void add_tail(lista &l, int r_d){ element* n_tail = new element; n_tail -> data = r_d; n_tail -> next = nullptr;
|
| 49 | - | st = dodana_wartosc; |
| 49 | + | if(l.tail != nullptr)l.tail -> next = n_tail; |
| 50 | - | } |
| 50 | + | else l.head = n_tail; l.tail = n_tail; l.el_count ++;} |
| 51 | - | void pop(stos* &st){
|
| 51 | + | |
| 52 | - | if(st != nullptr){
|
| 52 | + | element* n_head = new element; n_head -> data = r_d; n_head -> next = l.head; l.head = n_head; |
| 53 | - | stos* temp = st; |
| 53 | + | if(l.tail == nullptr) l.tail == n_head; l.el_count ++;} |
| 54 | - | st = st -> next; |
| 54 | + | |
| 55 | - | delete temp; |
| 55 | + | void add_position(lista &l, int r_d, int posit){ if(posit <0 or posit > l.el_count) return; if(posit == 0)
|
| 56 | - | cout << "Usuwanie powiodlo sie" << endl; |
| 56 | + | {add_head(l, r_d); return;}
|
| 57 | - | } |
| 57 | + | if(posit == l.el_count){ add_tail(l, r_d); return;}
|
| 58 | - | else |
| 58 | + | |
| 59 | - | cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl; |
| 59 | + | for(int i=0; i < posit -1; i++) temp = temp -> next; |
| 60 | - | } |
| 60 | + | element* n_e = new element; n_e -> data = r_d; n_e -> next = temp -> next; |
| 61 | temp -> next = n_e; l.el_count ++;} | |
| 62 | void delete_tail(lista &l){ if(l.tail == nullptr) return; element* temp = l.tail;
| |
| 63 | - | if (st == 0) |
| 63 | + | if(l.el_count == 1) {l.tail == nullptr; l.head == nullptr;}
|
| 64 | - | cout << "stos jest pusty" << endl; |
| 64 | + | else{ element * tempor = l.head;
|
| 65 | - | else{
|
| 65 | + | for (int i =1; i< l.el_count -1; i++) {tempor = tempor -> next;}
|
| 66 | - | cout << "na gorze stosu jest liczba :" << st -> data << endl; |
| 66 | + | l.tail = tempor; l.tail -> next = nullptr;} |
| 67 | - | } |
| 67 | + | l.el_count --; delete temp;} |
| 68 | - | } |
| 68 | + | void delete_head(lista &l){if(l.tail == nullptr) return; element* temp = l.head; l.head = l.head -> next;
|
| 69 | - | int main() |
| 69 | + | delete temp; if(l.head == nullptr) l.tail = nullptr; l.el_count--;} |
| 70 | - | {
|
| 70 | + | |
| 71 | - | float sre; |
| 71 | + | if (posit < 0 or posit > l.el_count){cout << "Podano niewlasciwa pozycje" << endl;return;}
|
| 72 | - | int *ilosc; |
| 72 | + | if (posit == 0){delete_head(l);return;}
|
| 73 | - | ilosc= new int; |
| 73 | + | if(posit == l.el_count){delete_tail(l);return;}
|
| 74 | - | int w; |
| 74 | + | element* temp = l.head; for (int i =1; i< l.el_count -1; i++) {temp = temp -> next;}
|
| 75 | - | stos* st = nullptr; |
| 75 | + | element* temp_delete = temp -> next; temp -> next = temp_delete -> next; |
| 76 | - | srand(time(NULL)); |
| 76 | + | delete temp_delete; l.el_count --;} |
| 77 | - | while (w!=6){
|
| 77 | + | void delete_list(lista &l){ while(l.head != nullptr) {delete_head(l);}}
|
| 78 | - | cout << " 1 - sprawdzenie czy stos jest pusty" << endl; |
| 78 | + | void show_list(lista l){element* temp = l.head; while (temp)
|
| 79 | - | cout << " 2 - dodanie elementu na stos" << endl; |
| 79 | + | {cout << temp -> data << endl; temp = temp -> next;}}
|
| 80 | - | cout << " 3 - usuniecie ze stosu" << endl; |
| 80 | + | int parzyste(lista l){int i=0; element* temp = l.head;
|
| 81 | - | cout << " 4 - pobranie elementu ze stosu" << endl; |
| 81 | + | while (temp){if(temp->data%2==0) i++; temp = temp -> next;} return i;}
|
| 82 | - | cout << " 5 - obliczyc srednia elementow" << endl; |
| 82 | + | void show_on_position(lista l, int posit){
|
| 83 | - | cout << " 6 - wyjscie z programu" << endl; |
| 83 | + | int c = 0; if (posit < 0 or posit > l.el_count) {return;}
|
| 84 | - | cout << " Co chcesz zrobic?" << endl; |
| 84 | + | |
| 85 | - | cin >> w; |
| 85 | + | while(l.el_count != 0){c++; if (c == posit) cout << temp -> data << endl; temp = temp -> next; l.el_count --;}}
|
| 86 | ---------------------------------------------LISTA DWUKIERUNKOWA------------------------------------------ | |
| 87 | - | switch (w) |
| 87 | + | |
| 88 | - | {
|
| 88 | + | struct element {int data; element* next; element* prev; };
|
| 89 | - | case 1: is_Empty(st); break; |
| 89 | + | struct dlist {element* head; element* tail; int el_count; };
|
| 90 | - | case 2: push (st,rand()%20); break; |
| 90 | + | dlist create_empty_dlist(){ dlist dl; dl.head = nullptr; dl.tail = nullptr; dl.el_count = 0; return dl;}
|
| 91 | - | case 3: pop(st); break; |
| 91 | + | element* create_element(int n_d){element* n_e = new element; n_e -> data = n_d;
|
| 92 | - | case 4: take_element(st); break; |
| 92 | + | n_e -> next = nullptr; n_e -> prev = nullptr; return n_e;} |
| 93 | - | case 5: sre=srednia(st ,ilosc); |
| 93 | + | void add_tail(int n_d, dlist &dl){ element* n_e = create_element(n_d);
|
| 94 | - | cout<< "wartosc srednia wynosi :" << sre << endl; |
| 94 | + | if (dl.el_count == 0) dl.head = n_e; else{ dl.tail -> next = n_e; n_e -> prev = dl.tail;}
|
| 95 | - | break; |
| 95 | + | dl.tail = n_e; dl.el_count ++;} if (dl.el_count == 0) dl.tail = n_e; |
| 96 | - | case 6: cout << "juz nic nie mozesz zrobic " << endl; break; |
| 96 | + | else{ dl.head -> prev = n_e; n_e -> next = dl.head; } dl.head = n_e; dl.el_count ++;}
|
| 97 | - | default: cout << " Bledny wybor" << endl; |
| 97 | + | element* find_position(dlist dl, int posit){if (posit < 0 or posit > dl.el_count-1) return nullptr;
|
| 98 | - | } |
| 98 | + | element* temp = dl.head; for (int i =0; i < posit; i++) temp = temp -> next; return temp;} |
| 99 | - | } |
| 99 | + | void add_position(int n_d, dlist &dl, int posit){if (posit < 1 or posit > dl.el_count){return;}
|
| 100 | - | return 0; |
| 100 | + | if (posit == 1) {add_head(n_d, dl); return;}
|
| 101 | - | } |
| 101 | + | if( posit == dl.el_count) {add_tail(n_d, dl); return;}
|
| 102 | element* temp = dl.head; for(int i=0; i < posit -1; i++) temp = temp -> next; | |
| 103 | element* n_e = new element; n_e -> data = n_d; n_e -> next = temp -> next; | |
| 104 | temp -> next = n_e; dl.el_count ++;} | |
| 105 | - | #include <iostream> |
| 105 | + | void delete_head(dlist &dl){ if (dl.el_count == 0){ return;}
|
| 106 | - | #include <time.h> |
| 106 | + | else{ element* temp = dl.head;
|
| 107 | - | #include <stdio.h> |
| 107 | + | if (dl.el_count == 1) {dl.head = nullptr; dl.tail = nullptr;}
|
| 108 | else {dl.head = dl.head -> next; dl.head -> prev = nullptr;} delete temp; dl.el_count --;}}
| |
| 109 | - | using namespace std; |
| 109 | + | |
| 110 | if (dl.el_count == 0) {return;}
| |
| 111 | - | struct element {
|
| 111 | + | else{ element* temp = dl.tail; if (dl.el_count == 1) {dl.tail = nullptr; dl.head = nullptr;}
|
| 112 | - | int x; |
| 112 | + | else{ dl.tail = dl.tail -> prev; dl.tail -> next = nullptr;} delete temp;
|
| 113 | - | element* nast; |
| 113 | + | dl.el_count --;}} |
| 114 | - | }; |
| 114 | + | void delete_position(dlist &dl, int posit){ if (posit < 1 or posit > dl.el_count){ return;}
|
| 115 | - | struct kolejka {
|
| 115 | + | if (posit == 1) {delete_head(dl); return;}
|
| 116 | - | element* poczatek; |
| 116 | + | if(posit == dl.el_count){delete_tail(dl); return;}
|
| 117 | - | element* koniec; |
| 117 | + | element* temp = dl.head; for (int i =1; i< posit -1; i++) {temp = temp -> next;}
|
| 118 | - | }; |
| 118 | + | element* temp_delete = temp -> next; temp -> next = temp_delete -> next; |
| 119 | delete temp_delete; dl.el_count --;} | |
| 120 | - | {
|
| 120 | + | void show_list(dlist dl){ element* temp = dl.head;
|
| 121 | - | kolejka nowa_kolejka; |
| 121 | + | while (temp) {cout << temp -> data << endl; temp = temp -> next;}}
|
| 122 | - | nowa_kolejka.poczatek = nullptr; |
| 122 | + | void delete_list(dlist &dl){ while(dl.head != nullptr){delete_head(dl);}}
|
| 123 | - | nowa_kolejka.koniec = nullptr; |
| 123 | + | |
| 124 | - | return nowa_kolejka; |
| 124 | + | |
| 125 | - | } |
| 125 | + | { int x; element* lewy; element* prawy; element* gora;};
|
| 126 | struct drzewo {element* r;};
| |
| 127 | - | {
|
| 127 | + | |
| 128 | - | cout << rd << endl; |
| 128 | + | { drzewo nowe_drzewo; nowe_drzewo.r = nullptr; return nowe_drzewo;}
|
| 129 | - | element* nowy = new element; |
| 129 | + | |
| 130 | - | nowy -> x = rd; |
| 130 | + | {element *p; element *nowy_element = new element; nowy_element -> gora = nullptr;
|
| 131 | - | nowy -> nast = nullptr; |
| 131 | + | nowy_element -> lewy = nullptr; nowy_element -> prawy = nullptr; nowy_element -> x = rd; |
| 132 | - | if(q.koniec != nullptr) q.koniec -> nast = nowy; |
| 132 | + | p = root.r; if(!p) root.r = nowy_element; |
| 133 | - | q.koniec=nowy; |
| 133 | + | else{while (1){if (rd < p -> x){ if (!p -> lewy){p -> lewy = nowy_element; break;}
|
| 134 | - | if(q.poczatek == nullptr) q.poczatek = nowy; |
| 134 | + | else p = p -> lewy;} else { if (!p -> prawy){ p -> prawy = nowy_element;break;}
|
| 135 | - | } |
| 135 | + | else p = p -> prawy;}} nowy_element -> gora = p;}} |
| 136 | int wys_drzewo(element *root) | |
| 137 | - | {
|
| 137 | + | {if(root==nullptr) return 0; wys_drzewo(root->lewy);
|
| 138 | - | if (q.poczatek == nullptr) return; |
| 138 | + | cout<< root->x <<" "; wys_drzewo(root->prawy);} |
| 139 | - | element* temp = q.poczatek; |
| 139 | + | |
| 140 | - | q.poczatek = q.poczatek -> nast; |
| 140 | + | {if(root==nullptr) return false; if(root->x==szukana) return true;
|
| 141 | - | delete temp; |
| 141 | + | else{if (root->x < szukana) czy_istnieje(root->prawy, szukana);
|
| 142 | - | if(q.poczatek == nullptr) q.koniec == nullptr; |
| 142 | + | else czy_istnieje(root->lewy, szukana);}} |
| 143 | - | } |
| 143 | + | ---------------------------SORTOWANIE PRZEZ WYBIERANIE----------------------------- |
| 144 | int find_max_el(int *arr, int n){int maxel = arr[0]; int imax = 0;
| |
| 145 | - | {
|
| 145 | + | for (int i =0; i<n; i++) if(arr[i] > maxel){ maxel= arr[i]; imax = i;} return imax;}
|
| 146 | - | while (q.poczatek != nullptr) |
| 146 | + | void selectionsort(int *arr, int n){ int max_in; for(int i = n; i>1; i--){
|
| 147 | - | {
|
| 147 | + | max_in=find_max_el(arr, i); swap(arr[i-1], arr[max_in]);}} |
| 148 | - | cout << q.poczatek -> x << endl; |
| 148 | + | -----------------BĄBELKOWE------------------- |
| 149 | - | usun_elem_pocz(q); |
| 149 | + | void bublesort(int *arr, int n){ for(int i = n-1; i>0; i-- ){ for( int j = 0; j<i; j++){
|
| 150 | - | } |
| 150 | + | if(arr[j]>arr[j+1]) swap(arr[j], arr[j+1]);}}} |
| 151 | - | } |
| 151 | + | -----------------SORTOWANIE PRZEZ WSTAWIANIE-------------- |
| 152 | void insertionsort(int *arr, int n){ int temp, j; for(int i=n-2; i>=0; i--){ j = i; temp = arr[j];
| |
| 153 | - | int main() |
| 153 | + | while(temp > arr[j+1] and j < n-1){ arr[j] = arr[j+1]; j++;} arr[j] = temp;}}
|
| 154 | - | {
|
| 154 | + | ---------------FLAGA POLSKA---------------- |
| 155 | - | kolejka q; |
| 155 | + | int flagapolska(struct student x[], short n){ int p=0, k=n-1; int ind;
|
| 156 | - | int wybor; |
| 156 | + | while(p<k) {while(p<n-1 && x[p].ocena>=10) p++; while(k>=0 && x[k].ocena < 10) k--;
|
| 157 | - | srand(time(NULL)); |
| 157 | + | if(p<k){ swap(x[p],x[k]); ind = k;}
|
| 158 | - | do |
| 158 | + | if(x[p].ocena > 10) ind=p; else ind=p-1;} return k;} |
| 159 | - | {
|
| 159 | + | void wys (struct student* x, int p, int k) |
| 160 | - | cout << "co zrobic?" << endl; |
| 160 | + | {for(int i=p; i<k; i++){ cout << "imie: " << x[i].imie << " ocena = " << x[i].ocena << endl;}}
|
| 161 | - | cout << "1. Stworz nowa, pusta kolejke" << endl; |
| 161 | + | ------------FLAGA FFRANCUSKA--------------- |
| 162 | - | cout << "2. Dodaj losowy element do kolejki" << endl; |
| 162 | + | int flagafrancuska(struct student x[], short n, int* ko){int p=-1,s=0 ,k=n;
|
| 163 | - | cout << "3. Usuna element z kolejki" << endl; |
| 163 | + | while(s<k){if(x[s].ocena%3 == 0){p++; swap(x[p], x[s]); s++;}
|
| 164 | - | cout << "4. wyswietl i usun cala kolejke" << endl; |
| 164 | + | else{ if(x[s].ocena%3==2){ k--; swap(x[k], x[s]);} else s++;}}
|
| 165 | - | cout << "5. Wyjscie" << endl; |
| 165 | + | *ko=k; return p;} |
| 166 | - | cin >> wybor; |
| 166 | + | ------------WYSZUKIWANIE LINIOWE------------ |
| 167 | - | switch(wybor){
|
| 167 | + | int linsearch(int *arr, int n, int klucz){ for(int i=0; i<n; i++){ if(arr[i]==klucz)
|
| 168 | - | case 1: q=stworz_kolejke(); break; |
| 168 | + | return i;} return -1;} |
| 169 | - | case 2: dodaj_element (q, rand()); break; |
| 169 | + | ---------BISEKCJA Z PĘTLA--------------- |
| 170 | - | case 3: usun_elem_pocz (q); break; |
| 170 | + | int bis_p(int *arr, int n, int klucz){ int l=0; int p=n-1; int s;
|
| 171 | - | case 4: wyswietl_kolejke(q); break; |
| 171 | + | 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;}
|
| 172 | - | case 5: break; |
| 172 | + | ------------BISEKCJA REKURENCYJNA-------------- |
| 173 | - | default : cout << "bledny wybor" << endl; break; |
| 173 | + | int bis_rek(int *arr, int a, int b, int klucz){ int srodek=(a+b)/2;
|
| 174 | - | } |
| 174 | + | if(b>a){ if(arr[srodek] != klucz){ if(klucz < arr[srodek])
|
| 175 | - | } |
| 175 | + | return bis_rek(arr, a, srodek-1, klucz); else{ return bis_rek(arr, srodek+1, b, klucz);}}
|
| 176 | - | while (wybor != 5); |
| 176 | + | else{ return srodek;}} else { return -1; }}
|
| 177 | - | return 0; |
| 177 | + | ---------------KMP----------------- |
| 178 | - | } |
| 178 | + | int KMP (int n, int m, int *ind, string tekst, string wzorzec) |
| 179 | {int *tab; tab = new int[m]; tab[0]=0; tab[1]=0; int t=0; int x=0;
| |
| 180 | for(int i=2; i<=m; i++){ while(t>0 and wzorzec[t]!=wzorzec[i-1]) t=tab[t];
| |
| 181 | if(wzorzec[t]==wzorzec[i-1])t++; tab[i]=t;} | |
| 182 | int i=1; int j=0; while(i<=n-m+1){ while(wzorzec[j]==tekst[i+j-1] and j<m) j++;
| |
| 183 | - | #include <iostream> |
| 183 | + | if(j==m){ ind[x]=i-1; x++; j=m;} i=i+max(1,j-tab[j]); j=tab[j];} return x;}
|
| 184 | - | #include <time.h> |
| 184 | + | ---------NAIWNY--------------- |
| 185 | - | #include <stdio.h> |
| 185 | + | int naiwny(int n, int m, string tekst, string wzorzec, int *tab) |
| 186 | {int x=-1, j=0; for(int i = 0; i < n-1; i++){
| |
| 187 | - | using namespace std; |
| 187 | + | if (wzorzec == tekst.substr(i,m)){tab[j]=i; j++;}} return j;} |