usaa20/cv6/a_station.c

107 lines
3.0 KiB
C
Raw Permalink Normal View History

2020-11-07 16:02:09 +00:00
#include "a_station.h"i
2020-11-07 14:14:45 +00:00
#include <stdlib.h>
#include <string.h>
struct station* create_station(){
struct station* station = (struct station*)calloc(1,sizeof(struct station));
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
station->track_count = STATION_SIZE;
return station;
}
void destroy_station(struct station* station){
for(int i = 0; i < station->track_count; i++){
2020-11-07 15:46:07 +00:00
//get one linked list from station
2020-11-07 14:14:45 +00:00
struct car* this = station->tracks[i];
while(this != NULL){
struct car* delete = this;
this = delete->next;
free(delete);
}
}
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target){
unsigned int hash = 0;
for(int i = 0; target[i] != '\0'; i++){
2020-11-07 15:52:33 +00:00
//i used "lose lose" function, because it's simple and clear
2020-11-07 15:46:07 +00:00
hash += target[i];
2020-11-07 14:14:45 +00:00
}
2020-11-07 15:52:33 +00:00
//change hash, so that it will be between 0 and station->track_count
hash = hash % station->track_count;
2020-11-07 14:14:45 +00:00
return hash;
}
void add_target_capacity(struct station* station,const char* target, int capacity){
2020-11-07 15:46:07 +00:00
//find hash
2020-11-07 14:14:45 +00:00
int track = select_track(station, target);
2020-11-07 15:46:07 +00:00
//get right linked list(with help of hash)
2020-11-07 14:14:45 +00:00
struct car* start = station->tracks[track];
while(start != NULL){
int x = strcmp(start->value, target);
2020-11-07 16:02:09 +00:00
//if the "wagon" track and the target are the same, then increment capacity
2020-11-07 14:14:45 +00:00
if(x == 0){
start->capacity = start->capacity + capacity;
return;
}
start = start->next;
}
2020-11-07 15:46:07 +00:00
//if track is empty, create new "wagon"
2020-11-07 15:46:56 +00:00
if(station->tracks[track] == NULL){
2020-11-07 15:46:07 +00:00
struct car* new = calloc(1, sizeof(struct car));
strcpy(new->value, target);
new->capacity = capacity;
new->next = start;
station->tracks[track] = new;
2020-11-07 14:14:45 +00:00
}
return;
}
int get_target_capacity(struct station* station,const char* target){
2020-11-07 15:46:07 +00:00
//find hash
2020-11-07 14:14:45 +00:00
int track = select_track(station, target);
2020-11-07 15:46:07 +00:00
//get right linked list
2020-11-07 14:14:45 +00:00
struct car* start = station->tracks[track];
while(start != NULL){
int x = strcmp(start->value, target);
2020-11-07 16:02:09 +00:00
//if the "wagon" and the track are the same, than get the capacity
2020-11-07 14:14:45 +00:00
if(x == 0){
return start->capacity;
}
start = start->next;
}
return 0;
}
2020-11-07 15:46:07 +00:00
//count all tracks
2020-11-07 14:14:45 +00:00
int count_targets(struct station* station){
int targets = 0;
for(int i = 0; i < STATION_SIZE; i++){
if(station->tracks[i] != NULL){
targets++;
}
}
return targets;
}
2020-11-07 15:46:07 +00:00
//count capacity in all tracks
2020-11-07 14:14:45 +00:00
int count_capacity(struct station* station){
int capacity = 0;
for(int i = 0; i < STATION_SIZE; i++){
2020-11-07 16:02:09 +00:00
//get one linked list and count capacity there
2020-11-07 14:14:45 +00:00
struct car* this = station->tracks[i];
if(this != NULL){
capacity = capacity + this->capacity;
this = this->next;
}
}
return capacity;
}