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 10
 9 
10 typedef struct _node {
11     int num;
12     char str[LEN];
13     struct _node *prev;
14     struct _node *next;
15 } node;
16 
17 node *list = NULL;
18 
19 void Show()
20 {
21     node *tmp = NULL;
22 
23     printf("================\n");
24     DL_FOREACH(list, tmp) {
25         printf("%d %s\n", tmp->num, tmp->str);
26     }
27 }
28 
29 int Cmp(node *a, node *b)
30 {
31     return b->num - a->num;
32 }
33 
34 int Cmp1(node *a, node *b)
35 {
36     return strcmp(a->str, b->str);
37 }
38 
39 int main()
40 {
41     node *cur = NULL;
42     node *tmp = NULL;
43     char strs[LEN * LEN] = "asr.srh.hhet.ad.hs.";
44     char dlip[LEN] = ".";
45     char *token = strtok(strs, dlip);
46     int num = 0;
47 
48     while (token != NULL) {
49         tmp = (node *)malloc(sizeof(node));
50         memset(tmp, 0, sizeof(node));
51         num++;
52         tmp->num = num;
53         strcpy(tmp->str, token);
54         DL_APPEND(list, tmp);
55         token = strtok(NULL, dlip);
56     }
57 
58     Show();
59     DL_SORT(list, Cmp);
60     Show();
61     DL_SORT(list, Cmp1);
62     Show();
63 
64     tmp = list;
65     DL_DELETE(list, tmp);
66     Show();
67     printf(" %d %s\n", tmp->num, tmp->str);
68 
69     tmp = list->prev;
70     DL_DELETE(list, tmp);
71     Show();
72     printf(" %d %s\n", tmp->num, tmp->str);
73 
74     DL_FOREACH_SAFE(list, cur, tmp) {
75         DL_DELETE(list, cur);
76         free(cur);
77     }
78     Show();
79 
80     return 0;
81 }
View Code

 

 
posted @ 2021-08-26 00:38  hemeiwolong  阅读(777)  评论(0编辑  收藏  举报