单向链表的实现

单向链表的头文件list.h文件:

#ifndef _LIST_H_
#define _LIST_H_

#include<stdlib.h>

/**** variable definition ****/
typedef struct ListElmt_ {
    void * data;
    struct ListElmt_ *next;
} ListElmt;

typedef struct List_ {
    int size;
    ListElmt *head;
    ListElmt *tail;

    int (*match)(const void *key1, const void *key2);
    void (*destory)(void *data);
} List;

/**** function definition ****/
void list_init(List *list, void (*destory)(void *data));
void list_destory(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);

#define list_size(list)         ((list)->size)
#define list_head(list)         ((list)->head)
#define list_tail(list)         ((list)->tail)
#define list_is_head(list, element)   ((element) == (list)->head ? 1 : 0)
#define list_is_tail(element)   ((element)->next == NULL ? 1 : 0)
#define list_data(element)      ((element)->data)

#endif //_LIST_H_

单向链表的实现文件list.c:

/*  List.h and List.c
 **
 ** This software is licensed under the terms of the GNU General Public
 ** License version 2, as published by the Free Software Foundation, and
 ** may be copied, distributed, and modified under those terms.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** Copyright (c) 2013-2018 Leo Wang.
 **
 ** PURPOSE: standard list funtion.
 **
 ** CHANGE HISTORY:
 **
 ** Version      Date            Author          Description
 ** 1.0.0        2013-05-25      leowang         created
 **
 */

#include "list.h"

/**
  * void list_init(List *list, void (*destory)(void *data))
  *
  * <Longer description>
  * This funtion is to initialize funtion of the list.
  *
  * @param  List *list : the list pointer
  * @param  void (*destory)(void *data) : the destory funtion to release the data
  * @return void : no return parameter
  */
void list_init(List *list, void (*destory)(void *data))
{
    /**** initialize the list ****/
    list->size = 0;
    list->head = NULL;
    list->tail = NULL;
    list->destory = destory;
}

/**
  * void list_destory(List *list)
  *
  * <Longer description>
  * This funtion is to destory the data of the list.
  *
  * @param  List *list : the list pointer
  * @return void : no return parameter
  */
void list_destory(List *list)
{
    void *data;
    while(list_size(list)) {
        if((list_rem_next(list, NULL, (void **)&data) == 0) && (list->destory != NULL)) {
            list->destory(data);
        }
    }

    memset(list, 0, sizeof(list));
    return;
}

/**
  * int list_ins_next(List *list, ListElmt *element, const void *data)
  *
  * <Longer description>
  * This funtion is to add a new element int the list. If the element is NULL, add the data
  * in the end of the list; If the element is not NULL, add the data just after the element.
  *
  * @param  List *list : the list pointer
  * @param  ListElmt *element : the new data will add after this element, the new data will
  *                             add in the end of the list if this element is NULL.
  * @param  const void *data : the data will be added to the list
  * @return int : 1 means fail and 0 means successful
  */
int list_ins_next(List *list, ListElmt *element, const void *data)
{
    ListElmt *new_element;

    new_element = (ListElmt *) malloc(sizeof(ListElmt));
    if(new_element == NULL)
        return -1;
    
    new_element->data = (void *)data;

    if(element == NULL) {
        if(list_size(list) == 0)
            list->tail = new_element;

        new_element->next = list->head;
        list->head = new_element;
    } else {
        if(element == NULL)
            list->tail = new_element;
        
        new_element->next = element->next;
        element->next = new_element;
    }

    list->size++;

    return 0;
}

/**
  * int list_rem_next(List* list, ListElmt *element, void **data)
  *
  * <Longer description>
  * This funtion is to remove a element int the list. If the element is NULL, remove the
  * data from the head of the list; If the element is not NULL, remove the data just after
  * the element.
  *
  * @param  List *list : the list pointer
  * @param  ListElmt *element : the data will remove after this element, the data will be
  *                             removed from the front of the list if this element is NULL.
  * @param  const void *data : the data will be removed from the list
  * @return int : 1 means fail and 0 means successful
  */

int list_rem_next(List* list, ListElmt *element, void **data)
{
    ListElmt *old_element;

    if(list_size(list) == 0)
        return -1;

    if(element == NULL) {
        *data = list_head(list)->data;
        old_element = list_head(list);
        list->head = old_element->next;

        if(list_size(list) == 1)
            list->tail = NULL;
    } else {
        if(element->next == NULL)
            return -1;

        old_element = element->next;
        *data = old_element->data;
        element->next = old_element->next;

        if(element->next == NULL)
            list->tail = element;
    }

    free(old_element);

    list->size--;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值