LIST.CPP
上传用户:xr_qian
上传日期:2007-01-05
资源大小:443k
文件大小:7k
源码类别:

通讯/手机编程

开发平台:

DOS

  1. // ******************************************************************** //
  2. //                                                                      //
  3. //      LIST.CPP                                                        //
  4. //      Copyright (c) 1993, Michael Holmes and Bob Flanders             //
  5. //      C++ Communication Utilities                                     //
  6. //                                                                      //
  7. //      This file contains the definition and interface for             //
  8. //      the List class.  The List class implements a simple             //
  9. //      double-linked list with a single string item stored             //
  10. //      in the object.                                                  //
  11. //                                                                      //
  12. // ******************************************************************** //
  13. /* ******************************************************************** *
  14.  *
  15.  *  List class definition
  16.  *
  17.  * ******************************************************************** */
  18. class List
  19.     {
  20.     public:
  21.         List(void);                         // build a list header
  22.         List(char *s,                       // create a single entry
  23.              char *d);                      // ..with string and data
  24.         List(List *l,                       // add to end of list
  25.              char *s,                       // ..string name
  26.              char *d);                      // ..and data
  27.         List(char *s,                       // put at the head of list
  28.              char *d,                       // ..string and data
  29.              List *l);                      // ..list to chain to
  30.         char *Find(char *s);                // find string name
  31.        ~List();                             // destructor
  32.     private:
  33.         void EntryInit(char *s,             // initialize a entry
  34.                        char *d);            // ..with string and data
  35.         char *string_name,                  // string name
  36.              *data;                         // ..and data
  37.         List *prev,                         // pervious item pointer
  38.              *next;                         // next item pointer
  39.     };
  40. /* ******************************************************************** *
  41.  *
  42.  *  List -- build list header
  43.  *
  44.  * ******************************************************************** */
  45. List::List(void)
  46. {
  47. EntryInit("", "");                          // initialize new instance
  48. }
  49. /* ******************************************************************** *
  50.  *
  51.  *  List -- build a single list entry
  52.  *
  53.  * ******************************************************************** */
  54. List::List(char *s,                         // string name
  55.            char *d)                         // ..and data
  56. {
  57. EntryInit(s, d);                            // initialize new instance
  58. }
  59. /* ******************************************************************** *
  60.  *
  61.  *  List -- add an entry to the end of the list
  62.  *
  63.  * ******************************************************************** */
  64. List::List(List *l,                         // list to chain to
  65.            char *s,                         // string name
  66.            char *d)                         // ..and data
  67. {
  68. EntryInit(s, d);                            // build base instance
  69. while (l->next)                             // loop thru..
  70.     l = l->next;                            // ..to the end of the list
  71. l->next = this;                             // put this at the end
  72. this->prev = l;                             // ..and backward chain
  73. }
  74. /* ******************************************************************** *
  75.  *
  76.  *  List -- put an entry at the head of the list
  77.  *
  78.  * ******************************************************************** */
  79. List::List(char *s,                         // string name
  80.            char *d,                         // ..and data
  81.            List *l)                         // list to chain into
  82. {
  83. EntryInit(s, d);                            // build base instance
  84. this->prev = l;                             // set up backward link
  85. this->next = l->next;                       // ..and forward link
  86. l->next = this;                             // update list anchor
  87. if (this->next)                             // q. any more after us?
  88.     (this->next)->prev = this;              // a. yes .. set up link
  89. }
  90. /* ******************************************************************** *
  91.  *
  92.  *  EntryInit -- initialize list entry instance
  93.  *
  94.  * ******************************************************************** */
  95. void List::EntryInit(char *s,               // string name
  96.                      char *d)               // ..and data
  97. {
  98. string_name = data = (char *) 0;            // init string pointers
  99. prev = next = 0;                            // ..and list pointers
  100. if (*s)                                     // q. string name given?
  101.     {
  102.     string_name = new char[strlen(s) + 1];  // a. yes .. get memory and
  103.     strcpy(string_name, s);                 // ..copy for this instance
  104.     }
  105. if (*d)                                     // q. data given
  106.     {
  107.     data = new char[strlen(d) + 1];         // a. yes .. get memory and
  108.     strcpy(data, d);                        // ..copy for this instance
  109.     }
  110. }
  111. /* ******************************************************************** *
  112.  *
  113.  *  Find -- find a list entry by the string name
  114.  *
  115.  * ******************************************************************** */
  116. char    *List::Find(char *s)                // string name to search on
  117. {
  118. List *l = this;                             // work pointer
  119. for (;;)                                    // loop thru the list
  120.     {
  121.     if (l->string_name &&                   // q. string available?
  122.             NOT stricmp(s, l->string_name)) // ..and find the entry?
  123.         return(l->data);                    // a. yes .. quit here
  124.     if ((l = l->next) == 0)                 // q. end of list?
  125.         break;                              // a. yes .. exit loop
  126.     }
  127. return(0);                                  // else return empty-handed
  128. }
  129. /* ******************************************************************** *
  130.  *
  131.  *  ~List -- object destructor
  132.  *
  133.  * ******************************************************************** */
  134. List::~List()
  135. {
  136. if (string_name)                            // q. string name given?
  137.     delete string_name;                     // a. yes .. de-alloc space
  138. if (data)                                   // q. data string available?
  139.     delete data;                            // a. yes .. de-alloc space
  140. if (this->next)                             // q. anything after this?
  141.     (this->next)->prev = this->prev;        // a. yes .. de-chain prev
  142. if (this->prev)                             // q. anything before this?
  143.     (this->prev)->next = this->next;        // a. yes .. de-chain next
  144. }