diff --git a/ds/1.intro/CMakeLists.txt b/ds/1.intro/CMakeLists.txt deleted file mode 100644 index 87f1a22e..00000000 --- a/ds/1.intro/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ - -add_executable(polynomial - main.c - ) -target_link_libraries(polynomial - -lm - ) \ No newline at end of file diff --git a/ds/1.intro/README.md b/ds/1.intro/README.md deleted file mode 100644 index c3c781d6..00000000 --- a/ds/1.intro/README.md +++ /dev/null @@ -1,4 +0,0 @@ - -# About - -计算多项式在给定的x处的值. diff --git a/ds/1.intro/main.c b/ds/1.intro/main.c deleted file mode 100644 index dc899125..00000000 --- a/ds/1.intro/main.c +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include -#include -#include - -static double sum_of_polynomial(const double* arr, size_t arr_len, double x) { - double p = 0.0; - for (size_t i = 0; i < arr_len; ++i) { - p += arr[i] * pow(x, (double)i); - } - return p; -} - -static double sum_of_polynomial2(const double* arr, size_t arr_len, double x) { - if (arr_len == 0) { - return 0.0; - } - - double p = 0.0; - for (int i = arr_len - 1; i >= 0; --i) { - p = arr[i] + x * p; - } - - return p; -} - -int main(void) { - const size_t kArrayLen = 9; - const double arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - const double x = 12.4; - assert(sum_of_polynomial(arr, kArrayLen, x) == 5418501173.194776); - assert(sum_of_polynomial2(arr, kArrayLen, x) == 5418501173.1947775); - - return 0; -} \ No newline at end of file diff --git a/ds/3.linear_structures/CMakeLists.txt b/ds/3.linear_structures/CMakeLists.txt deleted file mode 100644 index 9dcd5ca3..00000000 --- a/ds/3.linear_structures/CMakeLists.txt +++ /dev/null @@ -1,93 +0,0 @@ - -add_library(array-list STATIC - array_list.c - list.h - ) - -add_library(linked-list STATIC - linked_list.c - list.h - ) - -add_library(array-stack STATIC - array_stack.c - stack.h - ) - -add_library(linked-stack STATIC - linked_stack.c - stack.h - ) - -add_executable(calculator-stack - calculator_stack.c - ) -target_link_libraries(calculator-stack - array-stack - ) - -add_library(array-queue STATIC - array_queue.c - queue.h - ) -add_library(linked-queue STATIC - linked_queue.c - queue.h - ) - -# Unittest -enable_testing() -find_package(GTest REQUIRED) -add_executable(array-list-test - list_test.cpp - list.h - ) -target_link_libraries(array-list-test - GTest::gtest_main - array-list - ) - -add_executable(linked-list-test - list_test.cpp - list.h - ) -target_link_libraries(linked-list-test - GTest::gtest_main - linked-list - ) - -add_executable(array-stack-test - stack_test.cpp - stack.h - ) -target_link_libraries(array-stack-test - GTest::gtest_main - array-stack - ) - -add_executable(linked-stack-test - stack_test.cpp - stack.h - ) -target_link_libraries(linked-stack-test - GTest::gtest_main - linked-stack - ) - -add_executable(array-queue-test - queue_test.cpp - queue.h - ) -target_link_libraries(array-queue-test - GTest::gtest_main - array-queue - ) - -add_executable(linked-queue-test - queue_test.cpp - queue.h - ) -target_link_libraries(linked-queue-test - GTest::gtest_main - linked-queue - ) \ No newline at end of file diff --git a/ds/3.linear_structures/array_list.c b/ds/3.linear_structures/array_list.c deleted file mode 100644 index d895ed0f..00000000 --- a/ds/3.linear_structures/array_list.c +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. - -#include "list.h" - -#include -#include -#include - -struct list_s { - size_t capacity; - size_t length; - element_type* data; -}; - -static int32_t list_resize_shrink(list_t* list, size_t new_size) { - assert(list->length > new_size); - list->length = new_size; - // TODO(Shaohua): Free memory if too many elements are unused. - return 0; -} - -static int32_t list_resize_expand(list_t* list, size_t new_size) { - assert(list->length < new_size); - if (list->length + new_size < list->capacity) { - list->length += new_size; - return 0; - } - - const size_t new_capacity = (list->capacity > new_size) ? (list->capacity * 2) : (new_size * 2); - const size_t new_mem_size = sizeof(element_type) * new_capacity; - void* ptr = malloc(new_mem_size); - if (ptr == NULL) { - perror("malloc()"); - return -1; - } - memset(ptr, 0, new_mem_size); - - memcpy(ptr, list->data, sizeof(element_type) * list->length); - list->data = ptr; - list->length = new_size; - list->capacity = new_capacity; - return 0; -} - -static int32_t list_resize(list_t* list, size_t new_size) { - if (new_size == list->length) { - return 0; - } - if (new_size < list->length) { - return list_resize_shrink(list, new_size); - } - return list_resize_expand(list, new_size); -} - -list_t* list_new(size_t size) { - list_t* list = malloc(sizeof(list_t)); - assert(list != NULL); - const size_t mem_size = sizeof(element_type) * size; - list->data = malloc(mem_size); - assert(list->data != NULL); - memset(list->data, 0, mem_size); - list->capacity = size; - list->length = size; - return list; -} - -void list_free(list_t* list) { - list->length = 0; - list->capacity = 0; - free(list->data); - list->data = NULL; -} - -element_type list_value(list_t* list, size_t position) { - assert(position < list->length); - if (position >= list->length) { - fprintf(stderr, "[%s] Index out of range: %ld, len: %ld\n", __func__, position, list->length); - return 0; - } - return list->data[position]; -} - -void list_set_value(list_t* list, size_t position, element_type new_value) { - assert(position < list->length); - if (position >= list->length) { - fprintf(stderr, "[%s] Index out of range: %ld, len: %ld\n", __func__, position, list->length); - return; - } - list->data[position] = new_value; -} - -ssize_t list_index_of(list_t* list, element_type value) { - for (size_t i = 0; i < list->length; ++i) { - if (list->data[i] == value) { - return (ssize_t)i; - } - } - return -1; -} - -void list_append(list_t* list, element_type value) { - if (list_resize_expand(list, list->length + 1) != 0) { - return; - } - list->data[list->length - 1] = value; -} - -void list_prepend(list_t* list, element_type value) { - if (list_resize_expand(list, list->length + 1) != 0) { - return; - } - memmove(list->data + 1, list->data, sizeof(element_type) * list->length - 1); - list->data[0] = value; -} - -void list_insert(list_t* list, size_t position, element_type value) { - if (list_resize_expand(list, list->length + 1) != 0) { - return; - } - if (position == list->length) { - // Last element. - list->data[position] = value; - } else { - const size_t move_elems = list->length - position; - memmove(list->data + position + 1, list->data + position, sizeof(element_type) * move_elems); - list->data[position] = value; - } -} - -void list_delete(list_t* list, size_t position) { - assert(position < list->length); - if (position >= list->length) { - fprintf(stderr, "[%s] Index out of range: %ld, len: %ld", __func__, position, list->length); - return; - } - if (position < list->length - 1) { - memmove(list->data + position, list->data + position + 1, 1); - } - list->length -= 1; -} - -size_t list_length(list_t* list) { - return list->length; -} - -void list_debug_print(list_t* list) { - assert(list != NULL); - for (size_t i = 0; i < list->length - 1; ++i) { - printf("%d, ", list->data[i]); - } - if (list->length > 0) { - printf("%d\n", list->data[list->length - 1]); - } -} diff --git a/ds/3.linear_structures/array_queue.c b/ds/3.linear_structures/array_queue.c deleted file mode 100644 index f18460d0..00000000 --- a/ds/3.linear_structures/array_queue.c +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. - -#include "queue.h" - -#include -#include -#include -#include - -struct queue_s { - element_type* data; - ssize_t front; - ssize_t rear; - size_t capacity; - size_t size; -}; - -queue_t* queue_new(size_t size) { - queue_t* queue = malloc(sizeof(queue_t)); - assert(queue != NULL); - queue->front = -1; - queue->rear = -1; - queue->capacity = size; - queue->size = 0; - const size_t data_size = sizeof(element_type) * size; - queue->data = malloc(data_size); - assert(queue->data != NULL); - memset(queue->data, 0, data_size); - - return queue; -} - -void queue_free(queue_t* queue) { - free(queue->data); - queue->data = NULL; - free(queue); -} - -bool queue_is_full(queue_t* queue) { - return queue->size == queue->capacity; -} - -bool queue_is_empty(queue_t* queue) { - return queue->size == 0; -} - -size_t queue_size(queue_t* queue) { - return queue->size; -} - -void queue_add(queue_t* queue, element_type value) { - assert(!queue_is_full(queue)); - queue->rear = (queue->rear + 1) % (ssize_t)queue->capacity; - queue->data[queue->rear] = value; - queue->size += 1; -} - -element_type queue_delete(queue_t* queue) { - assert(!queue_is_empty(queue)); - queue->front = (queue->front + 1) % (ssize_t)queue->capacity; - queue->size -= 1; - return queue->data[queue->front]; -} - -element_type queue_get(queue_t* queue) { - assert(!queue_is_empty(queue)); - return queue->data[queue->front+1]; -} - -void queue_debug_print(queue_t* queue) { - if (queue_is_empty(queue)) { - return; - } - - if (queue->rear > queue->front) { - for (ssize_t i = queue->front + 1; i <= queue->rear; ++i) { - printf("%d, ", queue->data[i]); - } - printf("\n"); - } else { - for (ssize_t i = queue->front + 1; i != queue->size; ++i) { - printf("%d, ", queue->data[i]); - } - for (ssize_t i = 0; i <= queue->rear; ++i) { - printf("%d, ", queue->data[i]); - } - printf("\n"); - } - -} \ No newline at end of file diff --git a/ds/3.linear_structures/array_stack.c b/ds/3.linear_structures/array_stack.c deleted file mode 100644 index 2f5125c9..00000000 --- a/ds/3.linear_structures/array_stack.c +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include "stack.h" - -#include -#include -#include -#include - -struct stack_s { - size_t length; - size_t capacity; - element_type* data; -}; - -static void stack_resize_expand(Stack* stack, size_t new_size) { - assert(stack->length < new_size); - if (new_size < stack->capacity) { - stack->length = new_size; - } else { - const size_t new_capacity = (stack->capacity > new_size) ? (stack->capacity * 2) : (new_size * 2); - const size_t mem_size = sizeof(element_type) * new_capacity; - void* ptr = malloc(mem_size); - assert(ptr != NULL); - memset(ptr, 0, mem_size); - memmove(ptr, stack->data, sizeof(element_type) * stack->length); - stack->data = ptr; - stack->capacity = new_capacity; - stack->length = new_size; - } -} - -Stack* stack_new(size_t size) { - Stack* stack = malloc(sizeof(Stack)); - assert(stack != NULL); - const size_t mem_size = sizeof(element_type) * size; - void* ptr = malloc(mem_size); - assert(ptr != NULL); - memset(ptr, 0, mem_size); - stack->length = 0; - stack->capacity = size; - stack->data = ptr; - return stack; -} - -void stack_free(Stack* stack) { - free(stack->data); - stack->data = NULL; - free(stack); -} - -bool stack_is_full(Stack* stack) { - return stack->length + 1 == stack->capacity; -} - -bool stack_is_empty(Stack* stack) { - return stack->length == 0; -} - -size_t stack_size(Stack* stack) { - return stack->length; -} - -void stack_push(Stack* stack, element_type value) { - stack_resize_expand(stack, stack->length + 1); - stack->data[stack->length - 1] = value; -} - -element_type stack_pop(Stack* stack) { - assert(!stack_is_empty(stack)); - stack->length -= 1; - return stack->data[stack->length]; -} - -element_type stack_top(Stack* stack) { - assert(!stack_is_empty(stack)); - return stack->data[stack->length - 1]; -} - -void stack_debug_print(Stack* stack) { - for (size_t i = stack->length; i > 0; --i) { - printf("%d, ", stack->data[i - 1]); - } - printf("\n"); -} \ No newline at end of file diff --git a/ds/3.linear_structures/calculator_stack.c b/ds/3.linear_structures/calculator_stack.c deleted file mode 100644 index 073215d6..00000000 --- a/ds/3.linear_structures/calculator_stack.c +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include -#include -#include - -#include "stack.h" - -const char kPlus = '+'; -const char kMinus = '-'; -const char kMultiply = '*'; -const char kDivide = '/'; - -bool has_higher_priority(char a, char b) { - if ((a == kMultiply || a == kDivide) && (b == kPlus || b == kMinus)) { - return true; - } - return false; -} - -int main(void) { - const char* expression = "2+9/3-5"; - fprintf(stderr, "expression: %s\n", expression); - fflush(stderr); - Stack* stack = stack_new(64); - const size_t exp_len = strlen(expression); - for (size_t i = 0; i < exp_len; ++i) { - char c = expression[i]; - if (c >= '0' && c <= '9') { - fprintf(stderr, "%c ", c); - } else if (c == kMultiply || c == kDivide || c == kPlus || c == kMinus) { - if (stack_is_empty(stack) || has_higher_priority(c, (char) stack_top(stack))) { - stack_push(stack, c); - } else { - while (!stack_is_empty(stack) && !has_higher_priority(c, (char)stack_top(stack))) { - fprintf(stderr, "%c ", (char)stack_pop(stack)); - } - stack_push(stack, c); - } - } else { - fprintf(stderr, "Invalid char: %c\n", c); - break; - } - } - while (!stack_is_empty(stack)) { - const char last_value = (char) stack_pop(stack); - fprintf(stderr, "%c ", last_value); - } - fprintf(stderr, "\n"); - - stack_free(stack); - return 0; -} \ No newline at end of file diff --git a/ds/3.linear_structures/linked_list.c b/ds/3.linear_structures/linked_list.c deleted file mode 100644 index 592b3d11..00000000 --- a/ds/3.linear_structures/linked_list.c +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include "list.h" - -#include -#include -#include - -typedef struct list_node_s list_node_t; -struct list_node_s { - struct list_node_s* next; - element_type value; -}; - -struct list_s { - size_t length; - list_node_t* head; -}; - -static list_node_t* list_node_at(list_t* list, size_t position) { - if (position >= list->length) { - fprintf(stderr, "Index out of range: %ld, len: %ld\n", position, list->length); - return NULL; - } - list_node_t* node = list->head; - for (size_t i = 0; i < position && node != NULL; ++i) { - node = node->next; - } - return node; -} - -list_t* list_new(size_t size) { - list_t* list = malloc(sizeof(list_t)); - assert(list != NULL); - list->length = size; - list->head = NULL; - for (size_t i = 0; i < size; ++i) { - list_node_t* node = malloc(sizeof(list_node_t)); - assert(node != NULL); - node->next = list->head; - node->value = 0; - list->head = node; - } - return list; -} - -void list_free(list_t* list) { - list->length = 0; - while (list->head != NULL) { - list_node_t* node = list->head; - list->head = list->head->next; - free(node); - } - free(list); -} - -element_type list_value(list_t* list, size_t position) { - if (position >= list->length) { - fprintf(stderr, "Index out of range: %ld, len: %ld\n", position, list->length); - return 0; - } - - list_node_t* node = list_node_at(list, position); - assert(node != NULL); - return node->value; -} - -void list_set_value(list_t* list, size_t position, element_type new_value) { - if (position >= list->length) { - fprintf(stderr, "Index out of range: %ld, len: %ld\n", position, list->length); - return; - } - - list_node_t* node = list_node_at(list, position); - assert(node != NULL); - node->value = new_value; -} - -ssize_t list_index_of(list_t* list, element_type value) { - list_node_t* node = list->head; - for (size_t i = 0; i < list->length; ++i) { - if (node->value == value) { - return (ssize_t) i; - } - node = node->next; - } - return -1; -} - -void list_append(list_t* list, element_type value) { - list_insert(list, list->length, value); -} - -void list_prepend(list_t* list, element_type value) { - list_insert(list, 0, value); -} - -void list_insert(list_t* list, size_t position, element_type value) { - list_node_t* new_node = malloc(sizeof(list_node_t)); - assert(new_node != NULL); - new_node->value = value; - - if (position == 0) { - new_node->next = list->head; - list->head = new_node; - } else { - list_node_t* node = list_node_at(list, position - 1); - assert(node != NULL); - new_node->next = node->next; - node->next = new_node; - } - list->length += 1; -} - -void list_delete(list_t* list, size_t position) { - if (position >= list->length) { - fprintf(stderr, "Index out of range: %ld, len: %ld", position, list->length); - return; - } - - if (position == 0) { - list_node_t* node = list->head; - list->head = list->head->next; - free(node); - } else { - list_node_t* node = list_node_at(list, position - 1); - assert(node != NULL); - list_node_t* delete_node = node->next; - node->next = delete_node->next; - free(delete_node); - } - list->length -= 1; -} - -size_t list_length(list_t* list) { - return list->length; -} - -void list_debug_print(list_t* list) { - for (list_node_t* node = list->head; node != NULL; node = node->next) { - printf("%d, ", node->value); - } - printf("\n"); -} \ No newline at end of file diff --git a/ds/3.linear_structures/linked_queue.c b/ds/3.linear_structures/linked_queue.c deleted file mode 100644 index 2f93f90a..00000000 --- a/ds/3.linear_structures/linked_queue.c +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include "queue.h" - -#include -#include -#include -#include - -typedef struct queue_node_s queue_node_t; -struct queue_node_s { - element_type value; - queue_node_t* next; -}; - -struct queue_s { - queue_node_t* front; - queue_node_t* rear; - size_t size; -}; - -queue_t* queue_new(size_t size) { - (void) size; - queue_t* queue = malloc(sizeof(queue_t)); - assert(queue != NULL); - queue->front = NULL; - queue->rear = NULL; - queue->size = 0; - return queue; -} - -void queue_free(queue_t* queue) { - for (queue_node_t* node = queue->front; node != queue->rear; /* */) { - queue_node_t* next_node = node->next; - free(node); - node = next_node; - } - - free(queue); -} - -bool queue_is_full(queue_t* queue) { - (void) queue; - return false; -} - -bool queue_is_empty(queue_t* queue) { - return queue->size == 0; -} - -size_t queue_size(queue_t* queue) { - return queue->size; -} - -void queue_add(queue_t* queue, element_type value) { - queue->size += 1; - queue_node_t* new_node = malloc(sizeof(queue_node_t)); - assert(new_node != NULL); - new_node->value = value; - new_node->next = NULL; - if (queue->rear == NULL) { - queue->rear = new_node; - queue->front = new_node; - } else { - queue->rear->next = new_node; - queue->rear = new_node; - } -} - -element_type queue_delete(queue_t* queue) { - assert(!queue_is_empty(queue)); - queue_node_t* new_rear = queue->front; - if (queue->front == queue->rear) { - // When last element is deleted, rear ponter needs to be reset too. - queue->rear = NULL; - } - queue->front = queue->front->next; - free(new_rear); - queue->size -= 1; -} - -element_type queue_get(queue_t* queue) { - assert(!queue_is_empty(queue)); - return queue->front->value; -} - -void queue_debug_print(queue_t* queue) { - if (queue_is_empty(queue)) { - return; - } - - for (queue_node_t* ptr = queue->front; ptr != NULL; ptr = ptr->next) { - printf("%d, ", ptr->value); - } - printf("\n"); -} diff --git a/ds/3.linear_structures/linked_stack.c b/ds/3.linear_structures/linked_stack.c deleted file mode 100644 index ca763c1e..00000000 --- a/ds/3.linear_structures/linked_stack.c +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include "stack.h" - -#include -#include -#include - -typedef struct stack_node_s stack_node_t; -struct stack_node_s { - stack_node_t* next; - element_type value; -}; - -struct stack_s { - size_t length; - stack_node_t* head; -}; - -Stack* stack_new(size_t size) { - (void) size; - Stack* stack = malloc(sizeof(Stack)); - assert(stack != NULL); - stack->length = 0; - stack->head = NULL; - return stack; -} - -void stack_free(Stack* stack) { - while (stack->head != NULL) { - stack_node_t* node = stack->head; - stack->head = stack->head->next; - free(node); - } - stack->head = NULL; - free(stack); -} - -bool stack_is_full(Stack* stack) { - (void) stack; - return false; -} - -bool stack_is_empty(Stack* stack) { - return stack->length == 0; -} - -size_t stack_size(Stack* stack) { - return stack->length; -} - -void stack_push(Stack* stack, element_type value) { - assert(!stack_is_full(stack)); - stack->length += 1; - stack_node_t* node = malloc(sizeof(stack_node_t)); - assert(node != NULL); - node->value = value; - node->next = stack->head; - stack->head = node; -} - -element_type stack_pop(Stack* stack) { - assert(!stack_is_empty(stack)); - stack->length -= 1; - stack_node_t* node = stack->head; - stack->head = node->next; - element_type value = node->value; - free(node); - return value; -} - -element_type stack_top(Stack* stack) { - assert(!stack_is_empty(stack)); - return stack->head->value; -} \ No newline at end of file diff --git a/ds/3.linear_structures/list.h b/ds/3.linear_structures/list.h deleted file mode 100644 index 30b4d739..00000000 --- a/ds/3.linear_structures/list.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. - -#ifndef DS_LIST_H_ -#define DS_LIST_H_ - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct list_s list_t; - -#define element_type int32_t - -list_t* list_new(size_t size); - -void list_free(list_t* list); - -element_type list_value(list_t* list, size_t position); - -void list_set_value(list_t* list, size_t position, element_type new_value); - -ssize_t list_index_of(list_t* list, element_type value); - -void list_append(list_t* list, element_type value); - -void list_prepend(list_t* list, element_type value); - -void list_insert(list_t* list, size_t position, element_type value); - -void list_delete(list_t* list, size_t position); - -size_t list_length(list_t* list); - -void list_debug_print(list_t* list); - -#ifdef __cplusplus -} -#endif - -#endif // DS_LIST_H_ diff --git a/ds/3.linear_structures/list_test.cpp b/ds/3.linear_structures/list_test.cpp deleted file mode 100644 index 853fb714..00000000 --- a/ds/3.linear_structures/list_test.cpp +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include - -#include "list.h" - -TEST(ListTest, TestNew) { - list_t* list = list_new(4); - ASSERT_NE(list, nullptr); - ASSERT_EQ(list_length(list), 4); - list_free(list); -} - -TEST(ListTest, TestGetSet) { - list_t* list = list_new(4); - ASSERT_EQ(list_length(list), 4); - ASSERT_EQ(list_value(list, 2), 0); - list_set_value(list, 2, 42); - ASSERT_EQ(list_value(list, 2), 42); - list_free(list); -} - -TEST(ListTest, TestIndexOf) { - list_t* list = list_new(4); - list_set_value(list, 2, 42); - ASSERT_EQ(list_index_of(list, 42), 2); - ASSERT_EQ(list_index_of(list, -42), -1); - list_free(list); -} - -TEST(ListTest, TestAppend) { - list_t* list = list_new(4); - list_set_value(list, 3, 43); - list_append(list, 44); - ASSERT_EQ(list_value(list, 3), 43); - ASSERT_EQ(list_value(list, 4), 44); - list_free(list); -} - -TEST(ListTest, TestPrepend) { - list_t* list = list_new(4); - list_set_value(list, 0, 40); - list_prepend(list, 39); - ASSERT_EQ(list_value(list, 0), 39); - ASSERT_EQ(list_value(list, 1), 40); - list_free(list); -} - -TEST(ListTest, TestInsertHead) { - list_t* list = list_new(4); - list_set_value(list, 0, 40); - list_insert(list, 0, 39); - ASSERT_EQ(list_length(list), 5); - ASSERT_EQ(list_value(list, 0), 39); - ASSERT_EQ(list_value(list, 1), 40); -} - -TEST(ListTest, TestInsertMiddle) { - list_t* list = list_new(4); - list_set_value(list, 1, 41); - list_set_value(list, 2, 42); - list_insert(list, 1, 52); - ASSERT_EQ(list_length(list), 5); - ASSERT_EQ(list_value(list, 1), 52); - ASSERT_EQ(list_value(list, 2), 41); - ASSERT_EQ(list_value(list, 3), 42); -} - -TEST(ListTest, TestInsertTail) { - list_t* list = list_new(4); - list_set_value(list, 3, 44); - list_insert(list, 4, 45); - ASSERT_EQ(list_length(list), 5); - ASSERT_EQ(list_value(list, 3), 44); - ASSERT_EQ(list_value(list, 4), 45); -} - -TEST(ListTest, TestDeleteHead) { - list_t* list = list_new(4); - list_set_value(list, 0, 40); - list_set_value(list, 1, 41); - list_delete(list, 0); - ASSERT_EQ(list_length(list), 3); - ASSERT_EQ(list_value(list, 0), 41); - list_free(list); -} - -TEST(ListTest, TestDeleteMiddle) { - list_t* list = list_new(4); - list_set_value(list, 2, 42); - list_set_value(list, 3, 43); - list_delete(list, 2); - ASSERT_EQ(list_length(list), 3); - ASSERT_EQ(list_value(list, 2), 43); - list_free(list); -} - -TEST(ListTest, TestDeleteTail) { - list_t* list = list_new(4); - list_set_value(list, 2, 42); - list_set_value(list, 3, 43); - list_delete(list, 3); - ASSERT_EQ(list_length(list), 3); - ASSERT_EQ(list_value(list, 2), 42); - list_free(list); -} \ No newline at end of file diff --git a/ds/3.linear_structures/orthogonal_linked_list.c b/ds/3.linear_structures/orthogonal_linked_list.c deleted file mode 100644 index 6b0a2d55..00000000 --- a/ds/3.linear_structures/orthogonal_linked_list.c +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include - -typedef struct ol_node_s ol_node_t; -#define element_type int32_t - -// 二维十字链表 -struct ol_node_s { - int32_t row; - int32_t column; - element_type value; - ol_node_t* right; - ol_node_t* down; -}; - -// 用多重链表表示稀疏矩阵 -struct tag_s { - -}; - -struct term_s { - void* right; - void* down; - int32_t row; - int32_t column; - element_type value; -}; - -struct head_s { - void* right; - void* down; -}; - -// 广义表 -typedef struct general_list_s general_list_t; -struct general_list_s { - general_list_t* next; - element_type value; - general_list_t* sub_list; - int32_t has_sub_list; -}; \ No newline at end of file diff --git a/ds/3.linear_structures/queue.h b/ds/3.linear_structures/queue.h deleted file mode 100644 index 0d7d437b..00000000 --- a/ds/3.linear_structures/queue.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. - -#ifndef DS_QUEUE_H -#define DS_QUEUE_H - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct queue_s queue_t; -#define element_type int32_t - -queue_t* queue_new(size_t size); -void queue_free(queue_t* queue); - -bool queue_is_full(queue_t* queue); -bool queue_is_empty(queue_t* queue); -size_t queue_size(queue_t* queue); - -void queue_add(queue_t* queue, element_type value); -element_type queue_delete(queue_t* queue); -element_type queue_get(queue_t* queue); - -void queue_debug_print(queue_t* queue); - -#ifdef __cplusplus -} -#endif - -#endif // DS_QUEUE_H diff --git a/ds/3.linear_structures/queue_test.cpp b/ds/3.linear_structures/queue_test.cpp deleted file mode 100644 index a966557c..00000000 --- a/ds/3.linear_structures/queue_test.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include - -#include "queue.h" - -TEST(ArrayQueueTest, TestNew) { - queue_t* queue = queue_new(5); - ASSERT_NE(queue, nullptr); - ASSERT_TRUE(queue_is_empty(queue)); - queue_free(queue); -} - -TEST(ArrayQueueTest, TestAdd) { - queue_t* queue = queue_new(5); - queue_add(queue, 0); - ASSERT_EQ(queue_get(queue), 0); - ASSERT_EQ(queue_size(queue), 1); - queue_debug_print(queue); - queue_add(queue, 1); - ASSERT_EQ(queue_get(queue), 0); - ASSERT_EQ(queue_size(queue), 2); - queue_debug_print(queue); - queue_free(queue); -} - -TEST(ArrayQueueTest, TestDelete) { - queue_t* queue = queue_new(3); - queue_add(queue, 0); - queue_add(queue, 1); - queue_add(queue, 2); - queue_debug_print(queue); - - queue_delete(queue); - queue_debug_print(queue); - ASSERT_EQ(queue_size(queue), 2); - - queue_add(queue, 0); - ASSERT_EQ(queue_get(queue), 1); - ASSERT_EQ(queue_size(queue), 3); - queue_debug_print(queue); - queue_free(queue); -} \ No newline at end of file diff --git a/ds/3.linear_structures/stack.h b/ds/3.linear_structures/stack.h deleted file mode 100644 index 0aea1904..00000000 --- a/ds/3.linear_structures/stack.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#ifndef LIST__STACK_H_ -#define LIST__STACK_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -typedef struct stack_s Stack; -#define element_type int32_t - -Stack* stack_new(size_t size); -void stack_free(Stack* stack); - -bool stack_is_full(Stack* stack); -bool stack_is_empty(Stack* stack); -size_t stack_size(Stack* stack); - -void stack_push(Stack* stack, element_type value); -element_type stack_pop(Stack* stack); -element_type stack_top(Stack* stack); - -void stack_debug_print(Stack* stack); - -#ifdef __cplusplus -} -#endif - -#endif //LIST__STACK_H_ diff --git a/ds/3.linear_structures/stack_test.cpp b/ds/3.linear_structures/stack_test.cpp deleted file mode 100644 index db985d99..00000000 --- a/ds/3.linear_structures/stack_test.cpp +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include - -#include "stack.h" - -TEST(StackTest, StackNew) { - Stack* stack = stack_new(4); - ASSERT_TRUE(stack != nullptr); - stack_free(stack); -} - -TEST(StackTest, StackIsEmpty) { - Stack* stack = stack_new(4); - ASSERT_TRUE(stack_is_empty(stack)); - stack_free(stack); -} - -TEST(StackTest, StackPush) { - Stack* stack = stack_new(4); - stack_push(stack, 40); - stack_push(stack, 41); - stack_push(stack, 42); - ASSERT_FALSE(stack_is_empty(stack)); - ASSERT_EQ(stack_size(stack), 3); - stack_free(stack); -} - -TEST(StackTest, StackDebugPrint) { - Stack* stack = stack_new(4); - ASSERT_TRUE(stack_is_empty(stack)); - stack_push(stack, 40); - stack_push(stack, 41); - stack_push(stack, 42); - stack_free(stack); -} diff --git a/ds/4.tree/CMakeLists.txt b/ds/4.tree/CMakeLists.txt deleted file mode 100644 index 658cb7d5..00000000 --- a/ds/4.tree/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ - -add_library(linked-binary-tree-static STATIC - linked_binary_tree.c - binary_tree.h - ) - -add_executable(binary-tree-test - binary_tree_test.c -) -target_link_libraries(binary-tree-test - linked-binary-tree-static -) diff --git a/ds/4.tree/README.md b/ds/4.tree/README.md deleted file mode 100644 index f0904b8a..00000000 --- a/ds/4.tree/README.md +++ /dev/null @@ -1,19 +0,0 @@ - -# 树 -方便表示层次关系. -- 插入 -- 删除 -- 查找 - -## 顺序查找 - -## 二分法查找 - - -## 二叉树 - -遍历方法: -- 先序, preorder traversal, 根, 左子树, 右子树 -- 中序, in order traversal, 左子树, 根, 右子树 -- 后序, post order traversal, 左子树, 右子树, 根 -- 层次遍历, level order traversal, 从上到下, 从左到右 diff --git a/ds/4.tree/binary_tree.h b/ds/4.tree/binary_tree.h deleted file mode 100644 index dcc1a8b3..00000000 --- a/ds/4.tree/binary_tree.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#ifndef BINARY_TREE_H_ -#define BINARY_TREE_H_ - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct binary_tree_s binary_tree_t; - -/** - * Create an empty binary tree. - * - * @return - */ -extern binary_tree_t* binary_tree_new(); - -/** - * Deallocate tree nodes in binary tree and free the binary tree descriptor. - * - * @param tree - */ -extern void binary_tree_free(binary_tree_t* tree); - -/** - * Get number of elements in binary tree. - * - * @param tree - * @return - */ -extern size_t binary_tree_size(binary_tree_t* tree); - -/** - * Returns true if binary tree has no node. - * - * @param tree - * @return - */ -extern bool binary_tree_is_empty(binary_tree_t* tree); - -enum tree_traverse_mode { - kTraverseInOrder = 0, - kTraversePreOrder, - kTraversePostOrder, -}; - -/** - * Calls |apply| function for each node in binary tree. - * - * @param tree - * @param apply - * @param user_data - */ -extern void binary_tree_traverse(binary_tree_t* tree, - tree_traverse_mode mode, - void (*apply)(void* value, void* user_data), - void* user_data); - -#ifdef __cplusplus -} -#endif - -#endif // BINARY_TREE_H_ diff --git a/ds/4.tree/binary_tree_test.c b/ds/4.tree/binary_tree_test.c deleted file mode 100644 index dc72b138..00000000 --- a/ds/4.tree/binary_tree_test.c +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include - -#include "binary_tree.h" - -int main(void) { - binary_tree_t* tree = binary_tree_new(); - - binary_tree_free(tree); - tree = NULL; - - return 0; -} diff --git a/ds/4.tree/linked_binary_tree.c b/ds/4.tree/linked_binary_tree.c deleted file mode 100644 index af3eb8ea..00000000 --- a/ds/4.tree/linked_binary_tree.c +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) 2022 Xu Shaohua . All rights reserved. -// Use of this source is governed by GNU General Public License -// that can be found in the LICENSE file. - -#include "binary_tree.h" - -#include -#include -#include -#include - -typedef struct tree_node_s tree_node_t; - -struct tree_node_s { - tree_node_t* left; - tree_node_t* right; - void* value; -}; - -struct binary_tree_s { - size_t size; - tree_node_t* root; -}; - -binary_tree_t* binary_tree_new() { - binary_tree_t* tree = malloc(sizeof(binary_tree_t)); - assert(tree != NULL); - tree->size = 0; - tree->root = NULL; - - return tree; -} - -void binary_tree_free(binary_tree_t* tree) { - assert(tree != NULL); - // TODO(Shaohua): deallocate tree nodes. - free(tree); -} - -size_t binary_tree_size(binary_tree_t* tree) { - assert(tree != NULL); - return tree->size; -} - -bool binary_tree_is_empty(binary_tree_t* tree) { - assert(tree != NULL); - return tree->size > 0; -} - -static void tree_node_traverse_in_order(tree_node_t* node, - void (*apply)(void* value, void* user_data), - void* user_data) { - if (node != NULL) { - tree_node_traverse_in_order(node->left, apply, user_data); - apply(node->value, user_data); - tree_node_traverse_in_order(node->right, apply, user_data); - } -} - -static void tree_node_traverse_pre_order(tree_node_t* node, - void (*apply)(void* value, void* user_data), - void* user_data) { - if (node != NULL) { - apply(node->value, user_data); - tree_node_traverse_pre_order(node->left, apply, user_data); - tree_node_traverse_pre_order(node->right, apply, user_data); - } -} - -static void tree_node_traverse_post_order(tree_node_t* node, - void (*apply)(void* value, void* user_data), - void* user_data) { - if (node != NULL) { - tree_node_traverse_post_order(node->left, apply, user_data); - tree_node_traverse_post_order(node->right, apply, user_data); - apply(node->value, user_data); - } -} - -void binary_tree_traverse(binary_tree_t* tree, - tree_traverse_mode mode, - void (*apply)(void* value, void* user_data), - void* user_data) { - assert(tree != NULL); - switch (mode) { - case kTraverseInOrder: { - tree_node_traverse_in_order(tree->root, apply, user_data); - break; - } - case kTraversePreOrder: { - tree_node_traverse_pre_order(tree->root, apply, user_data); - break; - } - case kTraversePostOrder: { - tree_node_traverse_post_order(tree->root, apply, user_data); - break; - } - default: { - fprintf(stderr, "Invalid traverse mode\n"); - assert(0); - } - } -} diff --git a/ds/CMakeLists.txt b/ds/CMakeLists.txt deleted file mode 100644 index b648ddd6..00000000 --- a/ds/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(ds C CXX) - -add_subdirectory(1.intro) -add_subdirectory(3.linear_structures) -add_subdirectory(4.tree) \ No newline at end of file diff --git a/ds/README.md b/ds/README.md deleted file mode 100644 index aaff0327..00000000 --- a/ds/README.md +++ /dev/null @@ -1,5 +0,0 @@ - -# 关于 -数据结构, 第二版, 陈越 - -https://www.bilibili.com/video/BV1H4411N7oD/