home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / list.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  4.5 KB  |  237 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\notifyob
  4.  * File Name: list.h
  5.  * Purpose  : List manipulation functions.
  6.  */
  7.  
  8. #ifndef LIST_H_INCLUDED
  9.  
  10. #define LIST_H_INCLUDED
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <windows.h>
  15.  
  16.  
  17. template<class X, class Y> class List { 
  18.      struct Node {
  19.         X    item;
  20.         Y    key;
  21.         Node *next;
  22.      };
  23.  
  24.      Node  *m_head;
  25.      DWORD m_dwNodeCount;
  26.  
  27.   public:
  28.  
  29.               List ();
  30.  
  31.       virtual ~List();
  32.  
  33.      HRESULT  Insert (X item,
  34.                       Y key);
  35.  
  36.      HRESULT  Remove (X *item);
  37.  
  38.      HRESULT  RemoveThis (X item);
  39.  
  40.      HRESULT  RemoveByKey (Y key,
  41.                            X *item);
  42.  
  43.      VOID     RemoveAll(VOID);
  44.  
  45.      HRESULT  Find (DWORD dwIndex,
  46.                     X *item);
  47.  
  48.      HRESULT  FindByKey (Y key,
  49.                          X *item);
  50.  
  51.      DWORD    ListCount (VOID);
  52. };
  53.  
  54. template<class X, class Y> List<X, Y>::List ()
  55. {
  56.   m_head = NULL;
  57.   m_dwNodeCount = 0;
  58. }
  59.  
  60. template<class X, class Y> List<X, Y>::~List ()
  61. {
  62.   RemoveAll();
  63. }
  64.  
  65. template<class X, class Y> HRESULT List<X, Y>::Insert (X item,
  66.                                                        Y key)
  67. {
  68.   Node *pNewNode;
  69.  
  70.   pNewNode = new Node;
  71.  
  72.   if ( pNewNode ) {
  73.      pNewNode->item = item;
  74.      pNewNode->key = key;
  75.      pNewNode->next = NULL;
  76.  
  77.      if ( m_dwNodeCount ) {
  78.         pNewNode->next = m_head;
  79.      }
  80.  
  81.      m_head = pNewNode;
  82.      m_dwNodeCount++;
  83.   }
  84.  
  85.   return ( pNewNode ) ? S_OK : HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  86. }
  87.  
  88. template<class X, class Y> HRESULT List<X, Y>::Remove (X *item)
  89. {
  90.   Node *temp;
  91.  
  92.   if ( m_dwNodeCount == 0 ) {
  93.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  94.   }
  95.  
  96.   *item = m_head->item;
  97.   temp = m_head;
  98.   m_head = m_head->next;
  99.   delete temp;
  100.   m_dwNodeCount--;
  101.  
  102.   return S_OK;
  103. }
  104.   
  105. template<class X, class Y> HRESULT List<X, Y>::RemoveThis (X item)
  106. {
  107.   Node *temp;
  108.   Node *nodeToFind;
  109.  
  110.   if ( m_dwNodeCount == 0 ) {
  111.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  112.   }
  113.  
  114.   if ( m_head->item == item ) {
  115.  
  116.      nodeToFind = m_head;
  117.      m_head = m_head->next;
  118.   }
  119.   else {
  120.      for (temp = m_head; temp->next && (temp->next->item != item); ) {
  121.         temp = temp->next;
  122.      }
  123.  
  124.      if ( temp->next ) {
  125.         nodeToFind = temp->next;
  126.         temp->next = temp->next->next;
  127.      }
  128.      else
  129.         nodeToFind = NULL;
  130.   }
  131.  
  132.   if ( nodeToFind ) {
  133.      delete nodeToFind;
  134.      m_dwNodeCount--;
  135.      return S_OK;
  136.   }
  137.   else
  138.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  139. }
  140.  
  141. template<class X, class Y> HRESULT List<X, Y>::RemoveByKey (Y key,
  142.                                                             X *item)
  143. {
  144.   Node *temp;
  145.   Node *nodeToFind;
  146.  
  147.   if ( m_dwNodeCount == 0 ) {
  148.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  149.   }
  150.  
  151.   if ( m_head->key == key ) {
  152.  
  153.      nodeToFind = m_head;
  154.      m_head = m_head->next;
  155.   }
  156.   else {
  157.      for (temp = m_head; temp->next && (temp->next->key != key); ) {
  158.         temp = temp->next;
  159.      }
  160.  
  161.      if ( temp->next ) {
  162.         nodeToFind = temp->next;
  163.         temp->next = temp->next->next;
  164.      }
  165.      else
  166.         nodeToFind = NULL;
  167.   }
  168.  
  169.   if ( nodeToFind ) {
  170.      *item = nodeToFind->item;
  171.  
  172.      delete nodeToFind;
  173.      m_dwNodeCount--;
  174.  
  175.      return S_OK;
  176.   }
  177.   else
  178.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  179. }
  180.  
  181. template<class X, class Y> VOID List<X, Y>::RemoveAll (VOID)
  182. {
  183.   Node *temp;
  184.  
  185.   for (; m_dwNodeCount; --m_dwNodeCount) {
  186.      temp = m_head;
  187.      m_head = m_head->next;
  188.      delete temp;
  189.   }
  190.  
  191.   return;
  192. }
  193.  
  194. template<class X, class Y> HRESULT List<X, Y>::Find (DWORD dwIndex,
  195.                                                      X *item)
  196. {
  197.   Node  *temp;
  198.   DWORD i;
  199.  
  200.   if ( (m_dwNodeCount == 0) || (dwIndex > m_dwNodeCount) ) {
  201.  
  202.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  203.   }
  204.  
  205.   for (i=0, temp = m_head; i < dwIndex; ++i) {
  206.      temp = temp->next;
  207.   }
  208.  
  209.   *item = temp->item;
  210.   return S_OK;
  211. }
  212.  
  213. template<class X, class Y> HRESULT List<X, Y>::FindByKey (Y key,
  214.                                                           X *item)
  215. {
  216.   Node *temp;
  217.  
  218.   for (temp = m_head; temp && (temp->key != key); )
  219.      temp = temp->next;
  220.  
  221.   if ( temp ) {
  222.      *item = temp->item;
  223.      return S_OK;
  224.   }
  225.   else {
  226.      return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  227.   }
  228. }
  229.  
  230. template<class X, class Y> DWORD List<X, Y>::ListCount (VOID)
  231. {
  232.   return m_dwNodeCount;
  233. }
  234.  
  235. #endif
  236.  
  237.