Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3.  
  4. #define MAXLEN 30
  5.  
  6. struct Data {
  7. char firstname[MAXLEN];
  8. char lastname[MAXLEN];
  9. int year;
  10. };
  11.  
  12. struct Element {
  13. struct Data data;
  14. struct Element *next;
  15. struct Element *prev;
  16. };
  17.  
  18. struct List {
  19. struct Element* begin;
  20. struct Element* end;
  21. int size;
  22. };
  23.  
  24. // Funkcja od Bolka - dodaje na koniec
  25. struct Element* AddEnd(struct List* list, struct Data data) {
  26.  
  27. struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
  28. if (nel == NULL)
  29. return NULL;
  30. nel->data = data;
  31.  
  32. if (list->end == NULL) {
  33. list->begin = nel;
  34. nel->prev = NULL;
  35. }
  36. else
  37. {
  38. list->end->next = nel;
  39. nel->prev = list->end;
  40. }
  41. list->end = nel;
  42. nel->next = NULL;
  43.  
  44. list->size++;
  45. return nel;
  46. }
  47.  
  48. // Funkcja od Bolka - usuwa element
  49. void Remove(struct List* list, struct Element* pEl) {
  50.  
  51. if (pEl->prev == NULL)
  52. list->begin = pEl->next;
  53. else
  54. pEl->prev->next = pEl->next;
  55.  
  56. if (pEl->next == NULL)
  57. list->end = pEl->prev;
  58. else
  59. pEl->next->prev = pEl->prev;
  60. free(pEl);
  61. list->size--;
  62. }
  63.  
  64. // Funkcja od Bolka - dodaje po elemencie
  65. struct Element* AddAfter(struct List* list, struct Element* pEl, struct Data data) {
  66.  
  67. struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
  68. if (nel == NULL)
  69. return NULL;
  70.  
  71. nel->data = data;
  72. nel->next = pEl->next;
  73. nel->prev = pEl;
  74. pEl->next = nel;
  75. if (nel->next == NULL)
  76. list->end = nel;
  77. else
  78. nel->next->prev = nel;
  79. list->size++;
  80. return nel;
  81. }
  82.  
  83. // Funkcja od Bolka - dodaje przed elementem
  84. struct Element* AddBefore(struct List* list, struct Element* pEl, struct Data data) {
  85.  
  86. struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
  87. if (nel == NULL)
  88. return NULL;
  89.  
  90. nel->data = data;
  91. nel->prev = pEl->prev;
  92. nel->next = pEl;
  93. pEl->prev = nel;
  94. if (nel->prev == NULL)
  95. list->begin = nel;
  96. else
  97. nel->prev->next = nel;
  98. list->size++;
  99. return nel;
  100. }
  101.  
  102. // Nasza funkcja
  103. void removeBornBefore(struct List *lista, int rok) {
  104. struct Element *element = lista->begin;
  105. struct Element *doUsuniecia;
  106. while (element != NULL) {
  107. if (element->data.year < rok) {
  108. doUsuniecia = element;
  109. element = element->next;
  110. Remove(lista, doUsuniecia);
  111. } else {
  112. element = element->next;
  113. }
  114. }
  115. }
  116.  
  117. void Print(struct List *lista) {
  118. struct Element *obecny;
  119. printf("-----------------\n");
  120. printf("Elementow na liscie: %d\n", lista->size);
  121. for (obecny = lista->begin; obecny != NULL; obecny = obecny->next) {
  122. printf("%s %s, rok urodzenia: %d\n", obecny->data.firstname, obecny->data.lastname, obecny->data.year);
  123. }
  124. printf("-----------------\n");
  125. }
  126.  
  127. int main() {
  128. // Jakies tam dane...
  129. struct List lista = {NULL, NULL, 0};
  130. struct Data dane = {"Jan", "Kowalski", 1984};
  131. AddEnd(&lista, dane);
  132. dane = (struct Data) {"Anna", "Nowak", 1992};
  133. AddEnd(&lista, dane);
  134. dane = (struct Data) {"Stefan", "Czarny", 1987};
  135. AddEnd(&lista, dane);
  136. dane = (struct Data) {"Witold", "Zagorski", 1977};
  137. AddEnd(&lista, dane);
  138. dane = (struct Data) {"Maciej", "Grzelak", 1986};
  139. AddEnd(&lista, dane);
  140. // Wydrukuj liste
  141. Print(&lista);
  142. // Usun osoby urodzone przed 1985 rokiem
  143. removeBornBefore(&lista, 1985);
  144. // Wydrukuj nowa liste
  145. Print(&lista);
  146. return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement