doc_slist_algorithms.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //[doc_slist_algorithms_code
  13. #include <boost/intrusive/circular_slist_algorithms.hpp>
  14. #include <cassert>
  15. struct my_node
  16. {
  17. my_node *next_;
  18. //other members...
  19. };
  20. //Define our own slist_node_traits
  21. struct my_slist_node_traits
  22. {
  23. typedef my_node node;
  24. typedef my_node * node_ptr;
  25. typedef const my_node * const_node_ptr;
  26. static node_ptr get_next(const_node_ptr n) { return n->next_; }
  27. static void set_next(node_ptr n, node_ptr next) { n->next_ = next; }
  28. };
  29. int main()
  30. {
  31. typedef boost::intrusive::circular_slist_algorithms<my_slist_node_traits> algo;
  32. my_node one, two, three;
  33. //Create an empty singly linked list container:
  34. //"one" will be the first node of the container
  35. algo::init_header(&one);
  36. assert(algo::count(&one) == 1);
  37. //Now add a new node
  38. algo::link_after(&one, &two);
  39. assert(algo::count(&one) == 2);
  40. //Now add a new node after "one"
  41. algo::link_after(&one, &three);
  42. assert(algo::count(&one) == 3);
  43. //Now unlink the node after one
  44. algo::unlink_after(&one);
  45. assert(algo::count(&one) == 2);
  46. //Now unlink two
  47. algo::unlink(&two);
  48. assert(algo::count(&one) == 1);
  49. return 0;
  50. }
  51. //]