Added remove_inside_List function. Experimental user removal.

This commit is contained in:
Aethor 2018-06-21 19:21:17 +02:00
parent 12c4e1af19
commit 55126c9ca6
3 changed files with 55 additions and 16 deletions

View File

@ -40,6 +40,12 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
pthread_mutex_unlock(&this->mutex_passengers); pthread_mutex_unlock(&this->mutex_passengers);
} }
void remove_passenger_Elevator(THIS(Elevator), Passenger * passenger){
pthread_mutex_lock(&this->mutex_passengers);
this->passengers->remove_inside(this->passengers, passenger, passenger->compare);
pthread_mutex_unlock(&this->mutex_passengers);
}
int get_number_of_passengers_Elevator(THIS(Elevator)){ int get_number_of_passengers_Elevator(THIS(Elevator)){
int num; int num;
pthread_mutex_lock(&this->mutex_passengers); pthread_mutex_lock(&this->mutex_passengers);
@ -127,6 +133,7 @@ void *runnable_Elevator(void * void_this){
printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop); printf("DEBUG : Next passenger stop : %d\n", next_passenger_stop);
printf("DEBUG : Next call from user : %d\n", next_call); printf("DEBUG : Next call from user : %d\n", next_call);
printf("\n\n"); printf("\n\n");
fflush(stdout);
} }
this->set_floor(this, this->target_floor); this->set_floor(this, this->target_floor);
} }

View File

@ -140,6 +140,36 @@ void insert_inside_List(THIS(List), void * data, size_t data_size, int index){
} }
/**
* Remove element with specified data in the list
* @param this THIS(List)
* @param data_to_remove pointer to data you want to remove
* @param compare a compare function, return true or false and takes two void * pointers
* @return true or false, depending on if the element was found in the list
*/
int remove_inside_List(THIS(List), void * data_to_remove, int (*compare)(void*, void*)){
Element* temp_element = this->head;
while(temp_element != NULL){
if(compare(data_to_remove, temp_element->data)){
if(temp_element == this->head){
this->remove_head(this);
}else if (temp_element == this->tail){
this->remove_tail(this);
}else{
if(temp_element->previous != NULL)
temp_element->previous->next = temp_element->next;
if(temp_element->next != NULL)
temp_element->next->previous = temp_element->previous;
DELETE(temp_element);
}
return 1;
}
temp_element = temp_element->next;
}
return 0;
}
/** /**
* Check if data_to_find exist in list * Check if data_to_find exist in list
* @param this THIS(List) * @param this THIS(List)
@ -184,6 +214,7 @@ List *_init_List(){
get_element, get_element,
get_element_data, get_element_data,
set_custom_free, set_custom_free,
remove_inside,
insert_inside, insert_inside,
insert_tail, insert_tail,
insert_head, insert_head,

View File

@ -30,6 +30,7 @@ struct o_List {
PUBLIC int (*get_size)(_THIS(List)); PUBLIC int (*get_size)(_THIS(List));
PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *)); PUBLIC int (*contains)(_THIS(List), void * data_to_find, int (*compare)(void *, void *));
PUBLIC int (*remove_inside)(_THIS(List), void * data_to_remove, int (*compare)(void*, void*));
PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *)); PUBLIC void (*set_custom_free)(_THIS(List), void (*custom_free)(void *));
PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index); PUBLIC void (*insert_inside)(_THIS(List), void * data, size_t data_size, int index);