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