自己重新修改代码1234
立即下载
资源介绍:
自己重新修改代码1234
#include "MyLinkLisk.h"
db_list_t* db_list_create(void) {
db_list_t* list_head;
list_head = (db_list_t*)malloc(sizeof(db_list_t));
if (list_head == NULL) {
errno = ENOMEM;
return NULL;
}
list_head->limit_size = 0;
list_head->head = (db_lnode_t*)malloc(sizeof(db_lnode_t));
if (list_head->head == NULL) {
free(list_head);
errno = ENOMEM;
return NULL;
}
list_head->head->next = list_head->head->prev = NULL;
list_head->head->data = NULL;
list_head->tail = list_head->head;
return list_head;
}
int __db_list_insert_before(db_list_t** list_head, int num, void* new_node_data) {
int counter = 1;
db_lnode_t* current;
db_lnode_t* new_node;
if (*list_head == NULL || list_head == NULL) {
errno = EINVAL;
return -1;
}
if ((*list_head)->limit_size != 0) {
new_node = (db_lnode_t*)malloc(sizeof(db_lnode_t));
if (new_node == NULL) {
errno = ENOMEM;
return -1;
}
new_node->data = new_node_data;
new_node->prev = new_node->next = NULL;
if (num > 0 && num <= (*list_head)->limit_size) {
current = (*list_head)->head;
while (counter < num) {
counter++;
current = current->next;
}
if (counter == 1) {
(*list_head)->head->prev = new_node;
new_node->next = (*list_head)->head;
(*list_head)->head = new_node;
(*list_head)->limit_size++;
return 0;
}
current->next->prev = new_node;
new_node->prev = current->prev;
current->prev = new_node;
new_node->next = current;
(*list_head)->limit_size++;
return 0;
}
else {
errno = EINVAL;
free(new_node);
return -1;
}
}
else {
if (num != 0) {
errno = EINVAL;
return -1;
}
(*list_head)->head->data = new_node_data;
(*list_head)->limit_size++;
return 0;
}
}
int __db_list_insert_after(db_list_t** list_head, int num, void* new_node_data) {
int counter = 0;
db_lnode_t* current;
db_lnode_t* new_node;
if ((*list_head) == NULL || list_head == NULL) {
errno = EINVAL;
return -1;
}
if ((*list_head)->limit_size != 0) {
new_node = (db_lnode_t*)malloc(sizeof(db_lnode_t));
if (new_node == NULL) {
errno = ENOMEM;
return -1;
}
new_node->data = new_node_data;
new_node->next = new_node->prev = NULL;
if (num > 0 && num <= (*list_head)->limit_size) {
current = (*list_head)->head;
while (counter != num - 1) {
counter++;
current = current->next;
}
if (current->next == NULL) {
(*list_head)->tail = new_node;
current->next = new_node;
new_node->prev = current;
(*list_head)->limit_size++;
return 0;
}
new_node->prev = current;
new_node->next = current->next;
current->next->prev = new_node;
current->next = new_node;
(*list_head)->limit_size++;
return 0;
}
else {
free(new_node);
errno = EINVAL;
return -1;
}
}
else {
if (num != 0) {
errno = EINVAL;
return -1;
}
(*list_head)->head->data = new_node_data;
(*list_head)->limit_size++;
return 0;
}
}
void __db_lnode_flush(db_list_t* list_head, int num, void* new_node_data) {
int counter = 0;
db_lnode_t* current;
if (list_head == NULL || num<0 || num>list_head->limit_size) {
errno = EINVAL;
return;
}
current = list_head->head;
while (counter != num - 1) {
current = current->next;
counter++;
}
current->data = new_node_data;
}
void __db_list_delete(db_list_t** list_head, int num) {
int counter = 1;
db_lnode_t* current;
db_lnode_t* tmp;
if ((*list_head) == NULL || list_head == NULL) {
errno = EINVAL;
return;
}
current = (*list_head)->head;
while (counter < num) {
counter++;
current = current->next;
}
if (counter == 1) {
tmp = (*list_head)->head;
(*list_head)->head = (*list_head)->head->next;
free(tmp);
(*list_head)->head->prev = NULL;
(*list_head)->limit_size--;
return;
}
if ((*list_head)->limit_size == counter) {
tmp = (*list_head)->tail;
(*list_head)->tail = (*list_head)->tail->prev;
free(tmp);
(*list_head)->tail->next = NULL;
(*list_head)->limit_size--;
return;
}
tmp = current;
current->next->prev = current->prev;
current->prev->next = current->next;
free(tmp);
(*list_head)->limit_size--;
}
void __db_list_destory(db_list_t* list_head) {
db_lnode_t* current;
db_lnode_t* pos;
if (list_head == NULL) {
errno = EINVAL;
return;
}
current = pos = list_head->head;
if (list_head->limit_size != 0) {
for (int i = 0; i <= list_head->limit_size; i++) {
current = current->next;
free(pos);
pos = current;
}
free(list_head);
}
}
void* __db_list_visit(db_list_t** list_head, int num) {
int counter;
db_lnode_t* current = (*list_head)->head;
if (num<0 || num>(*list_head)->limit_size) {
errno = EINVAL;
return NULL;
}
for (counter = 0; counter < num; counter++) {
current = current->next;
//counter++;
}
return current->data;
}
void __db_list_travel(db_list_t* list_head, void(*do_function)(void*)) {
if (list_head->limit_size < 0 || list_head == NULL) {
errno = EINVAL;
return;
}
for (int i = 0; i < list_head->limit_size; i++) {
(*do_function)(__db_list_visit(&list_head, i));
}
}
int __db_list_search(
db_list_t** list_head, void* find_data, int(*compare)(void*, void*)) {
int counter = 1;
db_lnode_t* current;
if ((*list_head) == NULL || list_head == NULL) {
errno = EINVAL;
return 0;
}
current = (*list_head)->head;
while (compare(current->data, find_data) != 0 && current->next != NULL) {
current = current->next;
counter++;
}
if (current->next == NULL && compare(current->data, find_data) != 0)
return 0;
return counter;
}
void __db_list_merge(db_list_t* main_list, db_list_t* sub_list) {
main_list->tail->next = sub_list->head;
sub_list->head->prev = main_list->tail;
main_list->tail = sub_list->tail;
main_list->limit_size += sub_list->limit_size;
}
static void swap(db_lnode_t* A, db_lnode_t* B) {
void* temp = A->data;
A->data = B->data;
B->data = temp;
}
static db_lnode_t* get(db_list_t* list, int pos) {
int i = 0;
db_lnode_t* cur = list->head;
while (i < pos) {
cur = cur->next;
i++;
}
return cur;
}
static void quick_sort(db_list_t* list, int low, int high, int(*compare)(void*, void*)) {
if (high <= low) return;
int i = low;
int j = high;
db_lnode_t key = { 0 };
memcpy(key.data ,get(list, low)->data);
while (1)
{
/*从左向右找比key大的值*/
while (compare(get(list,i)->data, key->data)<=0)
{
i++;
if (i == high) {
break;
}
}
/*从右向左找比key小的值*/
while (compare(get(list,j)->data, key->data)>=0)
{
j--;
if (j == low) {
break;
}
}
if (i >= j) break;
/*交换i,j对应的值*/
swap(get(list, i), get(list, j));
}
/*中枢值与j对应值交换*/
get(list,low)->data = get(list,j)->data;
get(list,j)->data = key->data;
quick_sort(list, low, j - 1, compare);
quick_sort(list, j + 1, high, compare);
}
void __db_list_quick_sort(db_list_t* list, int(*compare)(void*, void*)) {
quick_sort(list, 0, list->limit_size-1, compare);
}
void __db_list_select_sort(db_list_t* list, int(*compare)(void*, void*)) {
int i = 0, j = 0;
int k = 0;
for (i = 0; i < list->limit_size; i++) {
k = i;
db_lnode_t* node_k;
for (j = i + 1; j < list->limit_size; j++) {
node_k = get(list, k);
db_lnode_t* node_j = get(list, j);
if (compare(node_k->data, node_j->data) < 0) {
k = j;
}
}
node_k = get(list, k);
db_lnode_t* node_i = get(list, i);
if (k != i) {
swap(node_k, node_i);
}
}
}
资源文件列表:
link_list_20240725_202602.zip 大约有1230个文件