diff --git a/datastruct/deque.c b/datastruct/deque.c index 12e4393..efab106 100644 --- a/datastruct/deque.c +++ b/datastruct/deque.c @@ -5,7 +5,7 @@ bool deque_push_back(struct _deque* self, void* obj) { assert(self != NULL); assert(self->_head != NULL); - struct _deque_node* node = NULL; + struct _deque_node* front = NULL; // create a new object void* new_obj = (void*)malloc(self->_obj_size); @@ -33,11 +33,11 @@ bool deque_push_back(struct _deque* self, void* obj) new_node->prev = self->_head->next; new_node->next = self->_head->prev; - node = self->_head->next; - node->next = new_node; + front = self->_head->next; + front->next = new_node; - node = self->_head->prev; - node->prev = new_node; + front = self->_head->prev; + front->prev = new_node; self->_head->next = new_node; @@ -52,7 +52,36 @@ bool deque_push_front(struct _deque* self, void* obj) bool deque_pop_back(struct _deque* self, void* obj) { + assert(self != NULL); + assert(self->_head != NULL); + struct _deque_node* node = NULL; + struct _deque_node* back = NULL; + if (self->empty(self)) + { + return false; + } + + if (obj != NULL) + { + memmove(obj, self->_head->next->obj, self->_obj_size); + } + back = self->_head->next; + self->_head->next = back->prev; + + node = self->_head->prev; + node->prev = self->_head->next; + + free(back->obj); + free(back); + + self->_size -= 1; + if (self->empty(self)) + { + self->_head->prev = NULL; + self->_head->next = NULL; + } + return true; } bool deque_pop_front(struct _deque* self, void* obj) @@ -109,7 +138,11 @@ bool deque_remove(struct _deque* self, void* obj) bool deque_clear(struct _deque* self) { - + while (!self->empty(self)) + { + deque_pop_back(self, NULL); + } + return true; } bool deque_get(struct _deque* self, int index, void* obj) diff --git a/datastruct/deque_test.c b/datastruct/deque_test.c index 800ab0c..ecfb376 100644 --- a/datastruct/deque_test.c +++ b/datastruct/deque_test.c @@ -3,13 +3,13 @@ static void print_num(void* obj) { - printf("(%2d )", *(int*)obj); + printf("(%2d ) ", *(int*)obj); } static void deque_test_num(void) { uint32_t i = 0; - int data[] = { 0, 1,2,3,4,5,6,7,8,9,10 }; + int data[] = { 1,2,3,4,5,6,7,8,9,10 }; int temp = 0; uint32_t len = sizeof(data) / sizeof(data[0]); @@ -19,7 +19,7 @@ static void deque_test_num(void) printf("\n\n----- deque_test_num -----\n"); - printf("----- push_back -----\n"); + printf("----- after push_back -----\n"); for (i = 0; i < len; i++) { dq.push_back(&dq, &data[i]); @@ -44,27 +44,45 @@ static void deque_test_num(void) printf("----- empty -----\n"); } -#if 0 - printf("----- pop -----\n"); + printf("----- push_back -----\n"); + for (i = 0; i < len; i++) + { + dq.push_back(&dq, &data[i]); + } + + printf("----- after pop_back -----\n"); for (i = 0; i < len + 1; i++) { - if (true == s.pop(&s, &temp)) + if (true == dq.pop_back(&dq, &temp)) { - printf("top = "); - s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + printf("pop = "); + dq.print_obj(&temp); + + if (true == dq.front(&dq, &temp)) + { + printf("front = "); + dq.print_obj(&temp); + } + + if (dq.back(&dq, &temp)) + { + printf("back = "); + dq.print_obj(&temp); + } + + printf("size = %2d\n", dq.size(&dq)); } else { printf("pop failed! because stack is empty\n"); } - if (s.empty(&s)) + if (dq.empty(&dq)) { printf("----- empty -----\n"); } } -#endif + dq.destory(&dq); } diff --git a/datastruct/stack_test.c b/datastruct/stack_test.c index 7b4a1af..0d91ac2 100644 --- a/datastruct/stack_test.c +++ b/datastruct/stack_test.c @@ -2,7 +2,7 @@ static void print_num(void* obj) { - printf("(%2d )", *(int*)obj); + printf("(%2d ) ", *(int*)obj); } static void stack_test_num(void) @@ -29,7 +29,8 @@ static void stack_test_num(void) printf("top = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + printf("size = %2d\n", s.size(&s)); } printf("----- print -----\n"); s.print(&s); @@ -40,9 +41,16 @@ static void stack_test_num(void) { if (true == s.pop(&s, &temp)) { - printf("top = "); + printf("pop = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + if (s.peek(&s, &temp)) + { + printf("top = "); + s.print_obj(&temp); + } + + printf("size = %2d\n", s.size(&s)); } else { @@ -60,7 +68,7 @@ static void stack_test_num(void) static void print_char(void* obj) { - printf("(%2c )", *(char*)obj); + printf("(%2c ) ", *(char*)obj); } static void stack_test_char(void) @@ -86,7 +94,8 @@ static void stack_test_char(void) s.peek(&s, &temp); printf("top = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + printf("size = %2d\n", s.size(&s)); } printf("----- print -----\n"); s.print(&s); @@ -97,9 +106,16 @@ static void stack_test_char(void) { if (true == s.pop(&s, &temp)) { - printf("top = "); + printf("pop = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + if (s.peek(&s, &temp)) + { + printf("top = "); + s.print_obj(&temp); + } + + printf("size = %2d\n", s.size(&s)); } else { @@ -154,7 +170,8 @@ static void stack_test_struct(void) s.peek(&s, &temp); printf("top = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + printf("size = %2d\n", s.size(&s)); } printf("----- print -----\n"); s.print(&s); @@ -177,9 +194,16 @@ static void stack_test_struct(void) { if (true == s.pop(&s, &temp)) { - printf("top = "); + printf("pop = "); s.print_obj(&temp); - printf("\tsize after push = %2d\n", s.size(&s)); + + if (s.peek(&s, &temp)) + { + printf("top = "); + s.print_obj(&temp); + } + + printf("size = %2d\n", s.size(&s)); } else {