Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. #include <string.h>
  8. #include <cstring>
  9. using namespace std;
  10.  
  11. class lista{
  12. private:
  13. int key;
  14. double zmienna;
  15. char znak;
  16. lista *next;
  17. public:
  18. void sprawdzenie_wstawienie(class lista *&head,int x);
  19. void dodaj(class lista *&head, int x);
  20. void wypisz(class lista *head);
  21. void szukanie(class lista *head,int wartosc);
  22. void wypisz_klucze(class lista *head,int ilosc);
  23. int liczenie(class lista *head);
  24. void sortowanie(class lista *&head);
  25. void usuwanie(class lista *&head);
  26. void wstawianieel(class lista *&head,int x);
  27. void usuwanieel(class lista *&head,int x);
  28. void wypisz_ostatnie(class lista *head,int ile, int ilosc);
  29. }*head=NULL,*tmp=NULL; // tmp=ogon
  30.  
  31. void lista:: dodaj(class lista *&head,int x)
  32. {
  33.  
  34. if(x==0)
  35. {
  36. head->key=(rand() % (99999 - 99 +1) + 99);
  37. head->znak='N';
  38. head->zmienna=(rand() % (9000 - 1000 + 1) - 1000);
  39. head->next=NULL;
  40. tmp=head;
  41. }
  42. else
  43. {
  44. bool test;
  45. lista *help=head,*help2=head,*help3 = NULL,*nowy=(class lista*)new lista;
  46. help3=NULL;
  47. do{
  48. help2=head;
  49. test=false;
  50. nowy->key=(rand() % (99999 - 99 +1) + 99);
  51. nowy->znak='N';
  52. nowy->zmienna=(rand() % (9000 - 1000 + 1) - 1000);
  53. while(help2)
  54. {
  55. if(nowy->key==help2->key)
  56. {
  57. test=true;
  58. }
  59. help2=help2->next;
  60. }
  61. }while(test==true);
  62. while(help)
  63. {
  64. if(help->next==NULL)
  65. {
  66. if(nowy->key>help->key)
  67. {
  68. help->next=nowy;
  69. nowy->next=NULL;
  70. return;
  71. }
  72. else
  73. {
  74. if(help3==NULL)
  75. {
  76. nowy->next=help;
  77. head=nowy;
  78. return;
  79. }
  80. else
  81. {
  82. help3->next=nowy;
  83. nowy->next=help;
  84. return;
  85. }
  86. }
  87. }
  88. if(nowy->key<help->key&&help3==NULL)
  89. {
  90. nowy->next=help;
  91. head=nowy;
  92. return;
  93. }
  94. if((nowy->key>help->key)&&(nowy->key<help->next->key))
  95. {
  96. nowy->next=help->next;
  97. help->next=nowy;
  98. return;
  99. }
  100.  
  101. help3=help;
  102. help=help->next;
  103. }
  104. }
  105. }
  106.  
  107.  
  108. void lista::wstawianieel(class lista *&head,int x)
  109. {
  110. lista *help=head, *help2 = NULL, *nowy=new lista;
  111. nowy->key=x;
  112. nowy->znak='T';
  113. nowy->zmienna=(rand() % (9000 - 1000 + 1) - 1000);
  114. while(help)
  115. {
  116. if(help->next==NULL)
  117. {
  118. if(nowy->key>help->key)
  119. {
  120. help->next=nowy;
  121. nowy->next=NULL;
  122. return;
  123. }
  124. else
  125. {
  126. if(help2==NULL)
  127. {
  128. nowy->next=help;
  129. head=nowy;
  130. return;
  131. }
  132. else
  133. {
  134. help2->next=nowy;
  135. nowy->next=help;
  136. return;
  137. }
  138. }
  139. }
  140. if(nowy->key<help->key&&help2==NULL)
  141. {
  142. nowy->next=help;
  143. head=nowy;
  144. return;
  145. }
  146. if((nowy->key>help->key)&&(nowy->key<help->next->key))
  147. {
  148. nowy->next=help->next;
  149. help->next=nowy;
  150. return;
  151. }
  152.  
  153. help2=help;
  154. help=help->next;
  155. }
  156.  
  157. }
  158.  
  159. void lista::usuwanieel(class lista *&head,int x)
  160. {
  161. class lista *help=head, *help2 = NULL;
  162. while(help)
  163. {
  164. if(help->key==x)
  165. {
  166. help2->next=help->next;
  167. return;
  168. }
  169. help2 = help;
  170. help=help->next;
  171. }
  172. cout<<"nie ma w liscie elemntu z kluczem "<<x<<" by go usunac"<<endl<<endl;
  173. }
  174.  
  175.  
  176. void lista::szukanie(class lista *head,int wartosc)
  177. {
  178. class lista *help=head;
  179. bool test=false;
  180. while(help)
  181. {
  182. if(help->key==wartosc)
  183. {
  184. cout<< "Szukany element----> Klucz:"<<help->key<<" "<<"zmienna:"<<help->zmienna<<" "<<"Znak:"<<help->znak<<" "<<"Adres: " <<help;
  185. test=true;
  186. break;
  187. }
  188. help=help->next;
  189. }
  190. if(test==false)
  191. {
  192. cout<<"Nie znalezniono wezla z kluczem "<<wartosc<<" w liscie"<<endl;
  193. }
  194. }
  195.  
  196. void lista::wypisz(class lista *head)
  197. {
  198. class lista *help=head;
  199. while(help)
  200. {
  201. cout<< "Klucz:"<<help->key<<" "<<"zmienna:"<<help->zmienna<<" "<<"Znak:"<<help->znak<<" "<<"Adres: " <<help;
  202. cout<<endl;
  203. help=help->next;
  204. }
  205. }
  206.  
  207. void lista::wypisz_ostatnie(class lista *head,int ile, int ilosc)
  208. {
  209. class lista *help=head;
  210. int licznik2=0,licznik=0;
  211. int tab[10];
  212. while(help)
  213. {
  214. if(licznik<ilosc-ile)
  215. {
  216. help=help->next;
  217. licznik++;
  218. }
  219. else
  220. {
  221. tab[licznik2]=help->key;
  222. licznik2++;
  223. help=help->next;
  224. }
  225. }
  226. for(int i=10;i>=0;i--)
  227. {
  228. cout<< "Klucz:"<<tab[i];
  229. cout<<endl;
  230. }
  231. }
  232.  
  233. void lista::wypisz_klucze(class lista *head,int ilosc)
  234. {
  235. class lista *help=head;
  236. for(int i=0;i<ilosc;i++)
  237. {
  238. cout<<"Klucz:"<<help->key<<endl;
  239. help=help->next;
  240. }
  241. }
  242.  
  243. int lista::liczenie(class lista *head)
  244. {
  245. class lista *help=head;
  246. int licznik=0;
  247. while(help)
  248. {
  249. licznik+=1;
  250. help=help->next;
  251. }
  252. return licznik;
  253. }
  254.  
  255. void lista::usuwanie(class lista *&head)
  256. {
  257. lista *help = NULL;
  258. while(head)
  259. {
  260. help = head;
  261. head = head->next;
  262. delete help;
  263. }
  264. delete head;
  265. }
  266.  
  267. void pierwsze_20(class lista *head)
  268. {
  269. cout<<"pierwsze 20:"<<endl;
  270. head->wypisz_klucze(head,20);
  271. cout<<endl;
  272. }
  273.  
  274. void lista::sprawdzenie_wstawienie(class lista *&head,int x)
  275. {
  276. bool test=true;
  277. lista *help=head;
  278. while(help)
  279. {
  280. if(help->key==x)
  281. {test=false;}
  282. help=help->next;
  283. }
  284. if(test==false)
  285. {
  286. cout<<"element z takim kluczem juz istnieje"<<endl<<endl;
  287. return;
  288. }
  289. else
  290. {
  291. head->wstawianieel(head,x);
  292. }
  293. }
  294.  
  295. int main()
  296. {
  297. srand( time( NULL ) );
  298. clock_t begin, end;
  299. double time_spent;
  300. begin = clock();
  301.  
  302. int ilosc_wezlow=0;
  303. int x;
  304. int k1,k2,k3,k4,k5;
  305. int n;
  306. ifstream dane("inlab02.txt");
  307. dane >> x;
  308. dane >> k1;
  309. dane >> k2;
  310. dane >> k3;
  311. dane >> k4;
  312. dane >> k5;
  313. cout <<"klucze: "<<k1<<" "<<k2<<" "<<k3<<" "<<k4<<" "<<k5<< endl;
  314. cout << endl;
  315.  
  316. head= new lista;
  317.  
  318. for(int i=0;i<x;i++)
  319. head->dodaj(head,i);
  320.  
  321. //head->wypisz(head);
  322. cout<<endl;
  323.  
  324. head->szukanie(head,k1);
  325. cout<<endl;
  326.  
  327. ilosc_wezlow=head->liczenie(head);
  328. cout<<"ilosc wezlow = "<<ilosc_wezlow<<endl<<endl;
  329.  
  330. pierwsze_20(head);
  331. head->sprawdzenie_wstawienie(head,k2);
  332. pierwsze_20(head);
  333. head->sprawdzenie_wstawienie(head,k3);
  334. pierwsze_20(head);
  335. head->sprawdzenie_wstawienie(head,k4);
  336. pierwsze_20(head);
  337. head->sprawdzenie_wstawienie(head,k5);
  338. head->usuwanieel(head,k3);
  339. pierwsze_20(head);
  340. head->usuwanieel(head,k2);
  341. pierwsze_20(head);
  342. head->usuwanieel(head,k5);
  343.  
  344. ilosc_wezlow=head->liczenie(head);
  345. cout<<"ilosc wezlow = "<<ilosc_wezlow<<endl<<endl;
  346.  
  347. head->szukanie(head,k5);
  348. cout<<endl;
  349.  
  350. cout<<"ostatnie 11:"<<endl;
  351. ilosc_wezlow=head->liczenie(head);
  352. head->wypisz_ostatnie(head,11,ilosc_wezlow);
  353. cout<<endl;
  354.  
  355. ilosc_wezlow=head->liczenie(head);
  356. cout<<"ilosc wezlow = "<<ilosc_wezlow<<endl<<endl;
  357.  
  358. head->usuwanie(head);
  359.  
  360. end = clock();
  361. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  362. cout << "Czas wykonania: " << time_spent << endl;
  363. return 0;
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement