List.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:10k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //=============================================================================
  2. //  Microsoft (R) Bloodhound (tm). Copyright (C) 1991-1999.
  3. //
  4. //  MODULE: list.h
  5. //
  6. //  Modification History
  7. //
  8. //  raypa           03/17/93    Created.
  9. //=============================================================================
  10. #if !defined(_LIST_)
  11. #define _LIST_
  12. #pragma pack(1)
  13. //=============================================================================
  14. //  The LINK structure is used to chain structures together into a list.
  15. //=============================================================================
  16. typedef struct _LINK *LPLINK;
  17. typedef struct _LINK
  18. {
  19.     LPLINK     PrevLink;                    //... Previous or back pointer.
  20.     LPLINK     NextLink;                    //... Next or forward pointer.
  21. } LINK;
  22. //=============================================================================
  23. //  The LIST data structure.
  24. //=============================================================================
  25. typedef struct _LIST
  26. {
  27.     LPLINK      Tail;                       //... List Tail pointer.
  28.     LPLINK      Head;                       //... List Head pointer.
  29.     DWORD       Length;                     //... List Length.
  30. } LIST;
  31. typedef LIST *LPLIST;
  32. #ifndef NO_INLINE
  33. #ifndef INLINE
  34. #define INLINE __inline
  35. #endif
  36. //=============================================================================
  37. //  FUNCTIONS.
  38. //=============================================================================
  39. INLINE LPLINK WINAPI GetPrevLink(LPLINK Link)
  40. {
  41.     return Link->PrevLink;
  42. }
  43. INLINE LPLINK WINAPI GetNextLink(LPLINK Link)
  44. {
  45.     return Link->NextLink;
  46. }
  47. INLINE LPLINK WINAPI GetHeadOfList(LPLIST List)
  48. {
  49.     return List->Head;
  50. }
  51. INLINE LPLINK WINAPI GetTailOfList(LPLIST List)
  52. {
  53.     return List->Tail;
  54. }
  55. INLINE DWORD WINAPI GetListLength(LPLIST List)
  56. {
  57.     return List->Length;
  58. }
  59. //=============================================================================
  60. //  FUNCTION: InitializeList()
  61. //
  62. //  Modification History
  63. //                               
  64. //  raypa           04/15/93        Created
  65. //=============================================================================
  66. INLINE LPLIST WINAPI InitializeList(LPLIST List)
  67. {
  68.     List->Head    = (LPLINK) 0L;
  69.     List->Tail    = (LPLINK) 0L;
  70.     List->Length  = 0;
  71.     return List;
  72. }
  73. //=============================================================================
  74. //  FUNCTION: AddLinkToLink()
  75. //
  76. //  Modification History
  77. //                               
  78. //  raypa           04/15/93        Created
  79. //=============================================================================
  80. INLINE VOID WINAPI AddLinkToLink(LPLINK DstLink, LPLINK SrcLink)
  81. {
  82.     //=========================================================================
  83.     //  Make the source link point at the destination link.
  84.     //=========================================================================
  85.     SrcLink->PrevLink = DstLink;
  86.     SrcLink->NextLink = DstLink->NextLink;
  87.     //=========================================================================
  88.     //  Make the destination link point at the source link.
  89.     //=========================================================================
  90.     DstLink->NextLink->PrevLink = SrcLink;
  91.     DstLink->NextLink = SrcLink;
  92. }
  93. //=============================================================================
  94. //  FUNCTION: AddToList()
  95. //
  96. //  Modification History
  97. //                               
  98. //  raypa           04/15/93        Created
  99. //=============================================================================
  100. INLINE LPLINK WINAPI AddToList(LPLIST List, LPLINK DstLink, LPLINK SrcLink)
  101. {
  102.     //=========================================================================
  103.     //  Grow the list length by one.
  104.     //=========================================================================
  105.     List->Length++;
  106.     //=========================================================================
  107.     //  If SrcLink is NULL then add DstLink to the end of the list.
  108.     //=========================================================================
  109.     if ( SrcLink == (LPLINK) 0L )
  110.     {
  111.         //=====================================================================
  112.         //  If the tail pointer is NULL then the list is empty.
  113.         //=====================================================================
  114.         if ( List->Tail != (LPLINK) 0L )
  115.         {
  116.             AddLinkToLink(List->Tail, DstLink);
  117.         }
  118.         else
  119.         {
  120.             DstLink->PrevLink = DstLink;
  121.             DstLink->NextLink = DstLink;
  122.             List->Head = DstLink;
  123.         }
  124.         return (List->Tail = DstLink);
  125.     }
  126.     //=========================================================================
  127.     //  If DstLink is NULL then add SrcLink to the front of the list.
  128.     //=========================================================================
  129.     if ( DstLink == (LPLINK) 0L )
  130.     {
  131.         //=====================================================================
  132.         //  If the head pointer is NULL then the list is empty.
  133.         //=====================================================================
  134.         if ( List->Head != (LPLINK) 0L )
  135.         {
  136.             AddLinkToLink(List->Head, SrcLink);
  137.         }
  138.         else
  139.         {
  140.             SrcLink->PrevLink = SrcLink;
  141.             SrcLink->NextLink = SrcLink;
  142.             List->Tail = SrcLink;
  143.         }
  144.         return (List->Head = SrcLink);
  145.     }
  146.     //=========================================================================
  147.     //  Neither DstLink nor SrcLink is NULL so link them together.
  148.     //=========================================================================
  149.     AddLinkToLink(DstLink, SrcLink);
  150.     return SrcLink;
  151. }
  152. //=============================================================================
  153. //  FUNCTION: DeleteFromList()
  154. //
  155. //  Modification History
  156. //                               
  157. //  raypa           04/15/93        Created
  158. //=============================================================================
  159. INLINE LPLINK WINAPI DeleteFromList(LPLIST List, LPLINK Link)
  160. {
  161.     //=========================================================================
  162.     //  If the list is empty then return NULL.
  163.     //=========================================================================
  164.     if ( List->Length != 0 )
  165.     {
  166.         //=====================================================================
  167.         //  If the list length is not zero then we may need to fixup head and
  168.         //  tail pointers in the event we delete the first or last link,
  169.         //  respectively.
  170.         //=====================================================================
  171.         if ( --List->Length != 0 )
  172.         {
  173.             //=================================================================
  174.             //  If we are deleting the front link then fixup the head pointer.
  175.             //=================================================================
  176.             if ( List->Head == Link )
  177.             {
  178.                 List->Head = List->Head->NextLink;
  179.             }
  180.             //=================================================================
  181.             //  If we are deleting the end link then fixup the tail pointer.
  182.             //=================================================================
  183.             if ( List->Tail == Link )
  184.             {
  185.                 List->Tail = List->Tail->PrevLink;
  186.             }
  187.             //=================================================================
  188.             //  Now we can unlink this link from the list.
  189.             //=================================================================
  190.             Link->NextLink->PrevLink = Link->PrevLink;
  191.             Link->PrevLink->NextLink = Link->NextLink;
  192.         }
  193.         else
  194.         {
  195.             //=================================================================
  196.             //  There is only one link on the list and we just deleted it.
  197.             //=================================================================
  198.             List->Head = (LPLINK) 0L;
  199.             List->Tail = (LPLINK) 0L;
  200.         }
  201.         return Link;
  202.     }
  203.     return (LPLINK) 0L;
  204. }
  205. //=============================================================================
  206. //  FUNCTION: AddToFrontOfList()
  207. //
  208. //  Modification History
  209. //                               
  210. //  raypa           04/15/93        Created
  211. //=============================================================================
  212. INLINE LPLINK WINAPI AddToFrontOfList(LPLIST List, LPLINK Link)
  213. {
  214.     return AddToList(List, (LPLINK) 0L, Link);
  215. }
  216. //=============================================================================
  217. //  FUNCTION: AddToEndOfList()
  218. //
  219. //  Modification History
  220. //                               
  221. //  raypa           04/15/93        Created
  222. //=============================================================================
  223. INLINE LPLINK WINAPI AddToEndOfList(LPLIST List, LPLINK Link)
  224. {
  225.     return AddToList(List, Link, (LPLINK) 0L);
  226. }
  227. //=============================================================================
  228. //  FUNCTION: DeleteFromFrontOfList()
  229. //
  230. //  Modification History
  231. //                               
  232. //  raypa           04/15/93        Created
  233. //=============================================================================
  234. INLINE LPLINK WINAPI DeleteFromFrontOfList(LPLIST List)
  235. {
  236.     return DeleteFromList(List, GetHeadOfList(List));
  237. }
  238. //=============================================================================
  239. //  FUNCTION: DeleteFromEndOfList()
  240. //
  241. //  Modification History
  242. //                               
  243. //  raypa           04/15/93        Created
  244. //=============================================================================
  245. INLINE LPLINK WINAPI DeleteFromEndOfList(LPLIST List)
  246. {
  247.     return DeleteFromList(List, GetTailOfList(List));
  248. }
  249. #endif
  250. #pragma pack()
  251. #endif