This commit is contained in:
Maryna Kravtsova 2020-11-07 17:02:09 +01:00
parent 3734bd0fdb
commit 4daaab9142
2 changed files with 14 additions and 10 deletions

View File

@ -6,11 +6,11 @@
struct car* add_car(struct car* first,const char* target) { struct car* add_car(struct car* first,const char* target) {
struct car* newcar = calloc(1, sizeof(struct car)); struct car* newcar = calloc(1, sizeof(struct car));
if(target == NULL){ if(target == NULL){
return 0; return 0;
} }
//if list is empty, then add newcar
if(first == NULL){ if(first == NULL){
strcpy(newcar->value, target); strcpy(newcar->value, target);
newcar->next = NULL; newcar->next = NULL;
@ -19,9 +19,13 @@ struct car* add_car(struct car* first,const char* target) {
strcpy(newcar->value,target); strcpy(newcar->value,target);
newcar->next = NULL; newcar->next = NULL;
struct car *this = first; struct car *this = first;
//find the end of linked list
while(this->next != NULL){ while(this->next != NULL){
this = this->next; this = this->next;
} }
//add target in the end
this->next = newcar; this->next = newcar;
return first; return first;
} }
@ -43,7 +47,7 @@ struct car* clear_train(struct car* first, const char* target) {
if(first == NULL){ if(first == NULL){
return 0; return 0;
} }
//if there is only one element in linked list, than check if target and value are the same
if(first->next == NULL){ if(first->next == NULL){
int result = strcmp(first->value, target); int result = strcmp(first->value, target);
if(result == 0){ if(result == 0){
@ -55,9 +59,11 @@ struct car* clear_train(struct car* first, const char* target) {
} }
} }
//check all the elemets in linked list
struct car* prev = first; struct car* prev = first;
while(prev->next != NULL){ while(prev->next != NULL){
int x = strcmp(first->value, target); int x = strcmp(first->value, target);
//if the first element and target are the same
if(x == 0){ if(x == 0){
prev = first->next; prev = first->next;
free(first); free(first);
@ -65,6 +71,7 @@ struct car* clear_train(struct car* first, const char* target) {
} }
x = strcmp(prev->next->value, target); x = strcmp(prev->next->value, target);
//else check all other elements
if(x == 0){ if(x == 0){
struct car* third = prev->next->next; struct car* third = prev->next->next;
free(prev->next); free(prev->next);

View File

@ -1,4 +1,4 @@
#include "a_station.h" #include "a_station.h"i
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -43,7 +43,7 @@ void add_target_capacity(struct station* station,const char* target, int capacit
struct car* start = station->tracks[track]; struct car* start = station->tracks[track];
while(start != NULL){ while(start != NULL){
int x = strcmp(start->value, target); int x = strcmp(start->value, target);
//if the "wagon" track is similar to the target, then increment capacity //if the "wagon" track and the target are the same, then increment capacity
if(x == 0){ if(x == 0){
start->capacity = start->capacity + capacity; start->capacity = start->capacity + capacity;
return; return;
@ -58,9 +58,6 @@ void add_target_capacity(struct station* station,const char* target, int capacit
new->capacity = capacity; new->capacity = capacity;
new->next = start; new->next = start;
station->tracks[track] = new; station->tracks[track] = new;
/*if(new->next){
return;
}*/
} }
return; return;
@ -74,7 +71,7 @@ int get_target_capacity(struct station* station,const char* target){
struct car* start = station->tracks[track]; struct car* start = station->tracks[track];
while(start != NULL){ while(start != NULL){
int x = strcmp(start->value, target); int x = strcmp(start->value, target);
//if the target is similar to "wagon", than get the capacity //if the "wagon" and the track are the same, than get the capacity
if(x == 0){ if(x == 0){
return start->capacity; return start->capacity;
} }
@ -97,7 +94,7 @@ int count_targets(struct station* station){
int count_capacity(struct station* station){ int count_capacity(struct station* station){
int capacity = 0; int capacity = 0;
for(int i = 0; i < STATION_SIZE; i++){ for(int i = 0; i < STATION_SIZE; i++){
//get one linked list and count there capacity //get one linked list and count capacity there
struct car* this = station->tracks[i]; struct car* this = station->tracks[i];
if(this != NULL){ if(this != NULL){
capacity = capacity + this->capacity; capacity = capacity + this->capacity;