C语言使用utlist实现的双向链表

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

utlist 下载地址:https://github.com/troydhanson/uthash

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utlist.h"
 
#define BUFLEN 20
 
typedef struct el {
    char bname[BUFLEN];
    struct el *next, *prev;
} el;
 
int namecmp(el *a, el *b) {
    return strcmp(a->bname,b->bname);
}
 
el *head = NULL; /* important- initialize to NULL! */
 
int main(int argc, char *argv[]) {
    el *name, *elt, *tmp, etmp;
 
    char linebuf[BUFLEN];
    int count;
    FILE *file;
 
    if ( (file = fopen( "test11.dat", "r" )) == NULL ) {
        perror("can't open: ");
        exit(-1);
    }
 
    while (fgets(linebuf,BUFLEN,file) != NULL) {
        if ( (name = (el*)malloc(sizeof(el))) == NULL) exit(-1);
        strncpy(name->bname,linebuf,BUFLEN);
        DL_APPEND(head, name);
    }
    DL_SORT(head, namecmp);
    DL_FOREACH(head,elt) printf("%s", elt->bname);
    DL_COUNT(head, elt, count);
    printf("%d number of elements in list\n", count);
 
    memcpy(&etmp.bname, "WES\n", 5);
    DL_SEARCH(head,elt,&etmp,namecmp);
    if (elt) printf("found %s\n", elt->bname);
 
    /* now delete each element, use the safe iterator */
    DL_FOREACH_SAFE(head,elt,tmp) {
      DL_DELETE(head,elt);
    }
 
    fclose(file);
 
    return 0;
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小

下一篇:Linux下通过C语言操作SqlLite数据库