Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 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 = 1;
  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. int main()
  33. {
  34. int n = 20;
  35. list* lista = new list;
  36. wstaw(lista, n);
  37. display(lista);
  38. system("pause");
  39. }
  40. void stworz(list* lista, int n)
  41. {
  42. node* temp = new node;
  43. node* poprzedni = lista->head;
  44. temp->a = rand() % 10000;
  45. temp->b = 'T';
  46. temp->next = NULL;
  47. temp->key = n;
  48. if (lista->head == NULL)
  49. {
  50. lista->head = temp;
  51. lista->tail = temp;
  52.  
  53. temp = NULL;
  54. }
  55. else
  56. {
  57. if (lista->head->key == temp->key)
  58. {
  59. cout << "Node with function argument n already exists" << endl;
  60. return;
  61. }
  62.  
  63. while(poprzedni->next != NULL)
  64. {
  65. if(poprzedni->next->key == temp->key)
  66. {
  67. cout << "Node with function argument n already exists" << endl;
  68. return;
  69. }
  70. if(poprzedni->next->key > temp->key)
  71. {
  72. break;
  73. }
  74. poprzedni = poprzedni->next;
  75. }
  76. temp->key = n;
  77. temp->next = poprzedni->next;
  78. poprzedni->next = temp;
  79. }
  80.  
  81.  
  82. }
  83. void wstaw(list* lista, int n)
  84. {
  85. node *check = new node;
  86. node* poprzedni = lista->head;
  87. check = lista->head;
  88. int tempo;
  89. srand(std::time(0));
  90. for (int i = 0; i < n; i++)
  91. {
  92. tempo = rand() % 100098 - 99;
  93. stworz(lista, tempo);
  94. while (poprzedni->next != NULL)
  95. {
  96. if (poprzedni->next->key == check->key)
  97. {
  98. cout << "Node with function argument n already exists" << endl;
  99. return;
  100. }
  101. if (poprzedni->next->key > check->key)
  102. {
  103. break;
  104. }
  105. poprzedni = poprzedni->next;
  106. }
  107. }
  108. }
  109. void display(list* lista)
  110. {
  111. node *temp = new node;
  112. temp = lista->head;
  113. while (temp != NULL)
  114. {
  115. cout << temp->key << endl;
  116. temp = temp->next;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement