diff --git a/yonglin_zhang/C/link_list/include/link_list.h b/yonglin_zhang/C/link_list/include/link_list.h index 9f3c85f97980967f18f95904a2531190b2b7812a..204cca96867a788a45c154df3975a539390f76d0 100644 --- a/yonglin_zhang/C/link_list/include/link_list.h +++ b/yonglin_zhang/C/link_list/include/link_list.h @@ -157,7 +157,7 @@ int reverse(node *head); * * \return #ALG_OK 操作成功。 */ -int ascend(); +int ascend(node *head); /** * \brief 降序排序。 @@ -167,7 +167,7 @@ int ascend(); * * \return #ALG_OK 操作成功。 */ -int descend(); +int descend(node *head); /** 查询操作 */ @@ -181,7 +181,7 @@ int descend(); * * \return #ALG_OK 操作成功。 */ -int show_all(); +int show_all(node *head); /** * \brief 查询数值为X的节点。 @@ -191,7 +191,7 @@ int show_all(); * * \return #ALG_OK 操作成功。 */ -int search_data_x(); +int search_data_x(node *head, DATA_TYPE target_data); /** * \brief 查询位置为X的节点。 @@ -201,7 +201,7 @@ int search_data_x(); * * \return #ALG_OK 操作成功。 */ -int search_pos_x(); +int search_pos_x(node *head, int pos); /** * \brief 返回整个链表的长度。 @@ -211,7 +211,7 @@ int search_pos_x(); * * \return #ALG_OK 操作成功。 */ -int get_length(); +int get_length(node *head, int *length); /** 更新操作 */ @@ -225,7 +225,7 @@ int get_length(); * * \return #ALG_OK 操作成功。 */ -int update_data_x(); +int update_data_x(node *head, DATA_TYPE target_data, DATA_TYPE new_data); /** * \brief 更新位置为X的节点。 @@ -235,7 +235,7 @@ int update_data_x(); * * \return #ALG_OK 操作成功。 */ -int update_pos_x(); +int update_pos_x(node *head, int pos, DATA_TYPE new_data); #ifdef __cplusplus diff --git a/yonglin_zhang/C/link_list/src/search.c b/yonglin_zhang/C/link_list/src/search.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b80dd28686e18ea0046e3d1f8a75bf75185d44c9 100644 --- a/yonglin_zhang/C/link_list/src/search.c +++ b/yonglin_zhang/C/link_list/src/search.c @@ -0,0 +1,90 @@ +#include +#include +#include "link_list.h" +#include "alg_errno.h" + +int show_all(node *head) +{ + node *t = head; + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + printf("link list:\n"); + while(t != NULL) { + printf("%d, ", t->data); + t = t->next; + } + return ALG_OK; +} + +int search_data_x(node *head, DATA_TYPE target_data) +{ + node *t = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + t = head; + + while(t != NULL) { + if(t->data == target_data) { + return ALG_OK; + } + t = t->next; + } + + return ALG_ERR_NO_THIS_POINT; +} + +int search_pos_x(node *head, int pos) +{ + node *t = NULL; + int counter = 0; + + if(head == NULL || pos <= 0) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + t = head; + while(t != NULL) { + counter++; + if(counter == pos) { + return ALG_OK; + } + t = t->next; + } + + return ALG_ERR_POS_NOT_IN_RANGE; +} + +int get_length(node *head, int *length) +{ + node *t = NULL; + + if(head == NULL || length == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + t = head; + + while(t != NULL) { + (*length)++; + t = t->next; + } + + return ALG_OK; +} \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/src/sort.c b/yonglin_zhang/C/link_list/src/sort.c index 42c3f77ba21c350477a51909deed335344187ac7..fd6242cf1e7f805c388a32fd8c88654253fa1cb2 100644 --- a/yonglin_zhang/C/link_list/src/sort.c +++ b/yonglin_zhang/C/link_list/src/sort.c @@ -3,3 +3,88 @@ #include "link_list.h" #include "alg_errno.h" +int reverse(node *head) +{ + node *p = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + //空链表不需要反转 + if(head->next == NULL) { + return ALG_OK; + } + + p = head->next; + head->next = NULL; + while(p != NULL) { + node *t = p; + p = p->next; + t->next = head->next; + head->next = t; + } + return ALG_OK; +} + +int ascend(node *head) +{ + node *p = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head->next; + + if(p == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + while(p != NULL) { + node *t = p->next; + while(t != NULL) { + if(t->data < p->data) { + DATA_TYPE temp = t->data; + t->data = p->data; + p->data = temp; + } + t = t->next; + } + p = p->next; + } + + return ALG_OK; +} + +int descend(node *head) +{ + node *p = NULL; + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + if(head == NULL) { + return ALG_ERR_BAD_INPUT_DATA; + } + + p = head->next; + + while(p != NULL) { + node *t = p->next; + while(t != NULL) { + if(t->data > p->data) { + DATA_TYPE temp = t->data; + t->data = p->data; + p->data = temp; + } + t = t->next; + } + p = p->next; + } + + return ALG_OK; +} \ No newline at end of file diff --git a/yonglin_zhang/C/link_list/src/update.c b/yonglin_zhang/C/link_list/src/update.c index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..7b80666a759ef3bc224051e94aad356c330da73a 100644 --- a/yonglin_zhang/C/link_list/src/update.c +++ b/yonglin_zhang/C/link_list/src/update.c @@ -0,0 +1,53 @@ +#include +#include +#include "link_list.h" +#include "alg_errno.h" + +int update_data_x(node *head, DATA_TYPE target_data, DATA_TYPE new_data) +{ + node *t = NULL; + + if(head == NULL ){ + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + t = head->next; + + while(t != NULL) { + if(t->data == target_data) { + t->data = new_data; + return ALG_OK; + } + t = t->next; + } + + return ALG_ERR_NO_THIS_POINT; +} + +int update_pos_x(node *head, int pos, DATA_TYPE new_data) +{ + node *t = NULL; + int counter = 1; + + if(head == NULL || pos <= 0) { + return ALG_ERR_BAD_INPUT_DATA; + } + if(head->next == NULL) { + return ALG_ERR_EMPTY_LIST; + } + + t = head->next; + + while(t != NULL) { + if(counter == pos) { + t->data = new_data; + return ALG_OK; + } + t = t->next; + } + + return ALG_ERR_POS_NOT_IN_RANGE; +} \ No newline at end of file