美文网首页
C语言实现单向链表

C语言实现单向链表

作者: 锋芒不露大宝剑 | 来源:发表于2019-04-06 23:13 被阅读0次
    ElementList.h
    //
    //  ElementList.h
    //  CppStudy
    //
    //  Created by 李晋 on 2019/4/6.
    //  Copyright © 2019 李晋. All rights reserved.
    //
    
    #ifndef ElementList_h
    #define ElementList_h
    
    #ifdef __cplusplus
    extern "C" {
    #endif
        
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
        
        /*******************************/
        typedef struct Node {
            struct Node *next;
        }Node;
        
        /*******************************/
        typedef struct Element {
            Node node;
            void *data;
        }Element;
        typedef void (*PRINT_ELEMENT_LIST)(Element *data);
        typedef void (*FREE_DATA)(void *data);
        
        void printElement(Element *element, PRINT_ELEMENT_LIST print);
        
        /*******************************/
        typedef struct ElementList {
            Element *head;
            int size;
        }ElementList;
        
        ElementList * makeList(void);
        void print_list(const ElementList * const list, PRINT_ELEMENT_LIST print);
        
        int push_back(ElementList *list, void *data);
        void *pop(ElementList *list, FREE_DATA free_data);
        
        int insert(ElementList *list, unsigned int pos, void *data);
        void remove_list(ElementList *list, unsigned int pos, FREE_DATA free_data);
        
        Element *get_list(const ElementList *list, unsigned int pos);
        
    #ifdef __cplusplus
    } // extern "C"
    #endif
    
    
    
    #endif /* ElementList_h */
    
    
    ElementList.c
    
    //
    //  ElementList.c
    //  CppStudy
    //
    //  Created by 李晋 on 2019/4/6.
    //  Copyright © 2019 李晋. All rights reserved.
    //
    
    #include "ElementList.h"
    
    /*******************************/
    //typedef struct Node {
    //    struct Node *next;
    //}Node;
    
    
    /*******************************/
    //typedef struct Element {
    //    Node node;
    //}Element;
    Element * makeElement(void *data) {
        Element *element = (Element *)malloc(sizeof(Element));
        memset(element, 0, sizeof(Element));
        element->node.next = NULL;
        element->data = data;
        return element;
    }
    
    void freeElement(Element **element, FREE_DATA free_data) {
        if (element == NULL) {
            return;
        }
        free_data((*element)->data);
        free(*element);
        *element = NULL;
    }
    
    void printElement(Element *element, PRINT_ELEMENT_LIST print) {
        if (element != NULL) {
            print(element);
        }
    }
    
    /*******************************/
    //typedef struct ElementList {
    //    int size;
    //    Element *head;
    //}ElementList;
    ElementList * makeList(void) {
        ElementList *list = (ElementList *)malloc(sizeof(ElementList));
        memset(list, 0, sizeof(ElementList));
        list->head = malloc(sizeof(Element));
        if (list->head == NULL) {
            return NULL;
        }
        list->head->node.next = NULL;
        list->size = 0;
        return list;
    }
    
    void print_list(const ElementList * const list, PRINT_ELEMENT_LIST print) {
        if (list != NULL && list->size > 0) {
            Element *element = (Element *)list->head->node.next;
            while (element != NULL) {
                print(element);
                element = (Element *)element->node.next;
            }
        }
    }
    
    int push_back(ElementList *list, void *data) {
        if (NULL == list) {
            return -1;
        }
        Node *node = (Node *)list->head;
        while (node->next != NULL) {
            node = node->next;
        }
        Element *new_element = makeElement(data);
        if (new_element == NULL) {
            return -1;
        }
        node->next = (Node *)new_element;
        list->size++;
        return 0;
    }
    
    void *pop(ElementList *list, FREE_DATA free_data) {
        if (NULL == list || list->size <= 0 || free_data == NULL) {
            return NULL;
        }
        Node *current = (Node *)list->head;
        Node *next = current->next;
        while (next->next != NULL) {
            current = current->next;
            next = current->next;
        }
        current->next = NULL;
        list->size--;
        void *data = ((Element *)next)->data;
        freeElement((Element **)(&next), free_data);
        return data;
    }
    
    int insert(ElementList *list, unsigned int pos, void *data) {
        if (list == NULL) {
            return -1;
        }
        Node *last = NULL;
        Node *current = NULL;
        int i = 0;
        if (pos + 1 > list->size) {
            last = &(list->head->node);
            current = list->head->node.next;
            for (; i < pos + 1; i ++) {
                if (current == NULL) {
                    Element *element = makeElement(NULL);
                    last->next = (Node *)element;
                    current = last->next;
                    list->size ++;
                }
                if (i == pos) {
                    ((Element *)current)->data = data;
                }
                last = current;
                current = current->next;
            }
        } else {
            last = &(list->head->node);
            current = (Node *)list->head->node.next;
            while (i++ != pos) {
                last = current;
                current = current->next;
            }
            Element *element = makeElement(data);
            last->next = (Node *)element;
            element->node.next = current;
            list->size++;
        }
        return 0;
    }
    
    void remove_list(ElementList *list, unsigned int pos, FREE_DATA free_data) {
        if (list == NULL || pos + 1 > list->size) {
            return;
        }
        int idx = 0;
        Node *last = &(list->head->node);
        Node *current = last->next;
        while (idx++ != pos) {
            last = current;
            current = current->next;
        }
        last->next = current->next;
        list->size --;
    //    void *data = ((Element *)current)->data;
        freeElement((Element **)&current, free_data);
    }
    
    Element *get_list(const ElementList *list, unsigned int pos) {
        if (list == NULL || pos + 1 > list->size) {
            return NULL;
        }
        int idx = 0;
        Node *current = list->head->node.next;
        while (idx++ != pos) {
            current = current->next;
        }
        return (Element *)current;
    }
    

    相关文章

      网友评论

          本文标题:C语言实现单向链表

          本文链接:https://www.haomeiwen.com/subject/nkadiqtx.html