From eef93d38acea1d8dcd18da8735d0b2867827e5ed Mon Sep 17 00:00:00 2001 From: Marat Izmailov Date: Thu, 7 Nov 2024 20:39:07 +0000 Subject: [PATCH] Add cv6/a_station.c --- cv6/a_station.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 cv6/a_station.c diff --git a/cv6/a_station.c b/cv6/a_station.c new file mode 100644 index 0000000..1024d9b --- /dev/null +++ b/cv6/a_station.c @@ -0,0 +1,95 @@ + +#include +#include +#include +#include "a_station.h" + +// Функция для создания новой станции +struct station* create_station() { + struct station* station = calloc(1, sizeof(struct station)); + station->tracks = calloc(STATION_SIZE, sizeof(struct car*)); + station->track_count = STATION_SIZE; + return station; +} + +// Функция для вычисления индекса колеи на основе названия станции +int select_track(struct station* station, const char* target) { + int hash = 0; + while (*target) { + hash += (unsigned char)(*target++); + } + return hash % station->track_count; +} + +// Функция для добавления записи о вместимости станции +void add_target_capacity(struct station* station, const char* target, int capacity) { + int track = select_track(station, target); + struct car* current = station->tracks[track]; + struct car* prev = NULL; + + // Поиск станции в цепочке + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + current->capacity += capacity; // Обновляем вместимость + return; + } + prev = current; + current = current->next; + } + + // Если станции не существует, создаем новый элемент + struct car* new_car = malloc(sizeof(struct car)); + new_car->capacity = capacity; + strcpy(new_car->value, target); + new_car->next = NULL; + + // Добавляем новый элемент в цепочку + if (prev == NULL) { + station->tracks[track] = new_car; + } else { + prev->next = new_car; + } +} + +// Функция для поиска вместимости станции по названию +int find_capacity(struct station* station, const char* target) { + int track = select_track(station, target); + struct car* current = station->tracks[track]; + + // Поиск элемента в цепочке + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + return current->capacity; + } + current = current->next; + } + return -1; // Если станция не найдена, возвращаем -1 +} + +// Функция для освобождения памяти, выделенной для станции +void destroy_station(struct station* station) { + for (int i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + struct car* temp = current; + current = current->next; + free(temp); + } + } + free(station->tracks); + free(station); +} + +// Функция для вывода информации о станции +void print_station(struct station* station) { + printf("station>>>\n"); + for (int i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + printf("%s %d -> ", current->value, current->capacity); + current = current->next; + } + printf("NULL\n"); + } + printf("<<