linked.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // linked.hpp - linked list and linked list node classes
  2. // written June 2, 1992 by Jonathan Clark (at home)
  3. // these classes provide the basic groundwork for any future linked list
  4. // please derive your own linked_node subclass and define the virtual
  5. // function compare.
  6. // example compare function
  7. // virtual int compare(void *n1, int field)
  8. // {return ((classname *) n1)->data > data);}
  9. // should return (1 if n1 is greater than (self)) else return 0
  10. // field is the value determined by linked_list::set_sort_field
  11. // this defaults to 1
  12. #ifndef linkman
  13. #define linkman
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #define loop(controll,first,inside) { (linked_node *)controll=first; \
  18. if (first) do { inside (linked_node *) controll=controll->next(); } \
  19. while ((linked_node *) controll!=first); }
  20. #define loopt(type,controll,first,inside) { controll=(type *)(first); \
  21. if (first) do { inside controll=(type *)(controll->next()); } \
  22. while (controll!=(type *)(first)); }
  23. #define loop_rev(controll,last,inside) { (linked_node *)controll=last; \
  24. if (first) do { inside (linked_node *) controll=controll->last(); } \
  25. while ((linked_node *) controll!=last); }
  26. #define loopct(type,controll,first,cond,inside) { controll=(type *)first; \
  27. if (first && (cond)) do { inside controll=(type *)controll->next(); } \
  28. while (controll!=(type *)first && (cond)); }
  29. #define loop_fort(type,controll,first,x) \
  30. int x=0; \
  31. if (first) \
  32. for (controll=(type *)(first); \
  33. (!x || (controll)!=(type *)(first));\
  34. controll=(type *)(controll->next()),x++)
  35. #define loop_forct(type,controll,first,cond,x) int x=0; if (first) for \
  36. (controll=(type *)(first);cond && (!x || controll!=(type *)(first));\
  37. controll=(type *)(controll->next()),x++)
  38. class linked_node
  39. {
  40. class linked_node *nextp, *lastp;
  41. public:
  42. virtual int compare(void *n1, int field) {return(0);} // default is = (equal)
  43. class linked_node *next() {return nextp;}
  44. class linked_node *last() {return lastp;}
  45. void set_next(class linked_node *p) {nextp=p;}
  46. void set_last(class linked_node *p) {lastp=p;}
  47. virtual ~linked_node() { ; }
  48. linked_node() { nextp=NULL; lastp=NULL; }
  49. };
  50. // this is the basic class for all linked_list
  51. // it's features should beself explanitory
  52. // openly use the functions listed after the keyword PUBLIC
  53. // type conversions may be nessary if you derive a class of nodes of your own
  54. // for example shape is an class derived from linked_node.
  55. // to add a shape to linked lis I have to say
  56. // mylist.add_end( (linked_node *) myshape_pointer);
  57. // unlink removes a node from the list via pointers but does not deallocate
  58. // it from the heap
  59. // the destructor for linked_list will get dispose of all the nodes as
  60. // well, so if you don't want something deleted then you must unlink
  61. // it from the list before the destructor is called
  62. class linked_list
  63. {
  64. class linked_node *fn, *cn; // first and current nodes
  65. int nn; char sortby;
  66. public :
  67. linked_list(linked_node *first=NULL);
  68. void add_front(class linked_node *p);
  69. void add_end(class linked_node *p);
  70. void insert(class linked_node *p);
  71. void set_sort_field(int x) {sortby=x;} // this is passed to compare
  72. class linked_node *current() {return cn;}
  73. class linked_node *first() {return fn;}
  74. class linked_node *last() {return fn->last();}
  75. class linked_node *get_node(int x);
  76. void set_current(class linked_node *p) {cn=p;}
  77. void go_first() {cn=fn;}
  78. void go_end() {cn=fn->last();}
  79. void go_next() {cn=cn->next();}
  80. void go_last() {cn=cn->last();}
  81. int number_nodes() {return nn;}
  82. int node_number(linked_node *p);
  83. int unlink(linked_node *p);
  84. ~linked_list();
  85. };
  86. #endif