Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAXLEN 30
  4.  
  5. struct Data {
  6. char firstname[MAXLEN];
  7. char lastname[MAXLEN];
  8. int year;
  9. };
  10.  
  11. struct Element {
  12. struct Data data;
  13. struct Element *next;
  14. struct Element *prev;
  15. };
  16.  
  17. struct List {
  18. struct Element* begin;
  19. struct Element* end;
  20. int size;
  21. };
  22.  
  23. //dodaje na koniec
  24. struct Element* AddEnd(struct List* list, struct Data data)
  25. {
  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. //usuwa element
  49. void Remove(struct List* list, struct Element* pEl)
  50. {
  51.  
  52. if (pEl->prev == NULL)
  53. list->begin = pEl->next;
  54. else
  55. pEl->prev->next = pEl->next;
  56.  
  57. if (pEl->next == NULL)
  58. list->end = pEl->prev;
  59. else
  60. pEl->next->prev = pEl->prev;
  61. free(pEl);
  62. list->size--;
  63. }
  64.  
  65. //dodaje po elemencie
  66. struct Element* AddAfter(struct List* list, struct Element* pEl, struct Data data)
  67. {
  68.  
  69. struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
  70. if (nel == NULL)
  71. return NULL;
  72.  
  73. nel->data = data;
  74. nel->next = pEl->next;
  75. nel->prev = pEl;
  76. pEl->next = nel;
  77. if (nel->next == NULL)
  78. list->end = nel;
  79. else
  80. nel->next->prev = nel;
  81. list->size++;
  82. return nel;
  83. }
  84.  
  85. //dodaje przed elementem
  86. struct Element* AddBefore(struct List* list, struct Element* pEl, struct Data data)
  87. {
  88.  
  89. struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
  90. if (nel == NULL)
  91. return NULL;
  92.  
  93. nel->data = data;
  94. nel->prev = pEl->prev;
  95. nel->next = pEl;
  96. pEl->prev = nel;
  97. if (nel->prev == NULL)
  98. list->begin = nel;
  99. else
  100. nel->prev->next = nel;
  101. list->size++;
  102. return nel;
  103. }
  104.  
  105. void removeBornBefore(struct List *lista, int rok)
  106. {
  107. struct Element *element = lista->begin;
  108. struct Element *doUsuniecia;
  109. while (element != NULL) {
  110. if (element->data.year < rok) {
  111. doUsuniecia = element;
  112. element = element->next;
  113. Remove(lista, doUsuniecia);
  114. } else {
  115. element = element->next;
  116. }
  117. }
  118. }
  119.  
  120. struct Element* FindYoungest(struct List* list)
  121. {
  122. struct Element* temp = list->begin;
  123.  
  124. for(struct Element* p = list->begin; p!= NULL; p = p->next)
  125. {
  126. if(p->data.year > temp->data.year)
  127. {
  128. temp->data = p->data;
  129. if(p->prev == NULL)
  130. list->begin = p->next;
  131. else
  132. p->prev->next = p->next;
  133. if(p->next == NULL)
  134. list->end = p->prev;
  135. else
  136. p->next->prev = p->prev;
  137. free(p);
  138. list->size--;
  139. }
  140. }
  141. return temp;
  142. }
  143.  
  144. void Print(struct List *lista)
  145. {
  146. struct Element *obecny;
  147. printf("-----------------\n");
  148. printf("Elementow na liscie: %d\n", lista->size);
  149. for (obecny = lista->begin; obecny != NULL; obecny = obecny->next) {
  150. printf("%s %s, rok urodzenia: %d\n", obecny->data.firstname, obecny->data.lastname, obecny->data.year);
  151. }
  152. printf("-----------------\n");
  153. }
  154.  
  155. void WysNajmlod(struct List *lista)
  156. {
  157. struct Element *obecny;
  158. printf("Najmlodsza osoba:\n");
  159. printf("%s %s, %d\n", obecny->data.firstname, obecny->data.lastname, obecny->data.year);
  160. printf("-----------------\n");
  161. }
  162.  
  163. int main() {
  164. struct List lista = {NULL, NULL, 0};
  165. struct Data dane = {"Jan", "Kowalski", 1984};
  166. AddEnd(&lista, dane);
  167. dane = (struct Data) {"Anna", "Nowak", 1992};
  168. AddEnd(&lista, dane);
  169. dane = (struct Data) {"Stefan", "Czarny", 1987};
  170. AddEnd(&lista, dane);
  171. dane = (struct Data) {"Witold", "Zagorski", 1977};
  172. AddEnd(&lista, dane);
  173. dane = (struct Data) {"Maciej", "Grzelak", 1986};
  174. AddEnd(&lista, dane);
  175. Print(&lista);
  176.  
  177. struct Element* pEl = FindYoungest(&lista);
  178. if(pEl == NULL)
  179. printf("Nie znaleziono osoby.\n");
  180. else WysNajmlod(&lista);
  181.  
  182.  
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement