skuska2
This commit is contained in:
		
							parent
							
								
									c3a7ed4b02
								
							
						
					
					
						commit
						0dc835c900
					
				
							
								
								
									
										153
									
								
								cv4/list_ops.c
									
									
									
									
									
								
							
							
						
						
									
										153
									
								
								cv4/list_ops.c
									
									
									
									
									
								
							| @ -3,63 +3,136 @@ | |||||||
| #include<stdlib.h> | #include<stdlib.h> | ||||||
| #include<string.h> | #include<string.h> | ||||||
| 
 | 
 | ||||||
| list_t *new_list(size_t length, list_element_t elements[]){ | list_t* new_list(size_t length, list_element_t elements[]) { | ||||||
|   list_t* list = (list_t*)malloc(sizeof(list_t) + length * sizeof(list_element_t)); |     list_t* list = NULL; | ||||||
|   list->length = length; |     list_t* current = NULL; | ||||||
|   memcpy(list->elements,elements, length*sizeof(list_element_t)); | 
 | ||||||
|   return list; |     for (size_t i = 0; i < length; i++) { | ||||||
|  |         list_t* new_node = (list_t*)malloc(sizeof(list_t)); | ||||||
|  |         new_node->data = elements[i]; | ||||||
|  |         new_node->next = NULL; | ||||||
|  | 
 | ||||||
|  |         if (list == NULL) { | ||||||
|  |             list = new_node; | ||||||
|  |             current = list; | ||||||
|  |         } else { | ||||||
|  |             current->next = new_node; | ||||||
|  |             current = current->next; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return list; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| list_t *append_list(list_t *list1, list_t *list2){ | list_t* append_list(list_t* list1, list_t* list2) { | ||||||
|   return NULL; |     if (list1 == NULL) { | ||||||
|  |         return list2; | ||||||
|  |     } | ||||||
|  |     if (list2 == NULL) { | ||||||
|  |         return list1; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|  |     list_t* current = list1; | ||||||
|  |     while (current->next != NULL) { | ||||||
|  |         current = current->next; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     current->next = list2; | ||||||
|  |     return list1; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // filter list returning only values that satisfy the filter function
 | list_t* filter_list(list_t* list, bool (*filter)(list_element_t)) { | ||||||
| list_t *filter_list(list_t *list, bool (*filter)(list_element_t)){ |     list_t* filtered = NULL; | ||||||
|   return NULL; |     list_t* current = list; | ||||||
|  |     list_t* filtered_current = NULL; | ||||||
| 
 | 
 | ||||||
|  |     while (current != NULL) { | ||||||
|  |         if (filter(current->data)) { | ||||||
|  |             list_t* new_node = (list_t*)malloc(sizeof(list_t)); | ||||||
|  |             new_node->data = current->data; | ||||||
|  |             new_node->next = NULL; | ||||||
|  | 
 | ||||||
|  |             if (filtered == NULL) { | ||||||
|  |                 filtered = new_node; | ||||||
|  |                 filtered_current = filtered; | ||||||
|  |             } else { | ||||||
|  |                 filtered_current->next = new_node; | ||||||
|  |                 filtered_current = filtered_current->next; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         current = current->next; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return filtered; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // returns the length of the list
 | size_t length_list(list_t* list) { | ||||||
| size_t length_list(list_t *list){ |     size_t count = 0; | ||||||
|   return 0; |     list_t* current = list; | ||||||
| 
 |     while (current != NULL) { | ||||||
|  |         count++; | ||||||
|  |         current = current->next; | ||||||
|  |     } | ||||||
|  |     return count; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // return a list of elements whose values equal the list value transformed by
 | list_t* map_list(list_t* list, list_element_t (*map)(list_element_t)) { | ||||||
| // the mapping function
 |     list_t* mapped = NULL; | ||||||
| list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)){ |     list_t* current = list; | ||||||
|   return NULL; |     list_t* mapped_current = NULL; | ||||||
| 
 | 
 | ||||||
|  |     while (current != NULL) { | ||||||
|  |         list_element_t mapped_value = map(current->data); | ||||||
|  |         list_t* new_node = (list_t*)malloc(sizeof(list_t)); | ||||||
|  |         new_node->data = mapped_value; | ||||||
|  |         new_node->next = NULL; | ||||||
|  | 
 | ||||||
|  |         if (mapped == NULL) { | ||||||
|  |             mapped = new_node; | ||||||
|  |             mapped_current = mapped; | ||||||
|  |         } else { | ||||||
|  |             mapped_current->next = new_node; | ||||||
|  |             mapped_current = mapped_current->next; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         current = current->next; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return mapped; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // folds (reduces) the given list from the left with a function
 | list_element_t foldl_list(list_t* list, list_element_t initial, | ||||||
| list_element_t foldl_list(list_t *list, list_element_t initial, |                           list_element_t (*foldl)(list_element_t, list_element_t)) { | ||||||
|                           list_element_t (*foldl)(list_element_t, |     list_element_t result = initial; | ||||||
|                                                   list_element_t)){ |     list_t* current = list; | ||||||
|   list_element_t res=0; |  | ||||||
|   return res; |  | ||||||
| 
 | 
 | ||||||
|  |     while (current != NULL) { | ||||||
|  |         result = foldl(result, current->data); | ||||||
|  |         current = current->next; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return result; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // folds (reduces) the given list from the right with a function
 | list_t* reverse_list(list_t* list) { | ||||||
| list_element_t foldr_list(list_t *list, list_element_t initial, |     list_t* prev = NULL; | ||||||
|                           list_element_t (*foldr)(list_element_t, |     list_t* current = list; | ||||||
|                                                   list_element_t)){ |     list_t* next; | ||||||
|   list_element_t res=0; |  | ||||||
|   return res; |  | ||||||
| 
 | 
 | ||||||
|  |     while (current != NULL) { | ||||||
|  |         next = current->next; | ||||||
|  |         current->next = prev; | ||||||
|  |         prev = current; | ||||||
|  |         current = next; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return prev; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // reverse the elements of the list
 | void delete_list(list_t* list) { | ||||||
| list_t *reverse_list(list_t *list){ |     while (list != NULL) { | ||||||
|   return NULL; |         list_t* temp = list; | ||||||
| 
 |         list = list->next; | ||||||
|  |         free(temp); | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // destroy the entire list
 |  | ||||||
| // list will be a dangling pointer after calling this method on it
 |  | ||||||
| void delete_list(list_t *list){ |  | ||||||
| 
 |  | ||||||
| }  |  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user