Advertisement
Guest User

Untitled

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