Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <time.h>
  4. const char v = 'T';
  5. const int N = 20;
  6.  
  7. struct Data {
  8. int key;
  9. double num;
  10. char val;
  11. Data * next;
  12. Data() { key = 0; next = 0; }
  13. Data(const int k, const double n, const char v) {
  14. key = k;
  15. num = n;
  16. val = v;
  17. next = 0;
  18. }
  19. };
  20. /*
  21. void showw(Data* first) {
  22. Data * temp = first;
  23. int i = 0;
  24. while (temp->next) {
  25. std::cout << i + 1 << ". ID: " << temp->key << " DOUBLE VALUE: " << temp->num << " CHAR SIGN: " << temp->val << std::endl;
  26. temp = temp->next;
  27. i++;
  28. }
  29. std::cout << std::endl;
  30.  
  31. }*/
  32. Data* create(const int n, Data * first); //2) tworzenie bazy danych
  33. Data * del(Data * first); //8) usuwanie wszystkich elementow listy
  34. int add_data(const int k, Data *& first); //1) dodawanie nowego elementu do listy
  35. int del_data(const int k, Data *& first); //4) usuwanie elementu
  36. void find(const int k, Data * first); //3) wyszukanie elementu
  37. void show(Data * first);
  38. void show_first(const int y, Data * first); //5) wyświetlenie pierwszych Y elementów
  39. void show_last( int &z, Data * first); //6) prezentacja ostatnich Z elementów
  40. int count(Data * first); //7) zliczanie węzłów
  41.  
  42. int main() {
  43. int y = 20, z = 11;
  44. std::fstream file;
  45. clock_t begin, end;
  46. unsigned int X;
  47. int k1, k2, k3, k4, k5;
  48. double timer;
  49.  
  50. file.open("inlab02.txt", std::ios::in); // ładowanie danych
  51. if (file.good()) {
  52. while (!file.eof())
  53. file >> X >> k1 >> k2 >> k3 >> k4 >> k5;
  54. }
  55. file.close();
  56.  
  57. begin = clock(); //czas start
  58. Data* database = new Data; //inicajalizacja
  59. find(k1, database); //wyszukanie k1
  60. database = create(X, database); //wstawianie x elementów
  61. std::cout << "Liczba wezlow w bazie to :" << count(database) << std::endl;
  62.  
  63. show_first(20, database); // pierwsze dwadziescia wartosci
  64. if(add_data(k2, database)==1)
  65. std::cout << "BLAD... rekord o podanym kluczu juz istnieje!" << std::endl;
  66. show_first(20, database);
  67. if (add_data(k3, database) == 1)
  68. std::cout << "BLAD... rekord o podanym kluczu juz istnieje!" << std::endl;
  69. show_first(20, database);
  70. if (add_data(k4, database) == 1)
  71. std::cout << "BLAD... rekord o podanym kluczu juz istnieje!" << std::endl;
  72. show_first(20, database);
  73. if (add_data(k5, database) == 1)
  74. std::cout << "BLAD... rekord o podanym kluczu juz istnieje!" << std::endl;
  75. del_data(k3, database);
  76. show_first(20, database);
  77. del_data(k2, database);
  78. show_first(20, database);
  79. del_data(k5, database);
  80. std::cout << "Liczba wezlow w bazie to :" << count(database) << std::endl;
  81. find(k5, database);
  82. show_last(z, database);
  83. std::cout << "Liczba wezlow w bazie to :" << count(database) << std::endl;
  84. database =del(database);
  85. std::cout << "Baza usunieta" << std::endl;
  86. end= clock();
  87. timer = (double)(end - begin) / CLOCKS_PER_SEC;
  88. std::cout << "czas wykonania programu to : " << timer<< std::endl;
  89. // std::cout << database->next;
  90. system("pause");
  91. return 0;
  92. }
  93.  
  94. int add_data(const int k, Data *& first) { //1
  95. double n = std::rand() % 100;
  96. Data * last = new Data(k, n, v);
  97. Data * temp;
  98. temp = first;
  99. if (first->key > k) {
  100. first = last;
  101. first->next = temp;
  102. return 0;
  103. }
  104. while (temp->next) {
  105. if (temp->key == k) {
  106. //std::cout << "BLAD... rekord o podanym kluczu juz istnieje!" << std::endl;
  107. return 1;
  108. }
  109. if (temp->key < k && temp->next->key > k)
  110. {
  111. last->next = temp->next;
  112. temp->next = last;
  113. return 0;
  114. }
  115. temp = temp->next;
  116. }
  117. if (temp->next == NULL) {
  118. temp->next = last;
  119. last->next = 0;
  120. return 0;
  121. }
  122.  
  123.  
  124. }
  125.  
  126. Data* create(const int n, Data * first) { //2
  127. int k;
  128. int flag = 1;
  129. int i = n;
  130. if (first->key == 0) {
  131. first->key = (std::rand() % 100000) + 99;
  132. first->num = std::rand() % 100;
  133. first->val = v;
  134. i--;
  135.  
  136. }
  137.  
  138. while (i) { //wstawianie n elementów
  139. do {
  140. k= (std::rand() % 100000) +99;
  141. if (first->key > k) {
  142. Data * last = new Data(k, std::rand() % 100 , v);
  143. Data * temp2 ;
  144. temp2 = first;
  145. first = last;
  146. first->next = temp2;
  147. continue;
  148. }
  149. flag = add_data(k, first);
  150. } while (flag);
  151. i--;
  152. }
  153. return first;
  154. }
  155. int del_data(const int k, Data *&first) { //4
  156. Data * temp = first;
  157. Data * buffer = new Data;
  158. if (temp->key == k) {
  159. first = temp->next;
  160. delete temp;
  161. temp = nullptr;
  162. return 0;
  163. }
  164. while (temp->next) {
  165. if (temp->next->key == k) {
  166. if (temp->next->next) {
  167. buffer = temp->next;
  168. temp->next = temp->next->next;
  169. delete buffer;
  170. buffer = nullptr;
  171. return 0;
  172. }
  173. else {
  174. buffer = temp->next;
  175. delete buffer;
  176. buffer = nullptr;
  177. temp->next = 0;
  178. return 0;
  179. }
  180. }
  181. temp = temp->next;
  182. }
  183.  
  184. }
  185. void show(Data* first) {
  186.  
  187. std::cout << " ID: " << first->key << " DOUBLE VALUE: " << first->num << " CHAR SIGN: " << first->val << std::endl;
  188.  
  189. }
  190.  
  191. Data* del(Data * first) { //8
  192.  
  193. if (first->next) return 0;
  194. Data * temp = first->next;
  195. while (temp) {
  196. delete first;
  197. first = temp->next;
  198. }
  199. first = nullptr;
  200. return first;
  201. }
  202.  
  203. void find(const int k, Data * first) { //3
  204. if (first->key == 0) {
  205. std::cout << "baza rekordow nie posiada wartosci" << std::endl;
  206. }
  207. else {
  208. Data* temp = new Data;
  209. temp = first;
  210. while (temp->next && temp->key != k) {
  211.  
  212. temp = temp->next;
  213. }
  214.  
  215. if (temp->next == 0)
  216. std::cout << "Nie odnaleziono rekordu o zadanym kluczu ( " << k << " )" << std::endl;
  217. else
  218. std::cout << "Rekord o zadanym kluczu: " << k << " -> " << temp->num << " " << temp->val << std::endl;
  219. }
  220. }
  221.  
  222. void show_first(const int y, Data * first) { //5
  223. std::cout << " Pierwsze dwadziescia rekordow" << std::endl;
  224. if (first==0 || y<=0) {
  225. std::cout << "Błąd wyświetlania rekordow!..." << std::endl;
  226. }
  227. else {
  228. show(first);
  229. Data * temp = first;
  230. for (int i = 0; i < y - 1; i++) {
  231. show(temp->next);
  232. temp = temp->next;
  233. }
  234. }
  235. std::cout << std::endl;
  236. }
  237.  
  238. void show_last( int &z, Data * first) { //6
  239. int limit = count(first)-1;
  240. int j=0;
  241. if (first == 0 || z < 0) {
  242. std::cout << "Błąd wyświetlania rekordow!..." << std::endl;
  243. }
  244. else {
  245. Data* temp = first;
  246. Data * buffer = new Data[z];
  247. for(int i =0; temp->next; i++){
  248. if (i -1>= limit - z ) {
  249. buffer[j].key = temp->key;
  250. buffer[j].num = temp->num;
  251. buffer[j].val = temp->val;
  252. j++;
  253. }
  254. temp = temp->next;
  255. }
  256. std::cout << " Ostatnie jedeneascie rekordow" << std::endl;
  257. j -= 1;
  258. while (j >= 0) {
  259. std::cout << " ID: " << buffer[j].key << " DOUBLE VALUE: " << buffer[j].num << " CHAR SIGN: " << buffer[j].val << std::endl;
  260. --j;
  261. }
  262. }
  263. }
  264.  
  265. int count(Data * first) { //7
  266. Data * temp = first;
  267. int num = 0;
  268. while (temp->next) {
  269. num++;
  270. temp = temp->next;
  271. }
  272. return num;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement