Browse Source

Added remove_inside_List function. Experimental user removal.

merge-requests/2/head
Aethor 4 years ago
parent
commit
55126c9ca6
  1. 7
      Elevator/Elevator.c
  2. 63
      List/List.c
  3. 1
      List/List.h

7
Elevator/Elevator.c

@ -40,6 +40,12 @@ void add_passenger_Elevator(THIS(Elevator), Passenger * passenger){
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 num;
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 call from user : %d\n", next_call);
printf("\n\n");
fflush(stdout);
}
this->set_floor(this, this->target_floor);
}

63
List/List.c

@ -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
* @param this THIS(List)
@ -176,22 +206,23 @@ List *_init_List(){
l->custom_free = free;
LINK_ALL(List, l,
get_head,
get_tail,
get_size,
get_head_data,
get_tail_data,
get_element,
get_element_data,
set_custom_free,
insert_inside,
insert_tail,
insert_head,
clear_custom,
clear,
remove_head,
remove_tail,
contains
get_head,
get_tail,
get_size,
get_head_data,
get_tail_data,
get_element,
get_element_data,
set_custom_free,
remove_inside,
insert_inside,
insert_tail,
insert_head,
clear_custom,
clear,
remove_head,
remove_tail,
contains
)
return l;
}

1
List/List.h

@ -30,6 +30,7 @@ struct o_List {
PUBLIC int (*get_size)(_THIS(List));
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 (*insert_inside)(_THIS(List), void * data, size_t data_size, int index);

Loading…
Cancel
Save