File: test23.c

package info (click to toggle)
uthash 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,788 kB
  • sloc: ansic: 9,838; makefile: 178; perl: 88; sh: 37; cpp: 30
file content (69 lines) | stat: -rw-r--r-- 1,286 bytes parent folder | download | duplicates (5)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include "uthash.h"

typedef struct {
    int key;
    int data;
    UT_hash_handle hh;
} item;

int main()
{
    item *i, *j, *items=NULL;
    int k;

    /* first item */
    k = 12345;
    i = (item*)malloc(sizeof(item));
    if (i == NULL) {
        exit(-1);
    }
    i->key = k;
    i->data = 0;
    HASH_ADD_INT(items,key,i);

    /* second item */
    k = 6789;
    i = (item*)malloc(sizeof(item));
    if (i == NULL) {
        exit(-1);
    }
    i->key = k;
    i->data = 0;
    HASH_ADD_INT(items,key,i);

    /* third item */
    k = 98765;
    i = (item*)malloc(sizeof(item));
    if (i == NULL) {
        exit(-1);
    }
    i->key = k;
    i->data = 0;
    HASH_ADD_INT(items,key,i);

    /* look them all up */
    k = 12345;
    HASH_FIND_INT(items, &k, j);
    if (j != NULL) {
        printf("found %d\n",k);
    }
    k = 6789;
    HASH_FIND_INT(items, &k, j);
    if (j != NULL) {
        printf("found %d\n",k);
    }
    k = 98765;
    HASH_FIND_INT(items, &k, j);
    if (j != NULL) {
        printf("found %d\n",k);
    }

    /* delete them not the way we prefer but it works */
    for(j=items; j != NULL; j=(item*)j->hh.next) {
        printf("deleting %d\n", j->key);
        HASH_DEL(items,j);
    }
    return 0;
}