110 lines
2.7 KiB
C
110 lines
2.7 KiB
C
#include "a_station.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
struct station* create_station(){
|
|
struct station* station = calloc(1,sizeof(struct station));
|
|
station->tracks = calloc(STATION_SIZE, sizeof(struct car*));
|
|
station->capacity = STATION_SIZE;
|
|
return station;
|
|
}
|
|
|
|
void destroy_station(struct station* station){
|
|
for (int i = 0 ; i< station->capacity; i++){
|
|
struct car* start = station->tracks[i];
|
|
struct car* this = start;
|
|
while(this != NULL){
|
|
this = start->next;
|
|
free(start);
|
|
start=this;
|
|
}
|
|
free(station->tracks[i]);
|
|
}
|
|
free(station);
|
|
}
|
|
|
|
int select_track(struct station* station, const char* target){
|
|
int c;
|
|
int hash = station->capacity;
|
|
char str[36];
|
|
char *pstr = str;
|
|
strcpy(str,target);
|
|
while ((c = *pstr++)){
|
|
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
|
|
|
return hash;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void add_target_capacity(struct station* station,const char* target, int capacity){
|
|
|
|
int track = select_track(station,target);
|
|
struct car* start = station->tracks[track];
|
|
if(start == NULL){
|
|
|
|
strcpy(start->value,target);
|
|
start->capacity = capacity;
|
|
start->next = NULL;
|
|
|
|
|
|
|
|
}else{
|
|
while(start->next != NULL){
|
|
if(strcmp(start->value,target)==0){
|
|
start->capacity = capacity;
|
|
}
|
|
start = start->next;
|
|
if(start->next == NULL){
|
|
struct car* this =(struct car*)malloc(sizeof(struct car));
|
|
strcpy(this->value,target);
|
|
this->capacity = capacity;
|
|
this->next = NULL;
|
|
start->next = this;
|
|
free(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
int get_target_capacity(struct station* station,const char* target){
|
|
int track = select_track(station,target);
|
|
struct car* start = station->tracks[track];
|
|
while(start->next != NULL){
|
|
if(strcmp(start->value,target)== 0){
|
|
return start->capacity;
|
|
}
|
|
start = start->next;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int count_targets(struct station* station){
|
|
int count=0;
|
|
for(int i = 0;i < station->capacity;i++){
|
|
struct car* start = station->tracks[i];
|
|
while(start->next != NULL){
|
|
count++;
|
|
start = start->next;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int count_capacity(struct station* station){
|
|
int count=0;
|
|
for(int i = 0;i< station->capacity;i++){
|
|
struct car* start = station->tracks[i];
|
|
while(start->next != NULL){
|
|
count =+ start->capacity;
|
|
start = start->next;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
|