refresh
This commit is contained in:
		
							parent
							
								
									73d906603a
								
							
						
					
					
						commit
						28d2e7604d
					
				@ -2,3 +2,90 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include "list_ops.h"
 | 
					#include "list_ops.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list_t *new_list(size_t length, list_element_t elements[]) {
 | 
				
			||||||
 | 
					    list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
 | 
				
			||||||
 | 
					    if (!list) { 
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    list->length = length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < length; i++) {
 | 
				
			||||||
 | 
					        list->elements[i] = elements[i];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return list;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list_t *new_list(size_t length, list_element_t elements[]) {
 | 
				
			||||||
 | 
					    list_t *result = malloc(sizeof(list_t) + sizeof(list_element_t) * length);
 | 
				
			||||||
 | 
					    if (result == NULL) {
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    result->length = length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (elements != NULL) {
 | 
				
			||||||
 | 
					        for (size_t idx = 0; idx < length; ++idx) {
 | 
				
			||||||
 | 
					            result->elements[idx] = elements[idx];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void delete_list(list_t *lst) {
 | 
				
			||||||
 | 
					    if (lst != NULL) {
 | 
				
			||||||
 | 
					        free(lst);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					size_t length_list(list_t *lst) {
 | 
				
			||||||
 | 
					    return (lst == NULL) ? 0 : lst->length;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list_t *append_list(list_t *first, list_t *second) {
 | 
				
			||||||
 | 
					    size_t a = first ? first->length : 0;
 | 
				
			||||||
 | 
					    size_t b = second ? second->length : 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    size_t total = a + b;
 | 
				
			||||||
 | 
					    list_t *joined = malloc(sizeof(list_t) + total * sizeof(list_element_t));
 | 
				
			||||||
 | 
					    if (!joined) { 
 | 
				
			||||||
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    joined->length = total;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < a; i++) {
 | 
				
			||||||
 | 
					        joined->elements[i] = first->elements[i];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (size_t j = 0; j < b; j++) {
 | 
				
			||||||
 | 
					        joined->elements[a + j] = second->elements[j];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return joined;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
 | 
				
			||||||
 | 
					    size_t count = 0;
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < list->length; i++) {
 | 
				
			||||||
 | 
					        if (filter(list->elements[i])) {
 | 
				
			||||||
 | 
					            count++;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    list_t *new = malloc(sizeof(list_t) + count * sizeof(list_element_t));
 | 
				
			||||||
 | 
					    if (!new) { 
 | 
				
			||||||
 | 
					    return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    new->length = count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    size_t j = 0;
 | 
				
			||||||
 | 
					    for (size_t i = 0; i < list->length; i++) {
 | 
				
			||||||
 | 
					        if (filter(list->elements[i])) {
 | 
				
			||||||
 | 
					            new->elements[j++] = list->elements[i];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return new;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user