Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. //ALGO2 IS1 210A LAB01
  2. //Grzegorz Cendrowski
  3. //cg44284@zut.edu.pl
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <ctime>
  10. #include <time.h>
  11. using namespace std;
  12. struct node
  13. {
  14. int key;
  15. double a;
  16. char b;
  17. node *next;
  18. node()
  19. {
  20. key = NULL;
  21. }
  22. };
  23. class list
  24. {
  25. public:
  26. node* head, *tail;
  27. list()
  28. {
  29. head = NULL;
  30. tail = NULL;
  31. }
  32. };
  33. //funkcje
  34. void stworz(list* lista, int n);
  35. void wstaw(list* lista, int n);
  36. void pokaz(list* lista, int n);
  37. void wyszukanie(list* lista, int n);
  38. void ostatnie(list* lista, int n);
  39. void podlicz(list* lista);
  40. void usunjeden(list* lista, int n);
  41. void usunwszystko(list* lista);
  42. int main()
  43. {
  44. srand(time(NULL));
  45. int n = 60;
  46. list* lista = new list;
  47.  
  48. stworz(lista, 1);
  49. wstaw(lista, n);
  50. pokaz(lista, 20);
  51. wyszukanie(lista, 1);
  52. podlicz(lista);
  53. stworz(lista, 99999);
  54. ostatnie(lista, 20);
  55. usunjeden(lista, 1);
  56. usunwszystko(lista);
  57. system("pause");
  58. }
  59. void stworz(list* lista, int n)
  60. {
  61. node* temp = new node;
  62. node* poprzedni = lista->head;
  63. temp->a = rand() % 10000;
  64. temp->b = 'T';
  65. temp->next = NULL;
  66. temp->key = n;
  67. if (lista->head == NULL)
  68. {
  69. lista->head = temp;
  70. lista->tail = temp;
  71.  
  72. temp = NULL;
  73. }
  74. else
  75. {
  76. if (lista->head->key == temp->key)
  77. {
  78. cout << "Error: Node z kluczem o wartosci n juz istnieje." << endl;
  79. return;
  80. }
  81.  
  82. while (poprzedni->next != NULL)
  83. {
  84. if (poprzedni->next->key == temp->key)
  85. {
  86. cout << "Error: Node z kluczem o wartosci n juz istnieje." << endl;
  87. return;
  88. }
  89. if (poprzedni->next->key > temp->key)
  90. {
  91. break;
  92. }
  93. poprzedni = poprzedni->next;
  94. }
  95. temp->key = n;
  96. temp->next = poprzedni->next;
  97. poprzedni->next = temp;
  98. }
  99.  
  100.  
  101. }
  102. void wstaw(list* lista, int n)
  103. {
  104. node *check = new node;
  105. node *poprzedni = lista->head;
  106. check = lista->head;
  107. int tempo;
  108. for (int i = 0; i < n; i++)
  109. {
  110. tempo = rand() % 100098 - 99;
  111. stworz(lista, tempo);
  112. }
  113. }
  114. void pokaz(list* lista, int n)
  115. {
  116. node *temp = new node;
  117. temp = lista->head;
  118. for (int a = 0; a < n; a++)
  119. {
  120. cout << temp->key << endl;
  121. temp = temp->next;
  122. }
  123. }
  124. void wyszukanie(list* lista, int n)
  125. {
  126. node *temp = new node;
  127.  
  128. int czywyszukana = 0;
  129. temp = lista->head;
  130. while (temp != NULL)
  131. {
  132. if (temp->key == n)
  133. {
  134. cout << "Adres o podanej wartosci kluczowej to: " << temp << endl;
  135. czywyszukana = 1;
  136. }
  137. temp = temp->next;
  138. }
  139. if (czywyszukana == 0)
  140. cout << "Error: Nie znaleziono podanej wartosci kluczowej." << endl;
  141.  
  142.  
  143. }
  144. void ostatnie(list* lista, int n)
  145. {
  146. node *temp = new node;
  147. temp = lista->head;
  148. int suma = 0, cur = 0;
  149. while (temp != NULL)
  150. {
  151. suma++;
  152. temp = temp->next;
  153. }
  154. suma = suma - n;
  155. temp = lista->head;
  156. while (temp != NULL)
  157. {
  158. if (suma <= cur)
  159. cout << temp->key << endl;
  160. temp = temp->next;
  161. cur++;
  162. }
  163.  
  164.  
  165. }
  166. void podlicz(list* lista)
  167. {
  168. node *temp = new node;
  169. temp = lista->head;
  170. int n = 0;
  171. while (temp != NULL)
  172. {
  173. n++;
  174. temp = temp->next;
  175. }
  176. cout << "Jest " << n << " node'ow" << endl;
  177. }
  178. void usunjeden(list* lista, int n)
  179. {
  180. node *temp = new node;
  181. node *poprzedni = new node;
  182. int czyusunieto = 0;
  183. temp = lista->head;
  184.  
  185. while (temp != NULL)
  186. {
  187. poprzedni = temp;
  188. if (temp->key == n)
  189. {
  190.  
  191. cout << "Usunieto node o adresie:" << temp << endl;
  192. delete temp;
  193. czyusunieto = 1;
  194. break;
  195. }
  196. temp = temp->next;
  197. lista->head = poprzedni;
  198. poprzedni->next = NULL;
  199. }
  200.  
  201. if (czyusunieto == 0)
  202. cout << "Nie usunieto node'a o podanej wartosci kluczowej gdyz nie zostal on znaleziony." << endl;
  203. }
  204. void usunwszystko(list* lista)
  205. {
  206. node *temp = lista->head;
  207. while (lista->head != NULL)
  208. {
  209. usunjeden(lista, lista->head->key);
  210. }
  211. lista->head = NULL;
  212.  
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement