数据结构_链表_C语言

先把代码贴上,后面补注释跟说明

1、头文件

common.h

#ifndef _COMMON_H
#define _COMMON_H

typedef enum Ret{
    ERROR_OK = 0x00000001,
    ERROR_MALLOC = 0x00000002,
    ERROR_NULL = 0x00000003,
    ERROR_FAILED = 0x00000004
} Ret;

#endif

list.h

#ifndef _LIST_H
#define _LIST_H

#include"common.h"

typedef void (*DataPrintFunc)(void* data);
typedef Ret (*DataCompareFunc)(void* src, void* dst);
typedef Ret (*DataDeleteFunc)(void* data);

typedef struct _Node
{
    void* data;
    struct _Node* next;
    struct _Node* pre;
} Node;
            
typedef struct _List
{
    Node* head;
    Node* end;
    int size;
} List;

Ret list_create(List** thiz);
Ret list_append(List* thiz, void* data);
Ret list_add(List* thiz, void* data, int index);
Ret list_del(List* thiz, int index, DataDeleteFunc del_func);
Ret list_get(List* thiz, int index, void** data);
Ret list_get_node(List* thiz, int index, Node** node);
Ret list_find(List* thiz, void* data, int* index, DataCompareFunc compare_func);
Ret list_foreach(List* thiz, DataPrintFunc print_func);
Ret list_clean(List* thiz, DataDeleteFunc del_func);
Ret list_destory(List** thiz);

#endif

2、逻辑实现

list.c

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

Ret list_create(List** thiz)
{
    List* list = NULL;

    list = (List*) malloc(sizeof(List));
    if (list == NULL) {
        return ERROR_MALLOC;
    }

    list->size = 0;
    list->head = NULL;
    list->end = NULL;

    *thiz = list;

    return ERROR_OK;
}

Ret list_append(List* thiz, void* data)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    Node* node = NULL;

    node = (Node*) malloc(sizeof(Node));
    if (node == NULL) {
        return ERROR_MALLOC;
    }

    node->next = NULL;
    node->pre = NULL;
    node->data = data;

    if (thiz->size == 0) {
        thiz->head = node;
    } else {
        thiz->end->next = node;
        node->pre = thiz->end;
    }

    thiz->end = node;
    thiz->size = thiz->size + 1;

    return ERROR_OK;
}

Ret list_add(List* thiz, void* data, int index)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    if (index < 0 || index > thiz->size) {
        return ERROR_FAILED;
    } else if (index == thiz->size || (index == 0 && thiz->size==0)) {
        Ret ret = ERROR_OK;

        ret = list_append(thiz, data);
        
        return ret;
    }

    Node* node = NULL;

    node = (Node*) malloc(sizeof(Node));
    if (node == NULL) {
        return ERROR_MALLOC;
    }

    node->next = NULL;
    node->pre = NULL;
    node->data = data;

    if (index == 0) {
        node->next = thiz->head;
        thiz->head->pre = node;
        thiz->head = node;
    } else {
        Node* pre_node = NULL;
        Node* next_node = NULL;
        int i = 0;

        pre_node = thiz->head;
        next_node = thiz->head->next;

        for (i = 1; i < index; i++) {
            pre_node = pre_node->next;
            next_node = next_node->next;
        }
        
        pre_node->next = node;
        node->pre = pre_node;
        node->next = next_node;
        next_node->pre = node;
    }

    thiz->size = thiz->size + 1;

    return ERROR_OK;
}

Ret list_del(List* thiz, int index, DataDeleteFunc del_func)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    if (index < 0 || index >= thiz->size) {
        return ERROR_FAILED;
    }

    int i = 0;
    Ret ret = ERROR_OK;

    Node* del_node = NULL;
    
    if (index == 0) {
        del_node = thiz->head;
        thiz->head = thiz->head->next;

        if (thiz->head != NULL) {
            thiz->head->pre = NULL; 
        }     
    } else {
        int i = 0;
        Node* pre_node = NULL;

        pre_node = thiz->head;

        for (i = 1; i<index; i++) {
            pre_node = pre_node->next;
        }

        del_node = pre_node->next;
        pre_node->next = pre_node->next->next;
        pre_node->next->pre = pre_node;
    }

    ret = del_func(del_node->data);
    if (ret != ERROR_OK) {
        return ret;
    } 

    free(del_node);
    del_node = NULL;

    thiz->size = thiz->size - 1;

    if (thiz->size == 0) {
        thiz->head = NULL;
        thiz->end = NULL;
    }

    return ERROR_OK;
}

Ret list_get(List* thiz, int index, void** data)
{
    Node* node = NULL;
    Ret ret = ERROR_OK;

    ret = list_get_node(thiz, index, &node);
    if (ret != ERROR_OK) {
        return ret;
    }

    *data = node->data;
    
    return ERROR_OK;
}

Ret list_get_node(List* thiz, int index, Node** node)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    if (index < 0 || index >= thiz->size) {
        return ERROR_FAILED;
    }
    
    Node* obj_node = NULL;
    int i = 1;

    for (obj_node = thiz->head; i < index; i++) {
        obj_node = obj_node->next;
    }

    *node = obj_node;

    return ERROR_OK;
}

