Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <time.h>
  4. #include <fstream>
  5. #include<windows.h>
  6. using namespace std;
  7.  
  8. struct Lista
  9. {
  10. int key;
  11. double zmiennaDouble;
  12. char zmiennaChar;
  13.  
  14. Lista* nastepny = nullptr;
  15. };
  16. void ini(Lista*& wsk)
  17. {
  18. wsk = nullptr;
  19.  
  20. }
  21. int addLista(Lista*& wsk, int klucz);
  22. void deleteLista(Lista*& wsk);
  23. void deleteElement(Lista*& wsk, int klucz);
  24. void dodaj(Lista*& wsk, int ile);
  25. void wyszukiwanie(Lista*& start, int klucz);
  26. int ileelementow(Lista*& wsk);
  27. void prezent_Y(Lista*& head, int ile);
  28. void prezent_Z(Lista*& head, int ile);
  29.  
  30.  
  31. int main()
  32. {
  33. Lista* start;
  34. start = nullptr;
  35. int x;
  36.  
  37. int X, k1, k2, k3, k4, k5;
  38. double time_spent;
  39. ifstream we("inlab02.txt");
  40.  
  41. if (!we) return -1;
  42. we >> X >> k1 >> k2 >> k3 >> k4 >> k5;
  43. we.close();
  44. srand(time(NULL));
  45. int startt = GetTickCount(); //--pomiar czasu początek
  46. ini(start);
  47.  
  48.  
  49. cout << "Wyszukuje klucz nr 1" << endl; wyszukiwanie(start, k1); cout << endl;
  50. cout << "Wstawienie X elementow do listy" << endl; dodaj(start, X); cout << endl;
  51. cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
  52. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  53. cout << "Wstawianie elementu k2" << endl; addLista(start, k2);
  54. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  55. cout << "Wstawianie elementu k3" << endl; addLista(start, k3);
  56. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  57. cout << "Wstawianie elementu k4" << endl; addLista(start, k4);
  58. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  59. cout << "Wstawienie elementu k5 i usuniecie k3" << endl; addLista(start, k5); deleteElement(start, k3);
  60. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  61. cout << "Usuniecie elementu k2" << endl; deleteElement(start, k2);
  62. cout << "Prezentacja 20 elementow czola: "; prezent_Y(start, 20); cout << endl;
  63. cout << "Usuniecie elementu k5" << endl; deleteElement(start, k5); cout << endl;
  64. cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
  65. cout << "Wyszukuje klucz nr 5" << endl; wyszukiwanie(start, k5); cout << endl;
  66. cout << "Prezentacja OSTATNICH 11 elementow w liscie: "; prezent_Z(start, 11); cout << endl;
  67. cout << "Liczba wezlow w liscie" << endl; ileelementow(start); cout << endl;
  68. cout << "Usuwam wszystkie elementy listy" << endl; deleteLista(start); cout << endl;
  69.  
  70. int koniec = GetTickCount(); //-pomiar czasu koniec
  71. cout << "Czas wykonania programu w sekundach: ";
  72. cout << (koniec - startt) * 0.001 << endl;
  73.  
  74. }
  75.  
  76. int addLista(Lista*& start, int klucz)
  77. {
  78.  
  79. Lista* x;
  80. x = nullptr;
  81.  
  82. if (start != nullptr)
  83. {
  84. Lista* wsk;
  85. wsk = start;
  86. if (wsk->key > klucz)
  87. {
  88. x = new Lista;
  89. x->key = klucz;
  90. x->zmiennaChar = 'T';
  91. x->zmiennaDouble = rand() % 1000;
  92. x->nastepny = start;
  93. start = x;
  94. return 0;
  95. }
  96. else {
  97.  
  98. while (wsk->nastepny != nullptr)
  99. {
  100. if (wsk->key == klucz)
  101. {
  102. //cout << "Blad: Klucz o tej wartosci juz istnieje." << endl;
  103. return -1;
  104. }
  105.  
  106. if (wsk->key < klucz && wsk->nastepny->key > klucz)
  107. {
  108. break;
  109. }
  110. wsk = wsk->nastepny;
  111.  
  112. };
  113. }
  114.  
  115. x = new Lista;
  116. x->key = klucz;
  117. x->zmiennaChar = 'T';
  118. x->zmiennaDouble = 99 + rand() % 1000;
  119. x->nastepny = wsk->nastepny;
  120. wsk->nastepny = x;
  121. return 0;
  122.  
  123.  
  124. }
  125.  
  126.  
  127. else
  128. {
  129. x = new Lista;
  130. x->key = klucz;
  131. x->zmiennaChar = 'T';
  132. x->zmiennaDouble = 99 + rand() % 1000;
  133. start = x;
  134. return 0;
  135. }
  136.  
  137. };
  138. void wyszukiwanie(Lista*& start, int klucz)
  139. {
  140.  
  141. if (start != nullptr)
  142. {
  143. Lista* x;
  144. x = start;
  145. if (klucz < start->key) cout << "NIE ISTNIEJEe." << endl;
  146. for (;;)
  147. {
  148. if (x->key == klucz)
  149. {
  150. cout << "Szukany wezel to :" << x->key << x->zmiennaChar << " " << x->zmiennaDouble << endl;
  151. break;
  152. }
  153. if (x->nastepny == nullptr)
  154.  
  155. {
  156. cout << "ELEMENT NIE ISTNIEJE " << endl;
  157. break;
  158. }
  159.  
  160.  
  161. x = x->nastepny;
  162.  
  163. }
  164. }
  165. else cout << "Lista jest pusta." << endl;
  166. }
  167. void deleteLista(Lista*& start)
  168. {
  169. Lista* wsk, * do_usuniecia;
  170. wsk = start;
  171. while (wsk != nullptr)
  172. {
  173. do_usuniecia = wsk;
  174. wsk = wsk->nastepny;
  175. delete do_usuniecia;
  176. };
  177.  
  178. start = nullptr;
  179. };
  180. void deleteElement(Lista*& start, int klucz)
  181. {
  182. if (start != nullptr)
  183. {
  184. Lista* x, * x2 = nullptr;
  185. if (start->key == klucz)
  186. {
  187. x = start->nastepny;
  188. delete start;
  189. start = x;
  190. }
  191. else
  192. {
  193. x = start;
  194. while (x)
  195. {
  196.  
  197. if (x->key == klucz)
  198. {
  199. x2->nastepny = x->nastepny;
  200. delete x;
  201. break;
  202. }
  203.  
  204. if (x->key < klucz && x->nastepny->key > klucz)
  205. {
  206. cout << "NIE MA TAKIEGO KLUCZA";
  207. return;
  208. }
  209. x2 = x;
  210. x = x->nastepny;
  211. };
  212. }
  213. }
  214. else cout << "Lista jest pusta." << endl;
  215. }
  216. void dodaj(Lista*& start, int ile)
  217. {
  218. int klucz = 0, error;
  219. for (int i = 0; i < ile; i++)
  220. {
  221. klucz = 99 + rand() % 100003;
  222.  
  223. error = addLista(start, klucz);
  224. if (error == -1)
  225. i--;
  226. }
  227.  
  228.  
  229.  
  230. }
  231. int ileelementow(Lista*& start)
  232. {
  233. Lista* wsk = start;
  234. int i = 0;
  235. while (wsk != nullptr)
  236. {
  237. i++;
  238. wsk = wsk->nastepny;
  239. }
  240. cout << i << endl;
  241. return i;
  242. }
  243. void prezent_Y(Lista*& head, int ile)
  244. {
  245. if (head != nullptr)
  246. {
  247. Lista* tmp;
  248. tmp = head;
  249. for (int i = 0; i < ile; i++)
  250. {
  251. cout << tmp->key << " ";
  252. tmp = tmp->nastepny;
  253. }
  254. cout << endl;
  255. }
  256. else cout << "Lista jest pusta." << endl;
  257. }
  258. void prezent_Z(Lista*& head, int ile)
  259. {
  260. int* tab = new int[ile];
  261.  
  262.  
  263. if (head != nullptr)
  264. {
  265. Lista* tmp = head;
  266. int s = ileelementow(head);
  267. for (int i = 0; i < ile; i++) {
  268. for (int j = 1; j <= s - i ; j++) {
  269. if (j == s - i) {
  270. cout << tmp->key << "| ";
  271. }
  272. else tmp = tmp->nastepny;
  273. }
  274. tmp = head;
  275. }
  276. }
  277. else cout << "Lista jest pusta." << endl;
  278. delete[] tab;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement