#include #include #include struct Car // struktura elementu { char number[32]; char model[64]; }; typedef struct Car Car; struct ListElement //struktura elementu listy (dodanie wskaznika na nastepny element) { Car car; struct ListElement* next; }; typedef struct ListElement ListElement; struct List // struktura listy (wskaznik na element) { ListElement* head; }; typedef struct List List; void pushFront(List* list, const char* number, const char* model) // dodawanie na poczatek listy { ListElement* element = (ListElement*)malloc(sizeof(ListElement)); strcpy(element->car.number, number); strcpy(element->car.model, model); element->next = list->head; list->head = element; } void pushBack(List* list, const char* number, const char* model) // dodawanie na koniec listy { ListElement* current = list->head; ListElement* element = (ListElement*)malloc(sizeof(ListElement)); element->next = NULL; strcpy(element->car.number, number); strcpy(element->car.model, model); if (list->head != 0) // moze byc (current != 0) { while (current->next != 0) { current = current->next; } current->next = element; } else list->head = element; } void listOfElements(List* list) // wylistowanie elementow { ListElement* current = list->head; while (current != 0) { printf("%s %sn", current->car.number, current->car.model); current = current->next; } } void popFront(List* list) // usuwanie z poczatku listy { ListElement* current = list->head; if (current != 0) { list->head = current->next; free(current); } } void popBack(List* list) // usuwanie z konca listy { ListElement* current = list->head; if (current != 0) { if (current->next != 0) { while (current->next->next != 0) { current = current->next; } free(current->next); current->next = NULL; } else { free(list->head); list->head = NULL; } } } int getNumberOfElements(List* list) // zwracanie liczby elementow { int number = 0; ListElement* current = list->head; while (current != 0) { number++; current = current->next; } return number; } ListElement* getAtIndex(List* list, int index) // zwracanie indexu(adresu do elementu) { int i = 0; ListElement* current = list->head; while (current != 0) { if (i++ == index) { return current; } current = current->next; } return 0; } void insert(List* list, int index, const char* number, const char* model) // wstawianie przed wybranym elementem { ListElement* current = list->head; ListElement* neighbor = getAtIndex(list, index); // pobranie elementu przed ktorym ma byc wstawiony nowy if (current == neighbor) { pushFront(list, number, model); } else { while (current->next != neighbor) { current = current->next; } current->next = (ListElement*)malloc(sizeof ListElement); strcpy(current->next->car.number, number); strcpy(current->next->car.model, model); current->next->next = neighbor; } } void remove(List* list, int index) // usuwanie wybranego elementu { ListElement* current = list->head; ListElement* neighbor = getAtIndex(list, index); if (current == neighbor) { popFront(list); } else { while (current->next != neighbor) { current = current->next; } current->next = neighbor->next; free(neighbor); } } void clear(List* list) // czyszczenie listy { ListElement* current = list->head; while (current != 0) { printf("Usuwam %s %sn", current->car.number, current->car.model); ListElement* p = current->next; free(current); current = p; } } int main(int argc, char** argv) { List list = { 0 }; // zerowanie head'a (wskaznika listy) int number; // zmienna dla liczby elementow pushFront(&list, "ABC 111", "Ford 1000"); pushFront(&list, "ABC 222", "Ford 1001"); pushFront(&list, "ABC 333", "Ford 1002"); pushFront(&list, "ABC 444", "Ford 1003"); pushFront(&list, "ABC 555", "Ford 1004"); pushFront(&list, "ABC 666", "Ford 1005"); pushFront(&list, "ABC 777", "Ford 1006"); pushFront(&list, "ABC ABC", "Ford 1007"); listOfElements(&list); number = getNumberOfElements(&list); printf("nLiczba elementow listy: %dnn", number); pushBack(&list, "ABC DEF", "Ford 1008"); listOfElements(&list); number = getNumberOfElements(&list); printf("nLiczba elementow listy: %dnn", number); insert(&list, 5, "DDD DDD", "Ford 1008"); insert(&list, 0, "EEE EEE", "Ford 1009"); //popBack(&list); listOfElements(&list); number = getNumberOfElements(&list); printf("nLiczba elementow listy: %dnn", number); //popFront(&list); remove(&list, 5); remove(&list, 0); listOfElements(&list); number = getNumberOfElements(&list); printf("nLiczba elementow listy: %dnn", number); clear(&list); return 0; }