aboutsummaryrefslogtreecommitdiffstats
path: root/doc/linkedlists.README
blob: 87e396fbc56be17f2a4ab7be340f8422a72a1db6 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
2nd version, implemented as macros.

	include <asterisk/linkedlists.h>

AST_LIST_ENTRY declares pointers inside the object structure : 

	struct ast_var_t {
	        char *name;
	        char *value;
	        AST_LIST_ENTRY(ast_var_t) listpointers;
	};

AST_LIST_HEAD declares a head structure, which is initialized
to AST_LIST_HEAD_NULL : 

	AST_LIST_HEAD(head, ast_var_t) head 

Next, we declare a pointer to this structure : 

	struct headtype *headp = head;

AST_LIST_INIT initializes the head pointer to a null value

	AST_LIST_INIT(headp);

AST_LIST_INSERT_HEAD inserts an element to the head of the list : 

	struct ast_var_t *node; 

	node=malloc(sizeof(struct ast_var_t));
	(...we fill data in struct....)
	data->name=malloc(100);
	strcpy(data->name,"lalalalaa");
	etc etc

	(then we insert the node in the head of the list :)

	AST_LIST_INSERT_HEAD(headp,node,listpointers);

AST_LIST_INSERT_HEAD_AFTER inserts an element after another : 

	struct ast_var_t *node1;
	...
	AST_LIST_INSERT_AFTER(node,node1,listpointers);

AST_LIST_REMOVE removes an arbitrary element from the head:

	AST_LIST_REMOVE(headp,node1,ast_var_t,listpointers);

AST_LIST_REMOVE_HEAD removes the entry at the head of the list: 

	AST_LIST_REMOVE(headp,listpointers);

AST_LIST_FIRST returns a pointer to the first element of the list;

	struct ast_var_t *firstnode;
	firstnode=AST_LIST_FIRST(headp);

AST_LIST_NEXT returns a pointer to the next element : 

	struct ast_var_t *nextnode;
	nextnode=AST_LIST_NEXT(firstnode,listpointers);

AST_LIST_TRAVERSE traverses all elements of the list : 

	struct ast_var_t *node;

	AST_LIST_TRAVERSE(headp,node,listpointers) {
		printf("%s\n",node->name);
	}

AST_LIST_EMPTY evaluates to a true condition if there are no elements on 
the list. 

To completely delete a list : 

	struct ast_var_t *vardata;

        while (!AST_LIST_EMPTY(headp)) {           /* List Deletion. */
                    vardata = AST_LIST_FIRST(head);
                    AST_LIST_REMOVE_HEAD(head, listpointers);
                    free(vardata->name);
		    free(vardata->value);
        }

AST_LIST_LOCK returns true if it can lock the list, AST_LIST_UNLOCK unlocks
the list : 

if (AST_LIST_LOCK(headp)) {
	...do all list operations here...
	AST_LIST_UNLOCK(headp);
} else {
	ast_log(LOG_WARNING,"List locked bla bla bla\n");
}