usaa20/cv6/a_station.c

112 lines
2.7 KiB
C
Raw Normal View History

2020-11-10 17:48:19 +00:00
#include "a_station.h"
#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;
2020-11-10 18:44:27 +00:00
2020-11-10 17:48:19 +00:00
return station;
}
2020-11-10 18:44:27 +00:00
/*unsigned long hash(char *key, size_t len){
unsigned long hash;
int i;
2020-11-10 17:48:19 +00:00
for(hash = i = 0; i < len; ++i)
2020-11-10 18:44:27 +00:00
{ hash += key[i];
2020-11-10 17:48:19 +00:00
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
2020-11-10 18:44:27 +00:00
}*/
2020-11-10 17:48:19 +00:00
void destroy_station(struct station* station){
2020-11-10 19:21:28 +00:00
//struct car* del =prev;
for(int i=0;i>station->track_count;i++){
2020-11-10 19:18:51 +00:00
struct car* prev = station->tracks[i];
while (prev!=NULL)
{
2020-11-10 19:21:28 +00:00
struct car* del =prev;
prev=del->next;
2020-11-10 19:18:51 +00:00
free(del);
}
}
free(station->tracks);
free(station);
}
2020-11-10 17:48:19 +00:00
int select_track(struct station* station, const char* target){
2020-11-10 19:11:19 +00:00
unsigned long hash=0;
for(int i=0;target[i]!='\0';i++){
hash = ((hash << 5) + hash) + target[i]; /* hash * 33 + c */
2020-11-10 18:57:03 +00:00
}
2020-11-10 19:11:19 +00:00
hash = hash%station->track_count;
return hash;
2020-11-10 18:44:27 +00:00
}
2020-11-10 17:48:19 +00:00
void add_target_capacity(struct station* station,const char* target, int capacity){
2020-11-10 20:54:31 +00:00
int i = select_track(station,target);
2020-11-10 20:47:36 +00:00
struct car* slot = station->tracks[i];
if(station->tracks[i]==NULL){
struct car* newCar = malloc(sizeof(struct car));
strcpy(newCar->value,target);
newCar->capacity=capacity;
newCar->next=slot;
station->tracks[i]=newCar;
}
while (slot!=NULL)
{
if(strcmp(slot->value,target)==0){
slot->capacity +=capacity;
}
slot=slot->next;
}
2020-11-10 17:48:19 +00:00
}
int get_target_capacity(struct station* station,const char* target){
2020-11-10 20:44:50 +00:00
for(int i=0;i<station->track_count;i++){
struct car* newCar= station->tracks[i];
while (newCar->value!=NULL)
{
if(strcmp(newCar->value,target)==0){
return newCar->capacity;
}
newCar=newCar->next;
}
}
2020-11-10 17:48:19 +00:00
return 0;
}
int count_targets(struct station* station){
2020-11-10 20:44:50 +00:00
int counter=0;
for(int i=0;i<station->track_count;i++){
struct car* newCar= station->tracks[i];
while (newCar->value!=NULL)
{
counter+=1;
newCar=newCar->next;
}
}
return counter;
2020-11-10 17:48:19 +00:00
}
int count_capacity(struct station* station){
2020-11-10 20:54:31 +00:00
int counter=0;
2020-11-10 20:50:01 +00:00
for(int i=0;i<station->track_count;i++){
struct car* newCar= station->tracks[i];
while (newCar->value!=NULL)
{
2020-11-10 20:54:31 +00:00
counter;+=1;
2020-11-10 20:50:01 +00:00
newCar=newCar->next;
}
}
2020-11-10 20:54:31 +00:00
return counter;
2020-11-10 17:48:19 +00:00
}