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

View File

@ -1,4 +1,4 @@
#include "a_station.h"
#include "a_station.h"i
#include <stdlib.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];
while(start != NULL){
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){
start->capacity = start->capacity + capacity;
return;
@ -58,9 +58,6 @@ void add_target_capacity(struct station* station,const char* target, int capacit
new->capacity = capacity;
new->next = start;
station->tracks[track] = new;
/*if(new->next){
return;
}*/
}
return;
@ -74,7 +71,7 @@ int get_target_capacity(struct station* station,const char* target){
struct car* start = station->tracks[track];
while(start != NULL){
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){
return start->capacity;
}
@ -97,7 +94,7 @@ int count_targets(struct station* station){
int count_capacity(struct station* station){
int capacity = 0;
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];
if(this != NULL){
capacity = capacity + this->capacity;