list.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/list.h>
  20. #include <pj/assert.h>
  21. #include <pj/log.h>
  22. /**
  23. * \page page_pjlib_samples_list_c Example: List Manipulation
  24. *
  25. * Below is sample program to demonstrate how to manipulate linked list.
  26. *
  27. * \includelineno pjlib-samples/list.c
  28. */
  29. struct my_node
  30. {
  31. // This must be the first member declared in the struct!
  32. PJ_DECL_LIST_MEMBER(struct my_node);
  33. int value;
  34. };
  35. int main()
  36. {
  37. struct my_node nodes[10];
  38. struct my_node list;
  39. struct my_node *it;
  40. int i;
  41. // Initialize the list as empty.
  42. pj_list_init(&list);
  43. // Insert nodes.
  44. for (i=0; i<10; ++i) {
  45. nodes[i].value = i;
  46. pj_list_insert_before(&list, &nodes[i]);
  47. }
  48. // Iterate list nodes.
  49. it = list.next;
  50. while (it != &list) {
  51. PJ_LOG(3,("list", "value = %d", it->value));
  52. it = it->next;
  53. }
  54. // Erase all nodes.
  55. for (i=0; i<10; ++i) {
  56. pj_list_erase(&nodes[i]);
  57. }
  58. // List must be empty by now.
  59. pj_assert( pj_list_empty(&list) );
  60. return 0;
  61. };