Czapek

kolos algorytmy

Jun 10th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.53 KB | None | 0 0
  1. ------------------------------------STOS---------------------------------------------
  2.  
  3. #include <iostream>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. struct stos
  9. {
  10. int data;
  11. stos* next;
  12. };
  13.  
  14.  
  15. bool is_Empty(stos* st){
  16. if(st == nullptr) cout << " Stos jest pusty" << endl;
  17. else{
  18. cout << " W stosie cos jednak bylo" << endl;
  19. return st == nullptr;
  20. }
  21. }
  22. int srednia(stos* &st, int *ilosc)
  23. {
  24. stos* st_2 = nullptr;
  25. *ilosc=0;
  26. float suma =0;
  27. float sre=0;
  28. while(st != nullptr)
  29. {
  30. suma =suma+st->data;
  31. (*ilosc)++;
  32. push(st_2, st->data);
  33. st=st->next;
  34. }
  35. while(st_2 != nullptr)
  36. {
  37. push(st, st_2->data);
  38. st_2=st_2->next;
  39. }
  40. float x=static_cast<float>(*ilosc);
  41. sre=suma/x;
  42. return sre;
  43. }
  44. void push (stos* &st, int new_data){
  45. cout << new_data << endl;
  46. stos* dodana_wartosc = new stos;
  47. dodana_wartosc -> data = new_data;
  48. dodana_wartosc -> next = st;
  49. st = dodana_wartosc;
  50. }
  51. void pop(stos* &st){
  52. if(st != nullptr){
  53. stos* temp = st;
  54. st = st -> next;
  55. delete temp;
  56. cout << "Usuwanie powiodlo sie" << endl;
  57. }
  58. else
  59. cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl;
  60. }
  61.  
  62. void take_element(stos* st){
  63. if (st == 0)
  64. cout << "stos jest pusty" << endl;
  65. else{
  66. cout << "na gorze stosu jest liczba :" << st -> data << endl;
  67. }
  68. }
  69. int main()
  70. {
  71. float sre;
  72. int *ilosc;
  73. ilosc= new int;
  74. int w;
  75. stos* st = nullptr;
  76. srand(time(NULL));
  77. while (w!=6){
  78. cout << " 1 - sprawdzenie czy stos jest pusty" << endl;
  79. cout << " 2 - dodanie elementu na stos" << endl;
  80. cout << " 3 - usuniecie ze stosu" << endl;
  81. cout << " 4 - pobranie elementu ze stosu" << endl;
  82. cout << " 5 - obliczyc srednia elementow" << endl;
  83. cout << " 6 - wyjscie z programu" << endl;
  84. cout << " Co chcesz zrobic?" << endl;
  85. cin >> w;
  86.  
  87. switch (w)
  88. {
  89. case 1: is_Empty(st); break;
  90. case 2: push (st,rand()%20); break;
  91. case 3: pop(st); break;
  92. case 4: take_element(st); break;
  93. case 5: sre=srednia(st ,ilosc);
  94. cout<< "wartosc srednia wynosi :" << sre << endl;
  95. break;
  96. case 6: cout << "juz nic nie mozesz zrobic " << endl; break;
  97. default: cout << " Bledny wybor" << endl;
  98. }
  99. }
  100. return 0;
  101. }
  102.  
  103. -------------------------------------KOLEJKA-----------------------------------------------
  104.  
  105. #include <iostream>
  106. #include <time.h>
  107. #include <stdio.h>
  108.  
  109. using namespace std;
  110.  
  111. struct element {
  112. int x;
  113. element* nast;
  114. };
  115. struct kolejka {
  116. element* poczatek;
  117. element* koniec;
  118. };
  119. kolejka stworz_kolejke ()
  120. {
  121. kolejka nowa_kolejka;
  122. nowa_kolejka.poczatek = nullptr;
  123. nowa_kolejka.koniec = nullptr;
  124. return nowa_kolejka;
  125. }
  126. void dodaj_element (kolejka &q, int rd)
  127. {
  128. cout << rd << endl;
  129. element* nowy = new element;
  130. nowy -> x = rd;
  131. nowy -> nast = nullptr;
  132. if(q.koniec != nullptr) q.koniec -> nast = nowy;
  133. q.koniec=nowy;
  134. if(q.poczatek == nullptr) q.poczatek = nowy;
  135. }
  136. void usun_elem_pocz (kolejka &q)
  137. {
  138. if (q.poczatek == nullptr) return;
  139. element* temp = q.poczatek;
  140. q.poczatek = q.poczatek -> nast;
  141. delete temp;
  142. if(q.poczatek == nullptr) q.koniec == nullptr;
  143. }
  144. void wyswietl_kolejke(kolejka &q)
  145. {
  146. while (q.poczatek != nullptr)
  147. {
  148. cout << q.poczatek -> x << endl;
  149. usun_elem_pocz(q);
  150. }
  151. }
  152.  
  153. int main()
  154. {
  155. kolejka q;
  156. int wybor;
  157. srand(time(NULL));
  158. do
  159. {
  160. cout << "co zrobic?" << endl;
  161. cout << "1. Stworz nowa, pusta kolejke" << endl;
  162. cout << "2. Dodaj losowy element do kolejki" << endl;
  163. cout << "3. Usuna element z kolejki" << endl;
  164. cout << "4. wyswietl i usun cala kolejke" << endl;
  165. cout << "5. Wyjscie" << endl;
  166. cin >> wybor;
  167. switch(wybor){
  168. case 1: q=stworz_kolejke(); break;
  169. case 2: dodaj_element (q, rand()); break;
  170. case 3: usun_elem_pocz (q); break;
  171. case 4: wyswietl_kolejke(q); break;
  172. case 5: break;
  173. default : cout << "bledny wybor" << endl; break;
  174. }
  175. }
  176. while (wybor != 5);
  177. return 0;
  178. }
  179.  
  180. ----------------------------------------LISTA JEDNOKIERUNKOWA----------------------------------------------
  181.  
  182.  
  183. #include <iostream>
  184. #include <time.h>
  185. #include <stdio.h>
  186.  
  187. using namespace std;
  188.  
  189. struct element {
  190. int data;
  191. element* next;
  192. };
  193. struct lista {
  194. element* head;
  195. element* tail;
  196. int el_count;
  197. };
  198.  
  199. lista create_list (){
  200. lista new_list;
  201. new_list.head = nullptr;
  202. new_list.tail = nullptr;
  203. new_list.el_count = 0;
  204. return new_list;}
  205.  
  206. void add_tail(lista &l, int r_d){
  207. cout << "Lista biezaca:" << endl;
  208. show_list(l);
  209. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  210. element* n_tail = new element;
  211. n_tail -> data = r_d;
  212. n_tail -> next = nullptr;
  213.  
  214. if(l.tail != nullptr)
  215. l.tail -> next = n_tail;
  216. else
  217. l.head = n_tail;
  218. l.tail = n_tail;
  219. l.el_count ++;
  220. cout << "Lista po zmianie:" << endl;
  221. show_list(l);
  222. }
  223.  
  224. void add_head(lista &l, int r_d){
  225. cout << "Lista biezaca:" << endl;
  226. show_list(l);
  227. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  228. element* n_head = new element;
  229. n_head -> data = r_d;
  230. n_head -> next = l.head;
  231. l.head = n_head;
  232.  
  233. if(l.tail == nullptr)
  234. l.tail == n_head;
  235. l.el_count ++;
  236. cout << "Lista po zmianie:" << endl;
  237. show_list(l);
  238. }
  239.  
  240. void add_position(lista &l, int r_d, int posit){
  241. cout << "Lista biezaca:" << endl;
  242. show_list(l);
  243. cout << "Numer ktory zostanie dodany: " << r_d << endl;
  244. if(posit <0 or posit > l.el_count) return;
  245. if(posit == 0)
  246. {
  247. add_head(l, r_d);
  248. return;
  249. }
  250. if(posit == l.el_count)
  251. {
  252. add_tail(l, r_d);
  253. return;
  254. }
  255. element* temp = l.head;
  256. for(int i=0; i < posit -1; i++)
  257. temp = temp -> next;
  258. element* n_e = new element;
  259. n_e -> data = r_d;
  260. n_e -> next = temp -> next;
  261. temp -> next = n_e;
  262. l.el_count ++;
  263. cout << "Lista po zmianie:" << endl;
  264. show_list(l);
  265. }
  266.  
  267.  
  268. void delete_tail(lista &l){
  269. cout << "Lista biezaca:" << endl;
  270. show_list(l);
  271. if(l.tail == nullptr) return;
  272. element* temp = l.tail;
  273. if(l.el_count == 1)
  274. {
  275. l.tail == nullptr;
  276. l.head == nullptr;
  277. }
  278. else{
  279. element * tempor = l.head;
  280. for (int i =1; i< l.el_count -1; i++)
  281. {
  282. tempor = tempor -> next;
  283. }
  284. l.tail = tempor;
  285. l.tail -> next = nullptr;
  286. }
  287. l.el_count --;
  288. delete temp;
  289. cout << "Lista po zmianie:" << endl;
  290. show_list(l);
  291. }
  292.  
  293. void delete_head(lista &l){
  294. if(l.tail == nullptr) return;
  295. cout << "Lista biezaca:" << endl;
  296. show_list(l);
  297. element* temp = l.head;
  298. l.head = l.head -> next;
  299. delete temp;
  300. if(l.head == nullptr)
  301. l.tail = nullptr;
  302. cout << "Element z poczatku zostal usuniety" << endl;
  303. l.el_count--;
  304. cout << "Lista po zmianie:" << endl;
  305. show_list(l);
  306. }
  307.  
  308. void delete_position(lista &l, int posit){
  309. if (posit < 0 or posit > l.el_count)
  310. {
  311. cout << "Podano niewlasciwa pozycje" << endl;
  312. return;
  313. }
  314. cout << "Lista biezaca:" << endl;
  315. show_list(l);
  316. if (posit == 0)
  317. {
  318. delete_head(l);
  319. return;
  320. }
  321. if(posit == l.el_count)
  322. {
  323. delete_tail(l);
  324. return;
  325. }
  326. element* temp = l.head;
  327. for (int i =1; i< l.el_count -1; i++)
  328. {
  329. temp = temp -> next;
  330. }
  331. element* temp_delete = temp -> next;
  332. temp -> next = temp_delete -> next;
  333. delete temp_delete;
  334. l.el_count --;
  335. cout << "Lista po zmianie:" << endl;
  336. show_list(l);
  337. }
  338.  
  339. void delete_list(lista &l){
  340. while(l.head != nullptr)
  341. {
  342. delete_head(l);
  343. }
  344. }
  345.  
  346. void show_list(lista l){
  347. element* temp = l.head;
  348. while (temp)
  349. {
  350. cout << temp -> data << endl;
  351. temp = temp -> next;
  352. }
  353. }
  354. int parzyste(lista l)
  355. {
  356. int i=0;
  357. element* temp = l.head;
  358. while (temp)
  359. {
  360. if(temp->data%2==0) i++;
  361. temp = temp -> next;
  362. }
  363. return i;
  364. }
  365.  
  366. void show_on_position(lista l, int posit)
  367. {
  368. int c = 0;
  369. if (posit < 0 or posit > l.el_count)
  370. {
  371. cout << "Podano niewlasciwa pozycje" << endl;
  372. return;
  373. }
  374. element* temp = l.head;
  375. while(l.el_count != 0){
  376. c++;
  377. if (c == posit) cout << temp -> data << endl;
  378. temp = temp -> next;
  379. l.el_count --;
  380. }
  381. }
  382.  
  383.  
  384. int main()
  385. {
  386. lista l = create_list();
  387. int wybor, posit, x;
  388. cout << "Podaj pozycje" << endl;
  389. cin >> posit;
  390. srand(time(NULL));
  391. do{
  392. cout << "Podaj wybor:" << endl;
  393. cout << "1. Dodawanie elementu o losowej wartosci na koniec listy " << endl;
  394. cout << "2. Dodawanie elementu o losowej wartosci na poczatek listy " << endl;
  395. cout << "3. Dodawanie elementu o losowej wartosci na pozycje o podanym numerze " << endl;
  396. cout << "4. Usuniecie elementu z konca listy " << endl;
  397. cout << "5. Usuniecie elementu z poczatku listy" << endl;
  398. cout << "6. Usuniecie elementu z podanej pozycji" << endl;
  399. cout << "7. Wyswietlenie elementu o podanym numerze" << endl;
  400. cout << "8. Wyswietlenie calej listy" << endl;
  401. cout << "9. Usuniecie wszystkich elementow listy wraz ze zwolnieniem pamieci" << endl;
  402. cout << "10. Ilosc przystych" << endl;
  403. cout << "11. Wyjscie" << endl;
  404. cout << "Odpowiedz:" << endl;
  405. cin >> wybor;
  406. switch(wybor){
  407. case 1: add_tail(l, rand()%10); break;
  408. case 2: add_head(l, rand()); break;
  409. case 3: add_position(l, rand(), posit); break;
  410. case 4: delete_tail(l); break;
  411. case 5: delete_head(l); break;
  412. case 6: delete_position(l, posit); break;
  413. case 7: show_on_position(l, posit); break;
  414. case 8: show_list(l); break;
  415. case 9: delete_list(l); break;
  416. case 10:
  417. x=parzyste(l);
  418. cout << "liczba przystych : " << x <<endl; break;
  419. case 11: cout << "Wyszedles z progrmau" << endl; break;
  420. default : cout << "bledny wybor" << endl; break;
  421.  
  422.  
  423. }
  424. } while (wybor != 11);
  425. return 0;
  426. }
  427.  
  428.  
  429. ---------------------------------------------LISTA DWUKIERUNKOWA------------------------------------------
  430.  
  431.  
  432. #include <iostream>
  433. #include <time.h>
  434. #include <stdio.h>
  435.  
  436. struct element {
  437. int data;
  438. element* next;
  439. element* prev;
  440. };
  441.  
  442. struct dlist {
  443. element* head;
  444. element* tail;
  445. int el_count;
  446. };
  447.  
  448. dlist create_empty_dlist(){
  449. dlist dl;
  450. dl.head = nullptr;
  451. dl.tail = nullptr;
  452. dl.el_count = 0;
  453. return dl;
  454. }
  455.  
  456. element* create_element(int n_d){ // nie dodaje, tworzy element
  457. element* n_e = new element;
  458. n_e -> data = n_d;
  459. n_e -> next = nullptr;
  460. n_e -> prev = nullptr;
  461. return n_e;
  462. }
  463.  
  464. void add_tail(int n_d, dlist &dl){
  465. element* n_e = create_element(n_d);
  466. cout << "Numer ktory zostanie dodany: " << n_d << endl;
  467. if (dl.el_count == 0)
  468. dl.head = n_e;
  469. else{
  470. dl.tail -> next = n_e;
  471. n_e -> prev = dl.tail;
  472. }
  473. dl.tail = n_e;
  474. dl.el_count ++;
  475. }
  476.  
  477. void add_head(int n_d, dlist &dl){
  478. element* n_e = create_element(n_d);
  479. cout << "Numer ktory zostanie dodany: " << n_d << endl;
  480. if (dl.el_count == 0)
  481. dl.tail = n_e;
  482. else{
  483. dl.head -> prev = n_e;
  484. n_e -> next = dl.head;
  485. }
  486. dl.head = n_e;
  487. dl.el_count ++;
  488. }
  489.  
  490. element* find_position(dlist dl, int posit){
  491.  
  492. if (posit < 0 or posit > dl.el_count-1)
  493. return nullptr;
  494.  
  495. element* temp = dl.head;
  496. for (int i =0; i < posit; i++)
  497. temp = temp -> next;
  498. return temp;
  499. }
  500.  
  501. void add_position(int n_d, dlist &dl, int posit){
  502. cout << "Numer ktory zostanie dodany: " << n_d << endl;
  503. if (posit < 1 or posit > dl.el_count){
  504. cout << " Niewlasciwa pozycja" << endl;
  505. return;
  506. }
  507. if (posit == 1)
  508. {
  509. add_head(n_d, dl);
  510. return;
  511. }
  512. if( posit == dl.el_count)
  513. {
  514. add_tail(n_d, dl);
  515. return;
  516. }
  517. element* temp = dl.head;
  518. for(int i=0; i < posit -1; i++)
  519. temp = temp -> next;
  520. element* n_e = new element;
  521. n_e -> data = n_d;
  522. n_e -> next = temp -> next;
  523. temp -> next = n_e;
  524. dl.el_count ++;
  525. }
  526.  
  527. void delete_head(dlist &dl){
  528. if (dl.el_count == 0)
  529. {
  530. cout << "Dwulista jest pusta" << endl;
  531. return;
  532. }
  533. else{
  534. element* temp = dl.head;
  535. if(dl.el_count == 1)
  536. {
  537. dl.head = nullptr;
  538. dl.tail = nullptr;
  539. }
  540. else
  541. {
  542. dl.head = dl.head -> next;
  543. dl.head -> prev = nullptr;
  544. }
  545. delete temp;
  546. dl.el_count --;
  547. }
  548. }
  549.  
  550. void delete_tail(dlist &dl){
  551.  
  552. if (dl.el_count == 0)
  553. {
  554. cout << " Dwulista jest pusta " << endl;
  555. return;
  556. }
  557. else{
  558. element* temp = dl.tail;
  559. if (dl.el_count == 1)
  560. {
  561. dl.tail = nullptr;
  562. dl.head = nullptr;
  563. }
  564. else
  565. {
  566. dl.tail = dl.tail -> prev;
  567. dl.tail -> next = nullptr;
  568. }
  569. delete temp;
  570. dl.el_count --;
  571. }
  572. }
  573.  
  574. void delete_position(dlist &dl, int posit){
  575. if (posit < 1 or posit > dl.el_count)
  576. {
  577. cout << "Podano niewlasciwa pozycje" << endl;
  578. return;
  579. }
  580. if (posit == 1)
  581. {
  582. delete_head(dl);
  583. return;
  584. }
  585. if(posit == dl.el_count)
  586. {
  587. delete_tail(dl);
  588. return;
  589. }
  590. element* temp = dl.head;
  591. for (int i =1; i< posit -1; i++)
  592. {
  593. temp = temp -> next;
  594. }
  595. element* temp_delete = temp -> next;
  596. temp -> next = temp_delete -> next;
  597. delete temp_delete;
  598. dl.el_count --;
  599. }
  600.  
  601. void show_list(dlist dl){
  602. element* temp = dl.head;
  603. while (temp)
  604. {
  605. cout << temp -> data << endl;
  606. temp = temp -> next;
  607. }
  608. }
  609.  
  610. void delete_list(dlist &dl){
  611. while(dl.head != nullptr){
  612. delete_head(dl);
  613. }
  614. cout << "Lista zostala usunieta" << endl;
  615. }
  616.  
  617. void show_reversed(dlist dl){
  618. element* temp = dl.tail;
  619. while (temp)
  620. {
  621. cout << temp -> data << endl;
  622. temp = temp -> prev;
  623. }
  624. }
  625.  
  626. using namespace std;
  627.  
  628. int main()
  629. {
  630. dlist dl = create_empty_dlist();
  631. int wybor, posit;
  632. cout << "Podaj pozycje:" << endl;
  633. cin >> posit;
  634. srand(time(NULL));
  635. do{
  636. cout << "Co chcesz zrobic?" << endl;
  637. cout << "1. Dodac element na koniec listy" << endl;
  638. cout << "2. Dodac element na poczatek listy" << endl;
  639. cout << "3. Dodac element na pozycje" << endl;
  640. cout << "4. Usunac z konca" << endl;
  641. cout << "5. Usunac z poczatku" << endl;
  642. cout << "6. Usunac z pozycji" << endl;
  643. cout << "7. Usunac calosc" << endl;
  644. cout << "8. Wyswietlic calosc" << endl;
  645. cout << "9. Wyswietlic calosc od konca" << endl;
  646. cout << "10. Juz nic nie chcesz" << endl;
  647. cin >> wybor;
  648. switch(wybor){
  649. case 1: add_tail( rand(),dl); break;
  650. case 2: add_head( rand(),dl); break;
  651. case 3: add_position( rand(), dl, posit); break;
  652. case 4: delete_tail(dl); break;
  653. case 5: delete_head(dl); break;
  654. case 6: delete_position(dl, posit); break;
  655. case 7: delete_list(dl); break;
  656. case 8: show_list(dl); break;
  657. case 9: show_reversed(dl); break;
  658. case 10: cout << " Zakonczyles program"; break;
  659. default : cout <<" Zly numer, sprobuj jeszcze raz" << endl;
  660. }
  661.  
  662. }
  663. while (wybor != 10);
  664. return 0;
  665. }
  666.  
  667.  
  668.  
  669.  
  670. -----------------------------------------------LISTA JEDNOKIERUNKOWA CYKLICZNA---------------------------------------
  671.  
  672. #include <iostream>
  673. #include <time.h>
  674. #include <stdio.h>
  675.  
  676. using namespace std;
  677.  
  678. struct element
  679. {
  680. int x;
  681. element* nast;
  682. };
  683. struct lista_cykliczna
  684. {
  685. element* biezacy;
  686. };
  687. lista_cykliczna stworz_liste ()
  688. {
  689. lista_cykliczna nowa_lista;
  690. nowa_lista.biezacy = nullptr;
  691. return nowa_lista;
  692. }
  693. void dodaj_element (lista_cykliczna &q, int rd)
  694. {
  695. cout << rd << endl;
  696. element* nowy = new element;
  697. nowy -> x = rd;
  698. if(q.biezacy != nullptr)
  699. {
  700. nowy -> nast = q.biezacy -> nast;
  701. q.biezacy -> nast = nowy;
  702. }
  703. else nowy -> nast = nowy;
  704. q.biezacy = nowy;
  705.  
  706. }
  707. void dodaj_element_za_element (lista_cykliczna &q, int istniejacy, int wstaw)
  708. {
  709. short kontrolka=0;
  710. element* nowy = new element;
  711. nowy -> x = wstaw;
  712. element* szukany = new element;
  713. szukany -> x = istniejacy;
  714. element* p = new element;
  715. p = q.biezacy;
  716. if(p!=nullptr)
  717. do
  718. {
  719.  
  720. if(p -> x == szukany -> x)
  721. {
  722. nowy -> nast = p -> nast;
  723. p -> nast = nowy;
  724. kontrolka=1;
  725. }
  726. p = p -> nast;
  727. }
  728. while(p != q.biezacy);
  729. else
  730. {
  731. nowy -> nast = szukany;
  732. q.biezacy = nowy;
  733. szukany -> nast = nowy;
  734. kontrolka=1;
  735. }
  736. if(kontrolka==0)
  737. {
  738. nowy -> nast = q.biezacy -> nast;
  739. q.biezacy -> nast = nowy;
  740. szukany -> nast = q.biezacy -> nast;
  741. q.biezacy -> nast = szukany;
  742. }
  743. }
  744. void wyswietl(lista_cykliczna &q)
  745. {
  746. element* nowy;
  747. nowy = q.biezacy;
  748. if(q.biezacy!=nullptr)
  749. do
  750. {
  751. cout << nowy->x << endl;
  752. nowy = nowy->nast;
  753. }
  754. while(nowy != q.biezacy);
  755. }
  756. void szukanko(lista_cykliczna &q, int istniejacy)
  757. {
  758. int kontrolka=0;
  759. element* szukany = new element;
  760. szukany -> x = istniejacy;
  761. element* p = new element;
  762. p = q.biezacy;
  763. if(p!=nullptr)
  764. do
  765. {
  766.  
  767. if(p -> x == szukany -> x)
  768. {
  769. cout << "Szukany element\n" << "Dane: " << p -> x << "\nAdres nastepnego elementu: " << p -> nast << "\nWartosc nastepnego elementu: " << p -> nast -> x << endl;
  770. kontrolka=1;
  771. }
  772. p = p -> nast;
  773. }
  774. while(p != q.biezacy);
  775. if(kontrolka==0) cout << "Elementu nie ma w kolejce lub kolejka jest pusta" << endl;
  776.  
  777. }
  778.  
  779. int main()
  780. {
  781. lista_cykliczna q;
  782. int wybor;
  783. int istniejacy, wstaw, istniejacy_2;
  784. srand(time(NULL));
  785. do
  786. {
  787. cout << "Co zrobic?" << endl;
  788. cout << "1. Stworz nowa, pusta liste" << endl;
  789. cout << "2. Dodaj losowy element do listy" << endl;
  790. cout << "3. Dodaj nowy element za istniejacy element" << endl;
  791. cout << "4. Wyszukaj element" << endl;
  792. cout << "5. Wyswietl liste" << endl;
  793. cout << "6. Wyjscie" << endl;
  794. cin >> wybor;
  795. cout << endl << endl;
  796. switch(wybor){
  797. case 1: q=stworz_liste(); break;
  798. case 2: dodaj_element (q, rand()); break;
  799. case 3:
  800. {
  801. cout << "Podaj element istniejacy" << endl;
  802. cin >> istniejacy;
  803. cout << endl << endl;
  804. cout << "Podaj nowy elemeny" << endl;
  805. cin >> wstaw;
  806. cout << endl << endl;
  807. dodaj_element_za_element (q, istniejacy, wstaw);
  808. break;
  809. }
  810. case 4:
  811. {
  812. cout << "Podaj element szukany" << endl;
  813. cin >> istniejacy_2;
  814. cout << endl << endl;
  815. szukanko(q, istniejacy_2);
  816. break;
  817. }
  818. case 5: wyswietl(q); break;
  819. case 6: break;
  820. default : cout << "bledny wybor" << endl; break;
  821. }
  822. }
  823. while (wybor != 6);
  824. return 0;
  825. }
  826.  
  827.  
  828. ----------------------------------------------LISTA DWUKIERUNKOWA CYKLICZNA-------------------------------------------
  829.  
  830. #include <iostream>
  831. #include <time.h>
  832. #include <stdio.h>
  833.  
  834. using namespace std;
  835.  
  836. struct element
  837. {
  838. int data;
  839. element* next;
  840. element* prev;
  841. };
  842.  
  843. struct dlist
  844. {
  845. element* current;
  846. int el_count;
  847. };
  848.  
  849. dlist create_empty_dlist()
  850. {
  851. dlist dl;
  852. dl.current = nullptr;
  853. dl.el_count = 0;
  854. return dl;
  855. }
  856.  
  857. element* create_element(int n_d)
  858. { // nie dodaje, tworzy element
  859. element* n_e = new element;
  860. n_e -> data = n_d;
  861. n_e -> next = nullptr;
  862. n_e -> prev = nullptr;
  863. return n_e;
  864. }
  865.  
  866. void add_new(int n_d, dlist &dl)
  867. {
  868. element* n_e = create_element(n_d);
  869. cout << "Numer ktory zostanie dodany: " << n_d << endl;
  870. if (dl.current == nullptr)
  871. {
  872. n_e -> next = n_e;
  873. n_e -> prev = n_e;
  874. }
  875. else
  876. {
  877. n_e -> prev = dl.current;
  878. n_e -> next = dl.current ->next;
  879. dl.current ->next ->prev = n_e;
  880. dl.current -> next = n_e;
  881. }
  882. dl.current = n_e;
  883. dl.el_count ++;
  884. }
  885.  
  886. void delete_element(dlist &dl, int value)
  887. {
  888. bool kontrolka = false;
  889. if (dl.el_count == 0)
  890. {
  891. cout << "Dwulista jest pusta" << endl;
  892. return;
  893. }
  894. else
  895. {
  896. element* temp = dl.current;
  897. if(temp != nullptr)
  898. do
  899. {
  900. if(temp -> data == value)
  901. {
  902. temp->prev->next = temp->next;
  903. temp->next->prev = temp->prev;
  904. kontrolka=true;
  905. }
  906. temp = temp -> next;
  907. }
  908. while(temp != dl.current and kontrolka == false);
  909. delete temp;
  910. dl.el_count --;
  911. }
  912. if(kontrolka==false) cout << "Elementu nie ma w liscie" << endl;
  913. }
  914.  
  915. void show_list(dlist &dl)
  916. {
  917. element* temp = dl.current;
  918. //for(int i = 0; i < dl.el_count; i++)
  919. if(dl.current != nullptr)
  920. do
  921. {
  922. cout << temp -> data << endl;
  923. temp = temp -> prev;
  924. }
  925. while(temp != dl.current);
  926. }
  927.  
  928. int main()
  929. {
  930. dlist dl = create_empty_dlist();
  931. int wybor, value;
  932. srand(time(NULL));
  933. do{
  934. cout << "Co chcesz zrobic?" << endl;
  935. cout << "1. Dodac element" << endl;
  936. cout << "2. Usunac element o podanej wartosci" << endl;
  937. cout << "3. Wyswietlic od prawej do lewej" << endl;
  938. cout << "4. Juz nic nie chcesz" << endl;
  939. cin >> wybor;
  940. switch(wybor)
  941. {
  942. case 1: add_new(rand()%50, dl); break;
  943. case 2:
  944. cout << "Podaj wartosc" << endl;
  945. cin >> value;
  946. delete_element(dl, value);
  947. break;
  948. case 3: show_list(dl); break;
  949. case 4: cout << " Zakonczyles program"; break;
  950. default : cout <<" Zly numer, sproboj jeszcze raz" << endl;
  951. }
  952. }
  953. while (wybor != 4);
  954. return 0;
  955. }
  956.  
  957.  
  958. ------------------------------------------DRZEWO------------------------------------------------
  959. #include <iostream>
  960. #include <time.h>
  961. #include <stdio.h>
  962.  
  963. using namespace std;
  964.  
  965. struct element
  966. {
  967. int x;
  968. element* lewy;
  969. element* prawy;
  970. element* gora;
  971. };
  972. struct drzewo
  973. {
  974. element* r;
  975. };
  976. drzewo stworz_drzewo ()
  977. {
  978. drzewo nowe_drzewo;
  979. nowe_drzewo.r = nullptr;
  980. return nowe_drzewo;
  981. }
  982. void dodaj_element (drzewo &root, int rd)
  983. {
  984. element *p;
  985. element *nowy_element = new element;
  986. nowy_element -> gora = nullptr;
  987. nowy_element -> lewy = nullptr;
  988. nowy_element -> prawy = nullptr;
  989. nowy_element -> x = rd;
  990. cout << rd << endl;
  991. p = root.r;
  992. if(!p) root.r = nowy_element;
  993. else
  994. {
  995. while (1)
  996. {
  997. if (rd < p -> x)
  998. {
  999. if (!p -> lewy)
  1000. {
  1001. p -> lewy = nowy_element;
  1002. break;
  1003. }
  1004. else p = p -> lewy;
  1005. }
  1006. else
  1007. {
  1008. if (!p -> prawy)
  1009. {
  1010. p -> prawy = nowy_element;
  1011. break;
  1012. }
  1013. else p = p -> prawy;
  1014. }
  1015. }
  1016. nowy_element -> gora = p;
  1017. }
  1018. }
  1019. int wys_drzewo(element *root)
  1020. {
  1021. if(root==nullptr) return 0;
  1022. wys_drzewo(root->lewy);
  1023. cout<< root->x <<" ";
  1024. wys_drzewo(root->prawy);
  1025. }
  1026. bool czy_istnieje(element *root, int szukana)
  1027. {
  1028. if(root==nullptr) return false;
  1029. if(root->x==szukana) return true;
  1030. else
  1031. {
  1032. if (root->x < szukana) czy_istnieje(root->prawy, szukana);
  1033. else czy_istnieje(root->lewy, szukana);
  1034. }
  1035. }
  1036. int main()
  1037. {
  1038. drzewo d;
  1039. int wybor, szukana;
  1040. bool istnieje;
  1041. d=stworz_drzewo();
  1042. srand(time(NULL));
  1043. do
  1044. {
  1045. cout << "Co zrobic?" << endl;
  1046. cout << "1. Dodaj losowy element do drzewa" << endl;
  1047. cout << "2. Wyswietl drzewo " << endl;;
  1048. cout << "3. Sprawdz czy element istnieje w drzewie" << endl;
  1049. cout << "4. Wyjscie" << endl;
  1050. cin >> wybor;
  1051. cout << endl << endl;
  1052. switch(wybor){
  1053. case 1: dodaj_element (d, rand()); break;
  1054. case 2: wys_drzewo(d.r); break;
  1055. case 3:
  1056. {
  1057. cout << "Podaj szukana" << endl;
  1058. cin >> szukana;
  1059. istnieje=czy_istnieje(d.r, szukana);
  1060. if(istnieje==true)cout << "element istnieje" << endl;
  1061. else cout << "element nie istnieje" << endl;
  1062. break;
  1063. }
  1064. case 4: break;
  1065. default : cout << "bledny wybor" << endl; break;
  1066. }
  1067. }
  1068. while (wybor != 4);
  1069. return 0;
  1070. }
Advertisement
Add Comment
Please, Sign In to add comment