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);
    }

    };

  • 相关阅读:
    早该知道的7个JavaScript技巧
    ASP.NET 实现伪静态网页方法
    Nginx http大文件断点续传分块上传
    java http大文件断点续传分块上传
    B/S http大文件断点续传上传
    前端 http大文件断点续传上传
    百度WebUploader http大文件断点续传上传
    webuploader http大文件断点续传上传
    ceph 之recovery machhine
    docker private registry使用
  • 原文地址:https://www.cnblogs.com/lxk613/p/2314931.html
Copyright © 2011-2022 走看看