Update cv6/a_station.c
This commit is contained in:
parent
7c8e8584b8
commit
b699c46bc7
146
cv6/a_station.c
146
cv6/a_station.c
@ -1,95 +1,95 @@
|
|||||||
|
#include "a_station.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "a_station.h"
|
|
||||||
|
|
||||||
// Функция для создания новой станции
|
// Создаёт пустую станцию
|
||||||
struct station* create_station() {
|
struct station* create_station() {
|
||||||
struct station* station = calloc(1, sizeof(struct station));
|
struct station* station = (struct station*)calloc(1, sizeof(struct station));
|
||||||
station->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
||||||
station->track_count = STATION_SIZE;
|
station->track_count = STATION_SIZE;
|
||||||
return station;
|
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) {
|
void destroy_station(struct station* station) {
|
||||||
for (int i = 0; i < station->track_count; i++) {
|
for (int i = 0; i < station->track_count; i++) {
|
||||||
struct car* current = station->tracks[i];
|
struct car* current = station->tracks[i];
|
||||||
while (current != NULL) {
|
while (current) {
|
||||||
struct car* temp = current;
|
struct car* to_free = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
free(temp);
|
free(to_free);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(station->tracks);
|
free(station->tracks);
|
||||||
free(station);
|
free(station);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Функция для вывода информации о станции
|
// Хеш-функция для выбора пути на основе названия конечной станции
|
||||||
void print_station(struct station* station) {
|
int select_track(struct station* station, const char* target) {
|
||||||
printf("station>>>\n");
|
int hash = 0;
|
||||||
for (int i = 0; i < station->track_count; i++) {
|
for (int i = 0; target[i] != '\0'; i++) {
|
||||||
struct car* current = station->tracks[i];
|
hash = (hash + target[i]) % station->track_count;
|
||||||
while (current != NULL) {
|
}
|
||||||
printf("%s %d -> ", current->value, current->capacity);
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Добавляет количество пассажиров для указанной станции
|
||||||
|
void add_target_capacity(struct station* station, const char* target, int capacity) {
|
||||||
|
int index = select_track(station, target);
|
||||||
|
struct car* current = station->tracks[index];
|
||||||
|
|
||||||
|
while (current) {
|
||||||
|
if (strcmp(current->value, target) == 0) {
|
||||||
|
current->capacity += capacity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
printf("NULL\n");
|
|
||||||
|
struct car* new_car = (struct car*)malloc(sizeof(struct car));
|
||||||
|
strncpy(new_car->value, target, TARGET_SIZE - 1);
|
||||||
|
new_car->value[TARGET_SIZE - 1] = '\0';
|
||||||
|
new_car->capacity = capacity;
|
||||||
|
new_car->next = station->tracks[index];
|
||||||
|
station->tracks[index] = new_car;
|
||||||
}
|
}
|
||||||
printf("<<<station\n");
|
|
||||||
|
// Получает количество пассажиров для указанной станции
|
||||||
|
int get_target_capacity(struct station* station, const char* target) {
|
||||||
|
int index = select_track(station, target);
|
||||||
|
struct car* current = station->tracks[index];
|
||||||
|
|
||||||
|
while (current) {
|
||||||
|
if (strcmp(current->value, target) == 0) {
|
||||||
|
return current->capacity;
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Считает количество конечных станций
|
||||||
|
int count_targets(struct station* station) {
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < station->track_count; i++) {
|
||||||
|
struct car* current = station->tracks[i];
|
||||||
|
while (current) {
|
||||||
|
count++;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Считает общую вместимость всех записей
|
||||||
|
int count_capacity(struct station* station) {
|
||||||
|
int total_capacity = 0;
|
||||||
|
for (int i = 0; i < station->track_count; i++) {
|
||||||
|
struct car* current = station->tracks[i];
|
||||||
|
while (current) {
|
||||||
|
total_capacity += current->capacity;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return total_capacity;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user