Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctime>
  6. using namespace std;
  7. struct node
  8. {
  9. int key;
  10. double a;
  11. char b;
  12. node *next;
  13. node()
  14. {
  15. key = NULL;
  16. }
  17. };
  18. class list
  19. {
  20. public:
  21. node* head, *tail;
  22. list()
  23. {
  24. head = NULL;
  25. tail = NULL;
  26. }
  27. };
  28. //funkcje
  29. void stworz(list* lista, int n);
  30. void wstaw(list* lista, int n);
  31. void display(list* lista);
  32. void wyszukanie(list* lista, int n);
  33. void usun(list* lista, int n);
  34. int main()
  35. {
  36. int n = 20;
  37. list* lista = new list;
  38.  
  39. stworz(lista, 1);
  40. wstaw(lista, n);
  41. display(lista);
  42. wyszukanie(lista, 1);
  43. usun(lista, 1);
  44. display(lista);
  45.  
  46. system("pause");
  47. }
  48. void stworz(list* lista, int n)
  49. {
  50. node* temp = new node;
  51. node* poprzedni = lista->head;
  52. temp->a = rand() % 10000;
  53. temp->b = 'T';
  54. temp->next = NULL;
  55. temp->key = n;
  56. if (lista->head == NULL)
  57. {
  58. lista->head = temp;
  59. lista->tail = temp;
  60.  
  61. temp = NULL;
  62. }
  63. else
  64. {
  65. if (lista->head->key == temp->key)
  66. {
  67. cout << "Node with function argument n already exists" << endl;
  68. return;
  69. }
  70.  
  71. while(poprzedni->next != NULL)
  72. {
  73. if(poprzedni->next->key == temp->key)
  74. {
  75. cout << "Node with function argument n already exists" << endl;
  76. return;
  77. }
  78. if(poprzedni->next->key > temp->key)
  79. {
  80. break;
  81. }
  82. poprzedni = poprzedni->next;
  83. }
  84. temp->key = n;
  85. temp->next = poprzedni->next;
  86. poprzedni->next = temp;
  87. }
  88.  
  89.  
  90. }
  91. void wstaw(list* lista, int n)
  92. {
  93. node *check = new node;
  94. node *poprzedni = lista->head;
  95. check = lista->head;
  96. int tempo;
  97. srand(std::time(0));
  98. for (int i = 0; i < n; i++)
  99. {
  100. tempo = rand() % 100098 - 99;
  101. stworz(lista, tempo);
  102. }
  103. }
  104. void display(list* lista)
  105. {
  106. node *temp = new node;
  107.  
  108. temp = lista->head;
  109. while (temp != NULL)
  110. {
  111. cout << temp->key << endl;
  112. temp = temp->next;
  113. }
  114. }
  115. void wyszukanie(list* lista, int n)
  116. {
  117. node *temp = new node;
  118.  
  119. int czywyszukana = 0;
  120. temp = lista->head;
  121. while (temp != NULL)
  122. {
  123. if (temp->key == n)
  124. {
  125. cout << "Adres podanej wartosci kluczowej to: " << temp << endl;
  126. czywyszukana = 1;
  127. }
  128. temp = temp->next;
  129. }
  130. if (czywyszukana == 0)
  131. cout << "Nie znaleziono podanej wartosci kluczowej." << endl;
  132.  
  133.  
  134. }
  135. void usun(list* lista, int n)
  136. {
  137. node *temp = new node;
  138. node *poprzedni = new node;
  139. int czyusunieto = 0;
  140. temp = lista->head;
  141.  
  142. while (temp != NULL)
  143. {
  144. poprzedni = temp;
  145. if (temp->key == n)
  146. {
  147.  
  148. cout << "Usunieto node o adresie:" << temp << endl;
  149. delete temp;
  150. czyusunieto = 1;
  151. break;
  152. }
  153. temp = temp->next;
  154. lista->head = poprzedni;
  155. poprzedni->next = NULL;
  156. }
  157.  
  158. if (czyusunieto == 0)
  159. cout << "Nie usunieto node'a o podanej wartosci kluczowej gdyz nie zostal on znaleziony." << endl;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement