Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #define MAXLEN 30
- struct Data {
- char firstname[MAXLEN];
- char lastname[MAXLEN];
- int year;
- };
- struct Element {
- struct Data data;
- struct Element *next;
- struct Element *prev;
- };
- struct List {
- struct Element* begin;
- struct Element* end;
- int size;
- };
- // Funkcja od Bolka - dodaje na koniec
- struct Element* AddEnd(struct List* list, struct Data data) {
- struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
- if (nel == NULL)
- return NULL;
- nel->data = data;
- if (list->end == NULL) {
- list->begin = nel;
- nel->prev = NULL;
- }
- else
- {
- list->end->next = nel;
- nel->prev = list->end;
- }
- list->end = nel;
- nel->next = NULL;
- list->size++;
- return nel;
- }
- // Funkcja od Bolka - usuwa element
- void Remove(struct List* list, struct Element* pEl) {
- if (pEl->prev == NULL)
- list->begin = pEl->next;
- else
- pEl->prev->next = pEl->next;
- if (pEl->next == NULL)
- list->end = pEl->prev;
- else
- pEl->next->prev = pEl->prev;
- free(pEl);
- list->size--;
- }
- // Funkcja od Bolka - dodaje po elemencie
- struct Element* AddAfter(struct List* list, struct Element* pEl, struct Data data) {
- struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
- if (nel == NULL)
- return NULL;
- nel->data = data;
- nel->next = pEl->next;
- nel->prev = pEl;
- pEl->next = nel;
- if (nel->next == NULL)
- list->end = nel;
- else
- nel->next->prev = nel;
- list->size++;
- return nel;
- }
- // Funkcja od Bolka - dodaje przed elementem
- struct Element* AddBefore(struct List* list, struct Element* pEl, struct Data data) {
- struct Element* nel = (struct Element*)malloc(sizeof(struct Element));
- if (nel == NULL)
- return NULL;
- nel->data = data;
- nel->prev = pEl->prev;
- nel->next = pEl;
- pEl->prev = nel;
- if (nel->prev == NULL)
- list->begin = nel;
- else
- nel->prev->next = nel;
- list->size++;
- return nel;
- }
- // Nasza funkcja
- void removeBornBefore(struct List *lista, int rok) {
- struct Element *element = lista->begin;
- struct Element *doUsuniecia;
- while (element != NULL) {
- if (element->data.year < rok) {
- doUsuniecia = element;
- element = element->next;
- Remove(lista, doUsuniecia);
- } else {
- element = element->next;
- }
- }
- }
- void Print(struct List *lista) {
- struct Element *obecny;
- printf("-----------------\n");
- printf("Elementow na liscie: %d\n", lista->size);
- for (obecny = lista->begin; obecny != NULL; obecny = obecny->next) {
- printf("%s %s, rok urodzenia: %d\n", obecny->data.firstname, obecny->data.lastname, obecny->data.year);
- }
- printf("-----------------\n");
- }
- int main() {
- // Jakies tam dane...
- struct List lista = {NULL, NULL, 0};
- struct Data dane = {"Jan", "Kowalski", 1984};
- AddEnd(&lista, dane);
- dane = (struct Data) {"Anna", "Nowak", 1992};
- AddEnd(&lista, dane);
- dane = (struct Data) {"Stefan", "Czarny", 1987};
- AddEnd(&lista, dane);
- dane = (struct Data) {"Witold", "Zagorski", 1977};
- AddEnd(&lista, dane);
- dane = (struct Data) {"Maciej", "Grzelak", 1986};
- AddEnd(&lista, dane);
- // Wydrukuj liste
- Print(&lista);
- // Usun osoby urodzone przed 1985 rokiem
- removeBornBefore(&lista, 1985);
- // Wydrukuj nowa liste
- Print(&lista);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement