macros.h 936 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*====================================================================
  2. macros.h
  3. macros used to access data structures and perform quick tests.
  4. ====================================================================*/
  5. /* general-purpose macros */
  6. #define SWAP(t,x,y) { t = x; x = y; y = t; }
  7. #define NEW(p,type) if ((p=(type *) malloc (sizeof(type))) == NULL) {\
  8. printf ("Out of Memory!\n");\
  9. exit(0);\
  10. }
  11. #define FREE(p) if (p) { free ((char *) p); p = NULL; }
  12. #define ADD( head, p ) if ( head ) { \
  13. p->next = head; \
  14. p->prev = head->prev; \
  15. head->prev = p; \
  16. p->prev->next = p; \
  17. } \
  18. else { \
  19. head = p; \
  20. head->next = head->prev = p; \
  21. }
  22. #define DELETE( head, p ) if ( head ) { \
  23. if ( head == head->next ) \
  24. head = NULL; \
  25. else if ( p == head ) \
  26. head = head->next; \
  27. p->next->prev = p->prev; \
  28. p->prev->next = p->next; \
  29. FREE( p ); \
  30. }