Advertisement
Guest User

Untitled

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