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


本文摘自PHP中文网,作者大家讲道理,侵删。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

#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;

}</string.h></stdlib.h></stdio.h>

相关阅读 >>

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

更多相关阅读请进入《代码片段》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...