zoukankan      html  css  js  c++  java
  • 链表操作,获得泛型效果

    http://stackoverflow.com/questions/2583580/what-is-the-best-way-to-write-class-template-like-generic-code-in-c

    struct list_element_s{
    void* data;
    struct list_element_s next;
    };
    typedef struct list_element_s list_element;
    struct list_s
    {
    void (*destructor)(void* data);
    int (*com)(const void *src,const void* dest);
    unsigned int size;
    list_elememt *head;
    list_element * tail;
    };
    typedef struct list_s list;
    list *list_alloc(void(*destructor)(void* data));//alloc memory for the list
    int list_free(list* l);//free the alloced memory
    int list_insert(list* l,list_element* element,const void* data);//insert notes
    void* list_remove_next(list* l,list_element * element);//remove notes
    list* list_alloc(void(*destructor)(void* data))
    {
    list* h=NULL;
    if(h=calloc(1,sizeof(*l)))!=NULL){
    h->size=0;
    h->destructor=destructor;
    h->head=NULL;
    h->tail=NULL;
    }
    return h;
    }

    int list_free(list* h)
    {
    void *data;
    if(h=NULL||h->destructor=NULL){
    return -1;
    }
    while(h->size>0)
    {
    if(data=list_remove_next(1,NULL))!=NULL){
    list->destructor(data);
    }
    free(h);
    return 0;
    }
    }

    int list_insert_next(list* h,list_element* element,const void* data)
    {
    list_element* new=NULL;
    new=calloc(1,sizeof(*new));
    if(h==NULL||new==NULL){
    return -1;
    }
    new->data=(void*)data;
    new->next=NULL;
    if(NULL==element)
    {
    if(h->size==0)
    h->tail=new;
    new->next=h->head;
    h->head=new;
    }
    else{
    if(element->next==NULL) h->tial=new;
    new->next=element->next;
    element->next=new;
    }
    h->size++;
    return 0;
    }

    void *list_remove_next(list* h,list_element* element)
    {
    void* data=NULL;
    list_element *old==NULL;
    if(h==NULL||h->size==0){
    return NULL;
    }
    if(element==NULL){
    data=h->data;
    old=l->head;
    h->head=h->head->next;
    if(h->size==1){
    h->tail=NULL;
    }
    }
    else{
    if(element->next=NULL){
    return NULL;
    }
    data=element->next->data;
    old=element->next;
    element->next=old->next;
    if(element->next==NULL)
    h->tail=element;
    }
    }
    free(old);
    l->size--;
    return data;
    }

    #include <stdlib.h>
    #include <stdio.h>
    //#include "nmlist.h"

    void simple_free(void *data){
    free(data);
    }

    int main(int argc, char *argv[]){
    list *l = NULL;
    int i, *j;

    l = list_alloc(simple_free);
    for(i = 0; i < 10; i++){
    j = calloc(1, sizeof(*j));
    if(j != NULL){
    *j = i;
    list_insert_next(l, NULL, (void*) j);
    }
    }

    for(i = 0; i < 10; i++){
    j = (int*) list_remove_next(l, NULL);
    if(j != NULL){
    printf("%d \n", *j);
    }
    }

    list_free(l);

    return (0);
    }

    };

  • 相关阅读:
    Nginx 部署多个 web 项目(虚拟主机)
    Nginx 配置文件
    Linux 安装 nginx
    Linux 安装 tomcat
    Linux 安装 Mysql 5.7.23
    Linux 安装 jdk8
    Linux 安装 lrzsz,使用 rz、sz 上传下载文件
    springMVC 拦截器
    spring 事务
    基于Aspectj 注解实现 spring AOP
  • 原文地址:https://www.cnblogs.com/lxk613/p/2314931.html
Copyright © 2011-2022 走看看