usaa25/du5/a_station.c

73 lines
1.5 KiB
C

#include "a_station.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.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){
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target){
int hash = 0;
for (int i = 0; i < strlen(target); i++)
{ hash += target[i];
}
hash = hash %station->track_count;
return hash;
}
void add_target_capacity(struct station* station,const char* target, int capacity){
struct car* ptr = *station->tracks;
int count = 0;
int res = select_track(station, target);
while(ptr && ptr->next){
if(count == res){
break;
}
ptr = ptr->next;
count++;
}
if(count < res){
struct car* next = (struct car*)calloc(1, sizeof(struct car));
strcpy(next->value, target);
next->capacity = capacity;
if(ptr){
ptr->next = next;
return;}
else{
*station->tracks = next;
return;
}
}
ptr->capacity += capacity;
return;
}
int get_target_capacity(struct station* station,const char* target){
return 0;
}
int count_targets(struct station* station){
return 0;
}
int count_capacity(struct station* station){
return 0;
}