Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4.  
  5. struct lista {
  6. int liczba;
  7. struct lista *next;
  8. };
  9.  
  10. lista* create(int value) //tworzenie listy
  11.  
  12. \\MIEJSCE NA TWOJ KOD
  13.  
  14. void wyswietl(lista* head)//wyswietlanie listy
  15. {
  16. lista * p = head;
  17. while (p != NULL)
  18. {
  19. printf("%d->", p->liczba);
  20. p = p->next;
  21. }
  22. }
  23. lista* get_tail(lista* head)//wyswietlanie ostatniego elementu
  24. {
  25. lista * p = head;
  26. if (head == nullptr)
  27. {
  28. printf("Lista jest pusta");
  29. return nullptr;
  30. }
  31. while (true)
  32. {
  33. if (p->next == nullptr)
  34. break;
  35. p = p->next;
  36. }
  37. return p;
  38. }
  39. void add(lista*& root, int value)
  40. {
  41. lista * p = root;
  42. while (true)
  43. {
  44. if (root == nullptr)
  45. {
  46. lista *obj = (lista*)malloc(sizeof(lista));
  47. obj->liczba = value;
  48. obj->next = nullptr;
  49. root = obj;
  50. break;
  51. }
  52. if (p->next == nullptr)
  53. {
  54. lista *obj = (lista*)malloc(sizeof(lista));
  55. obj->liczba = value;
  56. obj->next = nullptr;
  57. p->next = obj;
  58. break;
  59. }
  60. p = p->next;
  61. }
  62. }
  63. lista* find(lista* root, int value)
  64. {
  65. lista * p = root;
  66. if (root == nullptr)
  67. {
  68. printf("Lista jest pusta ");
  69. return nullptr;
  70. }
  71. while (true)
  72. {
  73. if (p->liczba == value)
  74. {
  75. printf("Znaleziono elemenent!\n");
  76. return p;
  77. }
  78. if (p->next == nullptr)
  79. {
  80. printf("Elementu nie znaleziono!");
  81. return nullptr;
  82. }
  83. p = p->next;
  84. }
  85. }
  86. lista* remove(lista*& head, int value) //usuwanie elementu
  87. {
  88. lista *p = head; //pierwszy element
  89. lista *poprzednik = NULL;
  90. while (p != nullptr)
  91. {
  92. if (p->liczba == value)
  93. {
  94. if (p == head) //element jest pierwszy
  95. {
  96. head = p->next;
  97. delete p;
  98. }
  99. else //element jest w srodku albo koncu
  100. {
  101. poprzednik->next = p->next;
  102. delete p;
  103. }
  104. return head;
  105. }
  106. poprzednik = p;
  107. p = p->next;
  108. }
  109.  
  110. printf("Nie ma elementu");
  111. return nullptr;
  112. }
  113. void free_all(lista*& root)
  114. {
  115. while (root != nullptr)
  116. {
  117. const auto del = get_tail(root);
  118. if (del != nullptr)
  119. remove(root, del->liczba);
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement