linkedlists.README 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 2nd version, implemented as macros.
  2. include <asterisk/linkedlists.h>
  3. AST_LIST_ENTRY declares pointers inside the object structure :
  4. struct ast_var_t {
  5. char *name;
  6. char *value;
  7. AST_LIST_ENTRY(ast_var_t) listpointers;
  8. };
  9. AST_LIST_HEAD declares a head structure, which is initialized
  10. to AST_LIST_HEAD_NULL :
  11. AST_LIST_HEAD(head, ast_var_t) head
  12. Next, we declare a pointer to this structure :
  13. struct headtype *headp = head;
  14. AST_LIST_INIT initializes the head pointer to a null value
  15. AST_LIST_INIT(headp);
  16. AST_LIST_INSERT_HEAD inserts an element to the head of the list :
  17. struct ast_var_t *node;
  18. node=malloc(sizeof(struct ast_var_t));
  19. (...we fill data in struct....)
  20. data->name=malloc(100);
  21. strcpy(data->name,"lalalalaa");
  22. etc etc
  23. (then we insert the node in the head of the list :)
  24. AST_LIST_INSERT_HEAD(headp,node,listpointers);
  25. AST_LIST_INSERT_HEAD_AFTER inserts an element after another :
  26. struct ast_var_t *node1;
  27. ...
  28. AST_LIST_INSERT_AFTER(node,node1,listpointers);
  29. AST_LIST_REMOVE removes an arbitrary element from the head:
  30. AST_LIST_REMOVE(headp,node1,ast_var_t,listpointers);
  31. AST_LIST_REMOVE_HEAD removes the entry at the head of the list:
  32. AST_LIST_REMOVE(headp,listpointers);
  33. AST_LIST_FIRST returns a pointer to the first element of the list;
  34. struct ast_var_t *firstnode;
  35. firstnode=AST_LIST_FIRST(headp);
  36. AST_LIST_NEXT returns a pointer to the next element :
  37. struct ast_var_t *nextnode;
  38. nextnode=AST_LIST_NEXT(firstnode,listpointers);
  39. AST_LIST_TRAVERSE traverses all elements of the list :
  40. struct ast_var_t *node;
  41. AST_LIST_TRAVERSE(headp,node,listpointers) {
  42. printf("%s\n",node->name);
  43. }
  44. AST_LIST_EMPTY evaluates to a true condition if there are no elements on
  45. the list.
  46. To completely delete a list :
  47. struct ast_var_t *vardata;
  48. while (!AST_LIST_EMPTY(headp)) { /* List Deletion. */
  49. vardata = AST_LIST_FIRST(head);
  50. AST_LIST_REMOVE_HEAD(head, listpointers);
  51. free(vardata->name);
  52. free(vardata->value);
  53. }
  54. AST_LIST_LOCK returns true if it can lock the list, AST_LIST_UNLOCK unlocks
  55. the list :
  56. if (AST_LIST_LOCK(headp)) {
  57. ...do all list operations here...
  58. AST_LIST_UNLOCK(headp);
  59. } else {
  60. ast_log(LOG_WARNING,"List locked bla bla bla\n");
  61. }