Ret list_find(List* thiz, void* data, int* index, DataCompareFunc compare_func)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    Ret ret = ERROR_OK;
    Node* node = NULL;
    int i = 0;

    for (node = thiz->head; node != NULL; i++) {
        ret = compare_func(node->data, data);
        if (ret == ERROR_OK) {
            *index = i;

            return ret;
        }

        node = node->next;
    }

    return ERROR_NULL;
}

Ret list_foreach(List* thiz, DataPrintFunc print_func)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    Ret ret = ERROR_OK;
    Node* node = NULL;

    /*for (node = thiz->head; node != NULL; ) {
        print_func(node->data);

        node = node->next;
    }*/

    for (node = thiz->end; node != NULL; ) {
        print_func(node->data);

        node = node->pre;
    }

    return ERROR_OK; 
}

Ret list_clean(List* thiz, DataDeleteFunc del_func)
{
    if (thiz == NULL) {
        return ERROR_NULL;
    }

    Ret ret = ERROR_OK;
    Node* node = NULL;
    int i = 0;

    for (node = thiz->head; node != NULL; i++) {
        ret = list_del(thiz, 0, del_func);
        if (ret != ERROR_OK) {
            printf("del %dth error of clean thiz.\n", i);

            return ret;
        }
        node = thiz->head;
    }

    return ERROR_OK; 
}

Ret list_destory(List** thiz)
{
    List* list = NULL;

    list = *thiz;

    if (list == NULL) {
        printf("the thiz is null, not need to destory.\n");
        return ERROR_NULL;
    }

    if (list->size != 0) {
        printf("Please clean it before destory.\n");
        return ERROR_FAILED;
    }

    free(list);
    list = NULL;

    *thiz = list;

    return ERROR_OK;
}

3、测试用例

main.c

#include<stdio.h>
#include<stdlib.h>
#include"list.h"

static void data_print(void* data);
static Ret data_compare(void* src, void* dst);
static Ret del_data(void* data);
static Ret list_del_data(List* thiz, int node_data);

int main(int argc, char* argv[]) 
{
    Ret ret = ERROR_OK;
    List* thiz = NULL;
    int i = 0;

    printf("-----create------\n");

    ret = list_create(&thiz);
    if (ret != ERROR_OK) {
        printf("Error code = %p\n", ret);

        return ret;
    }
    printf("Created.\n");

    printf("-----append------\n");

    for (i = 0; i < 10; i++) {
        int* data = NULL;

        data = (int*) malloc(sizeof(int*));
        if (data == NULL) {
            return ERROR_MALLOC;
        }

        *data = (i+1) * 10;

        ret = list_append(thiz, data);
        if (ret != ERROR_OK) {
            return ret;
        }
    }

    list_foreach(thiz, data_print);

    
    printf("-----add------\n");

    int* add_data = NULL;

    add_data = (int*) malloc(sizeof(int*));
    if (add_data == NULL) {
        return ERROR_MALLOC;
    }

    *add_data = 60;

    ret = list_add(thiz, add_data, 8);
    if (ret != ERROR_OK) {
        return ret;
    }
    
    list_foreach(thiz, data_print);

    printf("-----get------\n");

    int* data = NULL;
    int temp = 70;
    int index = 0;

    data = &temp;

    ret = list_find(thiz, data, &index, data_compare);
    if (ret != ERROR_OK) {
        return ret;
    }

    printf("index of %d:%d\n", temp, index);

    printf("-----del------\n");
    ret = list_del_data(thiz, 60);
    if (ret != ERROR_OK) {
        return ret;
    }

    list_foreach(thiz, data_print);

    printf("-----clean------\n");

    ret = list_clean(thiz, del_data);
    if (ret != ERROR_OK) {
        return ret;
    }

    list_foreach(thiz, data_print);

    printf("the size of thiz is:%d.\n", thiz->size);

    printf("-----destory------\n");

    ret = list_destory(&thiz);
    if (ret != ERROR_OK) {
        return ret;
    }
    
    printf("the thiz is:NULL.\n");

    list_foreach(thiz, data_print);

    return 99;
}

static void data_print(void* data)
{
    int* temp = NULL;

    temp = (int*) data;

    printf("data:%d\n", *temp);
}

static Ret data_compare(void* src, void* dst)
{
    int* src_int = NULL;
    int* dst_int = NULL;

    src_int = (int*) src;
    dst_int = (int*) dst;

    if (*src_int == *dst_int) {
        return ERROR_OK;
    }

    return ERROR_FAILED;
}

static Ret del_data(void* data)
{
    if (data == NULL) {
        return ERROR_NULL;
    }

    free(data);
    data = NULL;

    return ERROR_OK;
}

Ret list_del_data(List* thiz, int node_data) {
    
    Ret ret = ERROR_OK;
    int* data = NULL;
    int index = 0;

    data = &node_data;

    ret = list_find(thiz, data, &index, data_compare);
    if (ret != ERROR_OK) {
        return ret;
    }

    ret = list_del(thiz, index, del_data);
    if (ret != ERROR_OK) {
        return ret;
    }

    list_del_data(thiz, node_data);

    return ERROR_OK;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值