Paszta

Algosy - wersja kompaktowa

Jun 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                                 ------------------------------------STOS------------------------------------------
  2. struct stos{ int data; stos* next;};
  3. bool is_Empty(stos* st){
  4.     if(st == nullptr) cout << " Stos jest pusty" << endl;
  5.     else{return st == nullptr;}}
  6. int srednia(stos* &st, int *ilosc)
  7. {stos* st_2 = nullptr;
  8.     *ilosc=0;float suma =0; float sre=0;
  9.     while(st != nullptr)
  10.     {suma =suma+st->data; (*ilosc)++; push(st_2, st->data); st=st->next;}
  11.     while(st_2 != nullptr)
  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. dodana_wartosc -> next = st; st = dodana_wartosc;}
  18. void pop(stos* &st){if(st != nullptr){
  19.     stos* temp = st; st = st -> next; delete temp;}
  20.     else cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl; }
  21. void take_element(stos* st){
  22. if (st == 0) cout << "stos jest pusty" << endl;
  23. else{ cout << "na gorze stosu jest liczba :" << st -> data << endl;}}
  24.  
  25.             -------------------------------------KOLEJKA-----------------------------------------------
  26. struct element {int x; element* nast;};
  27. struct kolejka {element* poczatek; element* koniec;};
  28. kolejka stworz_kolejke ()
  29. {kolejka nowa_kolejka; nowa_kolejka.poczatek = nullptr;
  30.  nowa_kolejka.koniec = nullptr; return nowa_kolejka;}
  31. void dodaj_element (kolejka &q, int rd)
  32. {element* nowy = new element; nowy -> x = rd; nowy -> nast = nullptr;
  33.     if(q.koniec != nullptr) q.koniec -> nast = nowy; q.koniec=nowy;
  34.     if(q.poczatek == nullptr) q.poczatek = nowy;}
  35. void usun_elem_pocz (kolejka &q)
  36. {if (q.poczatek == nullptr) return; element* temp = q.poczatek;
  37.  q.poczatek = q.poczatek -> nast; delete temp;
  38.  if(q.poczatek == nullptr) q.koniec == nullptr;}
  39.  void wyswietl_kolejke(kolejka &q)
  40.  {while (q.poczatek != nullptr)
  41.     {cout << q.poczatek -> x << endl; usun_elem_pocz(q);}}
  42.  
  43. ----------------------------------------LISTA JEDNOKIERUNKOWA----------------------------------------------
  44. struct element {int data; element* next;};
  45. struct lista {element* head; element* tail; int el_count;};
  46. lista create_list (){lista new_list; new_list.head = nullptr; new_list.tail = nullptr;
  47. new_list.el_count = 0; return new_list;}
  48. void add_tail(lista &l, int r_d){ element* n_tail = new element; n_tail -> data = r_d; n_tail -> next = nullptr;
  49. if(l.tail != nullptr)l.tail -> next = n_tail;
  50. else l.head = n_tail; l.tail = n_tail; l.el_count ++;}
  51. void add_head(lista &l, int r_d){
  52. element* n_head = new element; n_head -> data = r_d; n_head -> next = l.head; l.head = n_head;
  53. if(l.tail == nullptr) l.tail == n_head; l.el_count ++;}
  54.  
  55. void add_position(lista &l, int r_d, int posit){ if(posit <0 or posit > l.el_count) return; if(posit == 0)
  56.     {add_head(l, r_d); return;}
  57. if(posit == l.el_count){ add_tail(l, r_d); return;}
  58. element* temp = l.head;
  59. for(int i=0; i < posit -1; i++) temp = temp -> next;
  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(l.el_count == 1) {l.tail == nullptr; l.head == nullptr;}
  64. else{ element * tempor = l.head;
  65. for (int i =1; i< l.el_count -1; i++) {tempor = tempor -> next;}
  66. l.tail = tempor; l.tail -> next = nullptr;}
  67. l.el_count --; delete temp;}
  68. void delete_head(lista &l){if(l.tail == nullptr) return; element* temp = l.head; l.head = l.head -> next;
  69. delete temp; if(l.head  == nullptr) l.tail = nullptr; l.el_count--;}
  70. void delete_position(lista &l, int posit){
  71. if (posit < 0 or posit > l.el_count){cout << "Podano niewlasciwa pozycje" << endl;return;}
  72. if (posit == 0){delete_head(l);return;}
  73. if(posit == l.el_count){delete_tail(l);return;}
  74. element* temp = l.head; for (int i =1; i< l.el_count -1; i++) {temp = temp -> next;}
  75. element* temp_delete = temp -> next; temp -> next = temp_delete -> next;
  76. delete temp_delete; l.el_count --;}
  77. void delete_list(lista &l){ while(l.head != nullptr) {delete_head(l);}}
  78. void show_list(lista l){element* temp = l.head; while (temp)
  79.     {cout << temp -> data << endl; temp = temp -> next;}}
  80. int parzyste(lista l){int i=0; element* temp = l.head;
  81. while (temp){if(temp->data%2==0) i++; temp = temp -> next;} return i;}
  82. void show_on_position(lista l, int posit){
  83. int c = 0; if (posit < 0 or posit > l.el_count) {return;}
  84. element* temp = l.head;
  85. while(l.el_count != 0){c++; if (c == posit) cout << temp -> data << endl; temp = temp -> next; l.el_count --;}}
  86. ---------------------------------------------LISTA DWUKIERUNKOWA------------------------------------------
  87.  
  88. struct element {int data; element* next; element* prev; };
  89. struct dlist {element* head; element* tail; int el_count; };
  90. dlist create_empty_dlist(){ dlist dl; dl.head = nullptr; dl.tail = nullptr; dl.el_count = 0; return dl;}
  91. element* create_element(int n_d){element* n_e = new element; n_e -> data = n_d;
  92. n_e -> next = nullptr; n_e -> prev = nullptr; return n_e;}
  93. void add_tail(int n_d, dlist &dl){ element* n_e = create_element(n_d);
  94. if (dl.el_count == 0) dl.head = n_e; else{ dl.tail -> next = n_e; n_e -> prev = dl.tail;}
  95. dl.tail = n_e; dl.el_count ++;} if (dl.el_count == 0) dl.tail = n_e;
  96. else{ dl.head -> prev = n_e; n_e -> next = dl.head; } dl.head = n_e; dl.el_count ++;}
  97. element* find_position(dlist dl, int posit){if (posit < 0 or posit > dl.el_count-1) return nullptr;
  98. element* temp = dl.head; for (int i =0; i < posit; i++) temp = temp -> next; return temp;}
  99. void add_position(int n_d, dlist &dl, int posit){if (posit < 1 or posit > dl.el_count){return;}
  100. if (posit == 1) {add_head(n_d, dl); return;}
  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. void delete_head(dlist &dl){ if (dl.el_count == 0){ return;}
  106. else{ element* temp = dl.head;
  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. void delete_tail(dlist &dl){
  110. if (dl.el_count == 0) {return;}
  111. else{ element* temp = dl.tail; if (dl.el_count == 1) {dl.tail = nullptr; dl.head = nullptr;}
  112. else{ dl.tail = dl.tail -> prev; dl.tail -> next = nullptr;} delete temp;
  113. dl.el_count --;}}
  114. void delete_position(dlist &dl, int posit){ if (posit < 1 or posit > dl.el_count){ return;}
  115. if (posit == 1) {delete_head(dl); return;}
  116. if(posit == dl.el_count){delete_tail(dl); return;}
  117. element* temp = dl.head; for (int i =1; i< posit -1; i++) {temp = temp -> next;}
  118. element* temp_delete = temp -> next; temp -> next = temp_delete -> next;
  119. delete temp_delete; dl.el_count --;}
  120. void show_list(dlist dl){ element* temp = dl.head;
  121. while (temp) {cout << temp -> data << endl; temp = temp -> next;}}
  122. void delete_list(dlist &dl){ while(dl.head != nullptr){delete_head(dl);}}
  123. ------------------------------------------DRZEWO------------------------------------------------
  124. struct element
  125. { int x; element* lewy; element* prawy; element* gora;};
  126. struct drzewo {element* r;};
  127. drzewo stworz_drzewo ()
  128. { drzewo nowe_drzewo; nowe_drzewo.r = nullptr; return nowe_drzewo;}
  129. void dodaj_element (drzewo &root, int rd)
  130. {element *p; element *nowy_element = new element; nowy_element -> gora = nullptr;
  131.  nowy_element -> lewy = nullptr; nowy_element -> prawy = nullptr; nowy_element -> x = rd;
  132.  p = root.r; if(!p) root.r = nowy_element;
  133.  else{while (1){if (rd < p -> x){ if (!p -> lewy){p -> lewy = nowy_element; break;}
  134.    else p = p -> lewy;} else { if (!p -> prawy){ p -> prawy = nowy_element;break;}
  135.      else p = p -> prawy;}} nowy_element -> gora = p;}}
  136. int wys_drzewo(element *root)
  137. {if(root==nullptr) return 0; wys_drzewo(root->lewy);
  138.     cout<< root->x <<" "; wys_drzewo(root->prawy);}
  139. bool czy_istnieje(element *root, int szukana)
  140. {if(root==nullptr) return false; if(root->x==szukana) return true;
  141. else{if (root->x < szukana) czy_istnieje(root->prawy, szukana);
  142.         else czy_istnieje(root->lewy, szukana);}}
  143. ---------------------------SORTOWANIE PRZEZ WYBIERANIE-----------------------------
  144. int find_max_el(int *arr, int n){int maxel = arr[0]; int imax = 0;
  145. for (int i =0; i<n; i++) if(arr[i] > maxel){ maxel= arr[i]; imax = i;} return imax;}
  146. void selectionsort(int *arr, int n){ int max_in; for(int i = n; i>1; i--){
  147. max_in=find_max_el(arr, i); swap(arr[i-1], arr[max_in]);}}
  148. -----------------BĄBELKOWE-------------------
  149. void bublesort(int *arr, int n){ for(int i = n-1; i>0; i-- ){ for( int j = 0; j<i; j++){
  150.  if(arr[j]>arr[j+1]) swap(arr[j], arr[j+1]);}}}
  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. while(temp > arr[j+1] and j < n-1){ arr[j] = arr[j+1]; j++;} arr[j] = temp;}}
  154. ---------------FLAGA POLSKA----------------
  155. int flagapolska(struct student x[], short n){ int p=0, k=n-1; int ind;
  156.  while(p<k) {while(p<n-1 && x[p].ocena>=10) p++; while(k>=0 && x[k].ocena < 10) k--;
  157.  if(p<k){ swap(x[p],x[k]); ind = k;}
  158.  if(x[p].ocena > 10) ind=p; else ind=p-1;} return k;}
  159. void wys (struct student* x, int p, int k)
  160. {for(int i=p; i<k; i++){ cout << "imie: " << x[i].imie << " ocena = " << x[i].ocena << endl;}}
  161. ------------FLAGA FFRANCUSKA---------------
  162. int flagafrancuska(struct student x[], short n, int* ko){int p=-1,s=0 ,k=n;
  163.  while(s<k){if(x[s].ocena%3 == 0){p++; swap(x[p], x[s]); s++;}
  164.  else{ if(x[s].ocena%3==2){ k--; swap(x[k], x[s]);} else s++;}}
  165. *ko=k; return p;}
  166. ------------WYSZUKIWANIE LINIOWE------------
  167. int linsearch(int *arr, int n, int klucz){ for(int i=0; i<n; i++){ if(arr[i]==klucz)
  168. return i;} return -1;}
  169. ---------BISEKCJA Z PĘTLA---------------
  170. int bis_p(int *arr, int n, int klucz){ int l=0; int p=n-1; int s;
  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. ------------BISEKCJA REKURENCYJNA--------------
  173. int bis_rek(int *arr, int a, int b, int klucz){ int srodek=(a+b)/2;
  174. if(b>a){ if(arr[srodek] != klucz){ if(klucz < arr[srodek])
  175. return bis_rek(arr, a, srodek-1, klucz); else{ return bis_rek(arr, srodek+1, b, klucz);}}
  176. else{ return srodek;}} else { return -1; }}
  177. ---------------KMP-----------------
  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. if(j==m){ ind[x]=i-1; x++; j=m;} i=i+max(1,j-tab[j]); j=tab[j];} return x;}
  184. ---------NAIWNY---------------
  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.  if (wzorzec == tekst.substr(i,m)){tab[j]=i; j++;}} return j;}
Add Comment
Please, Sign In to add comment