Delete du6/station.c
This commit is contained in:
parent
4889db4ff5
commit
3c2eb61482
147
du6/station.c
147
du6/station.c
@ -1,147 +0,0 @@
|
|||||||
#include "station.h"
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
struct station* create_station() {
|
|
||||||
struct station* st = (struct station*)malloc(sizeof(struct station));
|
|
||||||
if (st == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
|
||||||
if (st->tracks == NULL) {
|
|
||||||
free(st);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->track_count = STATION_SIZE;
|
|
||||||
return st;
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy_station(struct station* station) {
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (station == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < station->track_count; i++) {
|
|
||||||
struct car* current = station->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
struct car* next = current->next;
|
|
||||||
free(current);
|
|
||||||
current = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(station->tracks);
|
|
||||||
free(station);
|
|
||||||
}
|
|
||||||
|
|
||||||
int select_track(struct station* station, const char* target) {
|
|
||||||
unsigned long hash = 5381;
|
|
||||||
const unsigned char* ptr;
|
|
||||||
|
|
||||||
if (station == NULL || station->track_count <= 0 || target == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = (const unsigned char*)target;
|
|
||||||
while (*ptr != '\0') {
|
|
||||||
hash = ((hash << 5) + hash) + *ptr;
|
|
||||||
ptr++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (int)(hash % (unsigned long)station->track_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_target_capacity(struct station* station, const char* target, int capacity) {
|
|
||||||
int index;
|
|
||||||
struct car* current;
|
|
||||||
|
|
||||||
if (station == NULL || station->tracks == NULL || target == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = select_track(station, target);
|
|
||||||
current = station->tracks[index];
|
|
||||||
|
|
||||||
while (current != NULL) {
|
|
||||||
if (strcmp(current->value, target) == 0) {
|
|
||||||
current->capacity += capacity;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
current = (struct car*)malloc(sizeof(struct car));
|
|
||||||
if (current == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(current->value, target, TARGET_SIZE - 1);
|
|
||||||
current->value[TARGET_SIZE - 1] = '\0';
|
|
||||||
current->capacity = capacity;
|
|
||||||
current->next = station->tracks[index];
|
|
||||||
station->tracks[index] = current;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_target_capacity(struct station* station, const char* target) {
|
|
||||||
int index;
|
|
||||||
struct car* current;
|
|
||||||
|
|
||||||
if (station == NULL || station->tracks == NULL || target == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = select_track(station, target);
|
|
||||||
current = station->tracks[index];
|
|
||||||
|
|
||||||
while (current != NULL) {
|
|
||||||
if (strcmp(current->value, target) == 0) {
|
|
||||||
return current->capacity;
|
|
||||||
}
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int count_targets(struct station* station) {
|
|
||||||
int i;
|
|
||||||
int count = 0;
|
|
||||||
|
|
||||||
if (station == NULL || station->tracks == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < station->track_count; i++) {
|
|
||||||
struct car* current = station->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
count++;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
int count_capacity(struct station* station) {
|
|
||||||
int i;
|
|
||||||
int sum = 0;
|
|
||||||
|
|
||||||
if (station == NULL || station->tracks == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < station->track_count; i++) {
|
|
||||||
struct car* current = station->tracks[i];
|
|
||||||
while (current != NULL) {
|
|
||||||
sum += current->capacity;
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user