diff --git a/datastruct/list.c b/datastruct/list.c index 28c5332..001f8ed 100644 --- a/datastruct/list.c +++ b/datastruct/list.c @@ -1,365 +1,6 @@ #include "list.h" - -#ifdef LINK_LIST -bool list_init(plist_t *list) -{ - *list = (plist_t)malloc(sizeof(list_t)); - if(*list == NULL) - { - return false; - } - (*list)->data = (list_data_t)0; - (*list)->prev = *list; - (*list)->next = *list; - return true; -} - -void list_destroy(plist_t *list) -{ - if (*list != NULL) - { - list_clear(*list); - free(*list); - (*list)->prev = NULL; - (*list)->next = NULL; - *list = NULL; - } -} - -bool list_empty(plist_t list) -{ - return (list == NULL || (list->prev == list && list->next == list)) ? true : false; -} - -void list_clear(plist_t list) -{ - if(!list_empty(list)) - { - plist_t p = list->next; - plist_t ptmp = p; - while(p != list) - { - ptmp = p; - p->prev->next = p->next; - p->next->prev = p->prev; - p = p->next; - - // finally free memory - free(ptmp); - ptmp->prev = NULL; - ptmp->next = NULL; - } - } -} - -bool list_insert_head(plist_t list, list_data_t data) -{ - plist_t new_item; - if(list == NULL) - { - return false; - } - - new_item = (plist_t)malloc(sizeof(list_t)); - if(new_item == NULL) - { - return false; - } - new_item->data = data; - - // insert from head - new_item->next = list->next; - new_item->prev = list->next->prev; - list->next->prev = new_item; - list->next = new_item; - - return true; -} - -bool list_insert_tail(plist_t list, list_data_t data) -{ - plist_t new_item; - if(list == NULL) - { - return false; - } - - new_item = (plist_t)malloc(sizeof(list_t)); - if(new_item == NULL) - { - return false; - } - new_item->data = data; - - // insert from tail - new_item->prev = list->prev; - new_item->next = list->prev->next; - list->prev->next = new_item; - list->prev = new_item; - - return true; -} - -bool list_delete(plist_t list, list_data_t data) -{ - if(!list_empty(list)) - { - plist_t p = list; - while(p->next != list) - { - p = p->next; - if(p->data == data) - { - // delete the item - p->prev->next = p->next; - p->next->prev = p->prev; - free(p); - p->prev = NULL; - p->next = NULL; - return true; - } - } - } - return false; -} - -uint32_t list_count(plist_t list) -{ - uint32_t count = 0; - if(!list_empty(list)) - { - plist_t p = list; - while(p->next != list) - { - p = p->next; - count++; - } - } - return count; -} - -void list_traversal_sequence(plist_t list, list_data_disp_t disp) -{ - if(!list_empty(list)) - { - plist_t p = list->next; - while(p != list) - { - disp(p->data); - p = p->next; - } - } -} - -void list_traversal_reversed(plist_t list, list_data_disp_t disp) -{ - if(!list_empty(list)) - { - plist_t p = list->prev; - while(p != list) - { - disp(p->data); - p = p->prev; - } - } -} -#endif - -#if 0 -bool list_init(list_t list) -{ - // list->capacity = 64; - list->capacity = 8; - - list->size = 0; - list->extend_ratio = 2; - list->array = (list_data_t *)malloc(list->capacity * sizeof(list_data_t)); - if (list->array == NULL) - { - return false; - } - return true; -} - -void list_destory(list_t list) -{ - if (list->array != NULL) - { - free(list->array); - } -} - -int list_capacity(list_t list) -{ - return list->capacity; -} - -int list_size(list_t list) -{ - return list->size; -} - -bool list_empty(list_t list) -{ - return list_size(list) == 0 ? true : false; -} - -bool list_insert(list_t list, int index, list_data_t data) -{ - assert(index >= 0 && index <= list_size(list)); - if (list_size(list) == list_capacity(list)) - { - int capacity = list->capacity * list->extend_ratio; - list_data_t * array = (list_data_t*)realloc(list->array, capacity * sizeof(list_data_t)); - if (array == NULL) - { - return false; - } - list->array = array; - list->capacity = capacity; - } - memmove(&list->array[index + 1], &list->array[index], list_size(list) * sizeof(list_data_t)); - list->array[index] = data; - list->size += 1; - return true; -} - -bool list_append(list_t list, list_data_t data) -{ - if (list_size(list) == list_capacity(list)) - { - int capacity = list->capacity * list->extend_ratio; - list_data_t * array = (list_data_t*)realloc(list->array, capacity * sizeof(list_data_t)); - if (array == NULL) - { - return false; - } - list->array = array; - list->capacity = capacity; - } - list->array[list->size] = data; - list->size += 1; - return true; -} - -int list_delete(list_t list, int index) -{ - assert(index >= 0 && index < list_size(list)); - int temp = 0; - temp = list->array[index]; - if (index != list_size(list)) - { - memmove(&list->array[index], &list->array[index + 1], (list_size(list) - 1 - index) * sizeof(list_data_t)); - //for (int i = index; i < list_size(list) - 1; i++) - //{ - // list->array[i] = list->array[i + 1]; - //} - } - list->size -= 1; - return temp; -} - -bool list_clear(list_t list) -{ - memset(list->array, 0, list->size); - list->size = 0; - return true; -} - -list_data_t list_get(list_t list, int index) -{ - assert(index >= -list_size(list) && index < list_size(list)); - return index >= 0 ? list->array[index] : list->array[list_size(list) + index]; -} - -bool list_set(list_t list, int index, list_data_t data) -{ - assert(index >= 0 && index < list_size(list)); - list->array[index] = data; - return true; -} - -void list_print(list_t list) -{ - if (!list_empty(list)) - { - for (int i = 0; i < list_size(list); i++) - { - printf("%2d ", list->array[i]); - } - printf("\n"); - } -} - -void list_test(void) -{ - int i = 0; - - list_data_t data = 0; - struct _list list; - list_init(&list); - - printf("----- append -----\n"); - for (i = 0; i < 18; i++) - { - list_append(&list, i); - list_print(&list); - } - - printf("----- delete -----\n"); - list_delete(&list, 17); - list_print(&list); - - list_delete(&list, 0); - list_print(&list); - - list_delete(&list, 9); - list_print(&list); - - printf("----- clear -----\n"); - list_clear(&list); - printf("----- insert -----\n"); - for (i = 0; i < 18; i++) - { - list_insert(&list, 0, i); - list_print(&list); - } - - printf("----- get -----\n"); - data = list_get(&list, 0); - printf("list[0] = %2d\n", data); - - data = list_get(&list, 17); - printf("list[17] = %2d\n", data); - - data = list_get(&list, 5); - printf("list[5] = %2d\n", data); - - printf("----- like python -----\n"); - data = list_get(&list, -1); - printf("list[-1] = %2d\n", data); - - data = list_get(&list, -5); - printf("list[-5] = %2d\n", data); - - data = list_get(&list, -18); - printf("list[-18] = %2d\n", data); - - printf("----- set -----\n"); - for (i = 0; i < 18; i++) - { - list_set(&list, i, i); - list_print(&list); - } - - list_print(&list); - list_destory(&list); -} - -#endif - bool list_append(struct _list* self, void* obj) { assert(self != NULL); @@ -385,27 +26,9 @@ bool list_append(struct _list* self, void* obj) return true; } -bool list_clear(struct _list* self) +bool list_insert(struct _list* self, int index, void* obj) { - assert(self != NULL); - self->_size = 0; - return true; -} - -bool list_empty(struct _list* self) -{ - assert(self != NULL); - return !self->size(self); -} - -uint32_t list_index(struct _list* self, void* obj) -{ - return 0; -} - -bool list_insert(struct _list* self, uint32_t index, void* obj) -{ - assert(index >= 0 && index < self->size(self)); + assert(index >= 0 && index < (int)self->size(self)); if (self->size(self) == self->_capacity) { @@ -421,7 +44,7 @@ bool list_insert(struct _list* self, uint32_t index, void* obj) uint32_t offset = index * self->_obj_size; uint32_t offset1 = (index + 1) * self->_obj_size; - memmove((char*)self->obj + offset, (char *)self->obj + offset, self->size(self) * self->_obj_size); + memmove((char*)self->obj + offset, (char*)self->obj + offset, self->size(self) * self->_obj_size); self->_size += 1; return true; } @@ -453,7 +76,25 @@ bool list_pop(struct _list* self, int index, void* obj) return true; } -bool list_at(struct _list* self, int index, void *obj) +uint32_t list_index(struct _list* self, void* obj) +{ + return 0; +} + +bool list_remove(struct _list* self, void* obj) +{ + assert(self != NULL); + return true; +} + +bool list_clear(struct _list* self) +{ + assert(self != NULL); + self->_size = 0; + return true; +} + +bool list_get(struct _list* self, int index, void* obj) { assert(self != NULL); assert(obj != NULL); @@ -486,27 +127,10 @@ uint32_t list_size(struct _list* self) return self->_size; } -bool list_remove(struct _list* self, int index) +bool list_empty(struct _list* self) { assert(self != NULL); - assert(index >= (int)(0 - self->size(self)) && index < (int)self->size(self)); - - if (self->empty(self)) - { - return false; - } - - if (index < 0) - { - index += self->size(self); - } - - uint32_t count = self->size(self) - 1 - index; - uint32_t offset = index * self->_obj_size; - uint32_t offset1 = (index + 1) * self->_obj_size; - memmove((char*)self->obj + offset, (char*)self->obj + offset1, count * self->_obj_size); - self->_size -= 1; - return true; + return !self->size(self); } bool list_reverse(struct _list* self) @@ -559,7 +183,7 @@ bool list_init(struct _list* list, uint32_t obj_size) // 2. set function // kernel list->append = list_append; - list->at = list_at; + list->get = list_get; list->clear = list_clear; list->destory = list_destory; list->empty = list_empty; diff --git a/datastruct/list.h b/datastruct/list.h index ab46d29..89885d7 100644 --- a/datastruct/list.h +++ b/datastruct/list.h @@ -3,69 +3,43 @@ #include "common.h" -// #define LINK_LIST 1 -// #define LIST 1 - -#ifdef LINK_LIST -#define LIST_TEST - -#ifdef LIST_TEST - typedef int list_data_t; -#else - typedef int list_data_t; -#endif - -typedef struct _list_t -{ - list_data_t data; - struct _list_t * prev; - struct _list_t * next; -}list_t,*plist_t; - -typedef void (*list_data_disp_t)(list_data_t data); - - -bool list_init(plist_t *list); -void list_destroy(plist_t *list); -bool list_empty(plist_t list); -void list_clear(plist_t list); -bool list_insert_head(plist_t list, list_data_t data); -bool list_insert_tail(plist_t list, list_data_t data); -bool list_delete(plist_t list, list_data_t data); -uint32_t list_count(plist_t list); -void list_traversal_sequence(plist_t list, list_data_disp_t disp); -void list_traversal_reversed(plist_t list, list_data_disp_t disp); - -extern void list_test(void); -#endif - #ifdef LIST struct _list { void * obj; + uint32_t _obj_size; // 元素大小 uint32_t _size; // 栈大小 uint32_t _capacity; // 总容量 uint32_t _ratio; // 扩展比率 // kernel - bool (*append)(struct _list* self, void* obj); // 追加对象 - bool (*insert)(struct _list* self, uint32_t index, void* obj); // 在列表指定位置,插入对象 - bool (*pop)(struct _list* self, int index, void* obj); // 根据索引,删除对象并返回。 - bool (*remove)(struct _list* self, int index); // 根据索引,移除对象 + bool (*append)(struct _list* self, void* obj); // Append object to the end of the list. + bool (*insert)(struct _list* self, int index, void* obj); // Insert object before index. + bool (*pop)(struct _list* self, int index, void* obj); // Remove and return item at index. + + int (*index)(struct _list* self, void* obj); // Return first index of obj. Return -1 if the obj is not present. + bool (*remove)(struct _list* self, void *obj); // Remove first occurrence of obj. - int (*index)(struct _list* self, void* obj); // 在列表中,查找数据是否存在,若存在则返回其索引。否则返回-1 - bool (*at)(struct _list* self, int index, void* obj); // 根据索引,获取对象 + bool (*clear)(struct _list* self); // Remove all items from list. + + bool (*get)(struct _list* self, int index, void* obj); // 根据索引,获取对象 bool (*set)(struct _list* self, int index, void* obj); // 根据索引,修改对象 // base uint32_t(*size)(struct _list* self); - bool (*clear)(struct _list* self); bool (*empty)(struct _list* self); // sort - bool (*reverse)(struct _list* self); + bool (*reverse)(struct _list* self); // Reverse *IN PLACE*. + + /** + Sort the list in ascending order and return false. + The sort is in-place (i.e. the list itself is modified) and stable (i.e. the + order of two equal elements is maintained). + The reverse flag can be set to sort in descending order. + */ bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2)); // free diff --git a/datastruct/list_test.c b/datastruct/list_test.c index db62b22..61103fa 100644 --- a/datastruct/list_test.c +++ b/datastruct/list_test.c @@ -1,4 +1,4 @@ -#define _CRT_SECURE_NO_WARNINGS 1 +#define _CRT_SECURE_NO_WARNINGS 1 // for vs2022 #include "list.h" static void print_num(void* obj) @@ -28,16 +28,16 @@ static void list_test_num(void) list.print(&list); printf("\n"); - printf("----- remove -----\n"); - list.remove(&list, 9); + printf("----- pop -----\n"); + list.pop(&list, 9, NULL); list.print(&list); printf("\n"); - list.remove(&list, 0); + list.pop(&list, 0, NULL); list.print(&list); printf("\n"); - list.remove(&list, 4); + list.pop(&list, 4, NULL); list.print(&list); printf("\n"); @@ -55,17 +55,17 @@ static void list_test_num(void) list.print(&list); printf("\n"); - printf("----- at -----\n"); + printf("----- get -----\n"); index = 0; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); index = 4; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); index = 9; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); @@ -92,15 +92,15 @@ static void list_test_num(void) printf("----- at like python -----\n"); index = -1; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); index = -6; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); index = -10; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = %2d\n", index, temp); printf("----- set like python -----\n"); @@ -177,16 +177,16 @@ static void list_test_struct(void) list.print(&list); printf("\n"); - printf("----- remove -----\n"); - list.remove(&list, 9); + printf("----- pop -----\n"); + list.pop(&list, 9, NULL); list.print(&list); printf("\n"); - list.remove(&list, 0); + list.pop(&list, 0, NULL); list.print(&list); printf("\n"); - list.remove(&list, 4); + list.pop(&list, 4, NULL); list.print(&list); printf("\n"); @@ -206,17 +206,17 @@ static void list_test_struct(void) printf("----- at -----\n"); index = 0; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n"); index = 4; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n"); index = 9; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n"); @@ -250,17 +250,17 @@ static void list_test_struct(void) printf("----- at like python -----\n"); index = -1; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n"); index = -6; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n"); index = -10; - list.at(&list, index, &temp); + list.get(&list, index, &temp); printf("list[%4d] = ", index); list.print_obj(&temp); printf("\n");