⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.cpp

📁 串行通信编程源码
💻 CPP
字号:
// ******************************************************************** //
//                                                                      //
//      LIST.CPP                                                        //
//      Copyright (c) 1993, Michael Holmes and Bob Flanders             //
//      C++ Communication Utilities                                     //
//                                                                      //
//      This file contains the definition and interface for             //
//      the List class.  The List class implements a simple             //
//      double-linked list with a single string item stored             //
//      in the object.                                                  //
//                                                                      //
// ******************************************************************** //


/* ******************************************************************** *
 *
 *  List class definition
 *
 * ******************************************************************** */

class List
    {
    public:
        List(void);                         // build a list header
        List(char *s,                       // create a single entry
             char *d);                      // ..with string and data
        List(List *l,                       // add to end of list
             char *s,                       // ..string name
             char *d);                      // ..and data
        List(char *s,                       // put at the head of list
             char *d,                       // ..string and data
             List *l);                      // ..list to chain to
        char *Find(char *s);                // find string name
       ~List();                             // destructor

    private:
        void EntryInit(char *s,             // initialize a entry
                       char *d);            // ..with string and data
        char *string_name,                  // string name
             *data;                         // ..and data
        List *prev,                         // pervious item pointer
             *next;                         // next item pointer
    };



/* ******************************************************************** *
 *
 *  List -- build list header
 *
 * ******************************************************************** */

List::List(void)
{

EntryInit("", "");                          // initialize new instance

}



/* ******************************************************************** *
 *
 *  List -- build a single list entry
 *
 * ******************************************************************** */

List::List(char *s,                         // string name
           char *d)                         // ..and data
{

EntryInit(s, d);                            // initialize new instance

}



/* ******************************************************************** *
 *
 *  List -- add an entry to the end of the list
 *
 * ******************************************************************** */

List::List(List *l,                         // list to chain to
           char *s,                         // string name
           char *d)                         // ..and data
{

EntryInit(s, d);                            // build base instance

while (l->next)                             // loop thru..
    l = l->next;                            // ..to the end of the list

l->next = this;                             // put this at the end
this->prev = l;                             // ..and backward chain

}



/* ******************************************************************** *
 *
 *  List -- put an entry at the head of the list
 *
 * ******************************************************************** */

List::List(char *s,                         // string name
           char *d,                         // ..and data
           List *l)                         // list to chain into
{

EntryInit(s, d);                            // build base instance

this->prev = l;                             // set up backward link
this->next = l->next;                       // ..and forward link
l->next = this;                             // update list anchor

if (this->next)                             // q. any more after us?
    (this->next)->prev = this;              // a. yes .. set up link

}



/* ******************************************************************** *
 *
 *  EntryInit -- initialize list entry instance
 *
 * ******************************************************************** */

void List::EntryInit(char *s,               // string name
                     char *d)               // ..and data
{


string_name = data = (char *) 0;            // init string pointers
prev = next = 0;                            // ..and list pointers

if (*s)                                     // q. string name given?
    {
    string_name = new char[strlen(s) + 1];  // a. yes .. get memory and
    strcpy(string_name, s);                 // ..copy for this instance
    }

if (*d)                                     // q. data given
    {
    data = new char[strlen(d) + 1];         // a. yes .. get memory and
    strcpy(data, d);                        // ..copy for this instance
    }
}



/* ******************************************************************** *
 *
 *  Find -- find a list entry by the string name
 *
 * ******************************************************************** */

char    *List::Find(char *s)                // string name to search on
{
List *l = this;                             // work pointer

for (;;)                                    // loop thru the list
    {
    if (l->string_name &&                   // q. string available?
            NOT stricmp(s, l->string_name)) // ..and find the entry?
        return(l->data);                    // a. yes .. quit here

    if ((l = l->next) == 0)                 // q. end of list?
        break;                              // a. yes .. exit loop
    }

return(0);                                  // else return empty-handed

}



/* ******************************************************************** *
 *
 *  ~List -- object destructor
 *
 * ******************************************************************** */

List::~List()
{

if (string_name)                            // q. string name given?
    delete string_name;                     // a. yes .. de-alloc space

if (data)                                   // q. data string available?
    delete data;                            // a. yes .. de-alloc space

if (this->next)                             // q. anything after this?
    (this->next)->prev = this->prev;        // a. yes .. de-chain prev

if (this->prev)                             // q. anything before this?
    (this->prev)->next = this->next;        // a. yes .. de-chain next

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -