utlist使用总结

参考:https://blog.csdn.net/a123441/article/details/90374650

utlist.h获取(可直接粘贴):https://gitee.com/yanbib/libcoap2/blob/master/utlist.h

使用手册(暂时只找到uthash的):http://troydhanson.github.io/uthash/userguide.html#_delete_item

使用utlist双向链表可以模拟栈、队列。示例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <limits.h>
 4 #include <string.h>
 5 #include "utlist.h"
 6 #include "uthash.h"
 7 
 8 #define LEN 5
 9 
10 typedef struct el {
11     char name[LEN];
12     struct el *next;
13     struct el *prev;
14 } el;
15 
16 int Cmp(el *a, el *b)
17 {
18     return strcmp(a->name, b->name);
19 }
20 
21 int main()
22 {
23     el *head = NULL;
24     el *input = NULL;
25     el *temp = NULL;
26 
27     for (int i = 0; i < LEN; i++) {
28         input = (el *)malloc(sizeof(el));
29         memset(input, 0, sizeof(el));
30         scanf("%s", input->name);
31         DL_APPEND(head, input);
32     }
33 
34     DL_FOREACH(head, temp) {
35         printf("%s\n", temp->name);
36     }
37     printf("\n==========\n");
38 
39     DL_SORT(head, Cmp);
40 
41     DL_FOREACH(head, temp) {
42         printf("%s\n", temp->name);
43     }
44     printf("\n==========\n");
45     
46     DL_DELETE(head, head);
47     printf("%s\n", head->name);
48     printf("%s\n", head->prev->name);
49     DL_DELETE(head, head->prev);
50     printf("%s\n", head->name); 
51     printf("%s\n", head->prev->name);
52 
53     return 0;
54 }

运行示例:

utlist使用总结

utlist使用总结

上一篇:Centos7 解决gcc 4.85版本,升级更改版本gcc


下一篇:剑指 Offer 56 - II. 数组中数字出现的次数 II