home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj8911.zip / STEVENS.LST < prev    next >
File List  |  1989-10-04  |  6KB  |  275 lines

  1. _C PROGRAMMING COLUMN_
  2. by Al Stevens
  3.  
  4. [LISTING ONE]
  5.  
  6. // -------- linklist.h
  7.  
  8. #ifndef LINKLIST
  9. #define LINKLIST
  10.  
  11. #include <stdio.h>
  12.  
  13. class LinkedList {
  14.     typedef struct list_entry    {
  15.         struct list_entry *NextEntry;
  16.         struct list_entry *PrevEntry;
  17.         void *entrydata;
  18.     } ListEntry;
  19.     ListEntry *FirstEntry;
  20.     ListEntry *LastEntry;
  21.     ListEntry *CurrEntry;
  22. public:
  23.     // ---- constructor
  24.     LinkedList(void)
  25.         { FirstEntry = LastEntry = CurrEntry = NULL; }
  26.     // ---- destructor
  27.     ~LinkedList(void);
  28.     // ---- add an entry
  29.     void addentry(void *newentry, int size);
  30.     // ---- delete the current entry
  31.     void delete_entry(void);
  32.     // ---- get the first entry in the list
  33.     void *getfirst(void);
  34.     // ---- get the next entry in the list
  35.     void *getnext(void);
  36.     // ---- get the previous entry in the list
  37.     void *getprev(void);
  38.     // ---- get the last entry in the list
  39.     void *getlast(void);
  40.     // ---- get the current entry in the list
  41.     void *getcurr(void)
  42.         {return CurrEntry==NULL ? NULL : CurrEntry->entrydata;}
  43. };
  44.  
  45. #endif
  46.  
  47.  
  48.  
  49.  
  50. [LISTING TWO]
  51.  
  52. // -------------- linklist.cpp
  53.  
  54. #include <string.h>
  55. #include "linklist.h"
  56.  
  57. // ------- linked list destructor
  58. LinkedList::~LinkedList(void)
  59. {
  60.     ListEntry *thisentry = FirstEntry;
  61.  
  62.     while (thisentry != NULL)    {
  63.         delete thisentry->entrydata;
  64.         ListEntry *hold = thisentry;
  65.         thisentry = thisentry->NextEntry;
  66.         delete hold;
  67.     }
  68. }
  69.  
  70. // --------- add an entry to the list
  71. void LinkedList::addentry(void *newentry, int size)
  72. {
  73.     /* ------- build the new entry ------- */
  74.     ListEntry *thisentry = new ListEntry;
  75.     thisentry->entrydata = new char[size];
  76.     memcpy(thisentry->entrydata, newentry, size);
  77.  
  78.     if (CurrEntry == NULL)    {
  79.         thisentry->PrevEntry = NULL;
  80.         // ---- adding to the beginning of the list 
  81.         if (FirstEntry != NULL)    {
  82.             /* ---- already entries in this list ---- */
  83.             thisentry->NextEntry = FirstEntry;
  84.             FirstEntry->PrevEntry = thisentry;
  85.         }
  86.         else    {
  87.             // ----- adding to an empty list 
  88.             thisentry->NextEntry = NULL;
  89.             LastEntry = thisentry;
  90.         }
  91.         FirstEntry = thisentry;
  92.     }
  93.     else    {
  94.         // ------- inserting into the list 
  95.         thisentry->NextEntry = CurrEntry->NextEntry;
  96.         thisentry->PrevEntry = CurrEntry;
  97.         if (CurrEntry == LastEntry)
  98.             // ---- adding to the end of the list 
  99.             LastEntry = thisentry;
  100.         else
  101.             // ---- inserting between existing entries 
  102.             CurrEntry->NextEntry->PrevEntry = thisentry;
  103.         CurrEntry->NextEntry = thisentry;
  104.     }
  105.     CurrEntry = thisentry;
  106. }
  107.  
  108. // ---------- delete the current entry from the list
  109. void LinkedList::delete_entry(void)
  110. {
  111.     if (CurrEntry != NULL)    {
  112.         if (CurrEntry->NextEntry != NULL)
  113.             CurrEntry->NextEntry->PrevEntry = CurrEntry->PrevEntry;
  114.         else
  115.             LastEntry = CurrEntry->PrevEntry;
  116.         if (CurrEntry->PrevEntry != NULL)
  117.             CurrEntry->PrevEntry->NextEntry = CurrEntry->NextEntry;
  118.         else
  119.             FirstEntry = CurrEntry->NextEntry;
  120.         delete CurrEntry->entrydata;
  121.         ListEntry *hold = CurrEntry->NextEntry;
  122.         delete CurrEntry;
  123.         CurrEntry = hold;
  124.     }
  125. }
  126.  
  127. // ---- get the first entry in the list
  128. void *LinkedList::getfirst(void)
  129. {
  130.     CurrEntry = FirstEntry;
  131.     return CurrEntry == NULL ? NULL : CurrEntry->entrydata;
  132. }
  133.  
  134. // ---- get the next entry in the list
  135. void *LinkedList::getnext(void)
  136. {
  137.     if (CurrEntry == NULL)
  138.         CurrEntry = FirstEntry;
  139.     else
  140.         CurrEntry = CurrEntry->NextEntry;
  141.     return CurrEntry == NULL ? NULL : CurrEntry->entrydata;
  142. }
  143.  
  144. // ---- get the previous entry in the list
  145. void *LinkedList::getprev(void)
  146. {
  147.     if (CurrEntry == NULL)
  148.         CurrEntry = LastEntry;
  149.     else
  150.         CurrEntry = CurrEntry->PrevEntry;
  151.     return CurrEntry == NULL ? NULL : CurrEntry->entrydata;
  152. }
  153.  
  154. // ---- get the last entry in the list
  155. void *LinkedList::getlast(void)
  156. {
  157.     CurrEntry = LastEntry;
  158.     return CurrEntry == NULL ? NULL : CurrEntry->entrydata;
  159. }
  160.  
  161.  
  162.  
  163. [LISTING THREE]
  164.  
  165. // -------- demolist.cpp
  166.  
  167. #include <stream.hpp>
  168. #include "strings.h"
  169. #include "linklist.h"
  170.  
  171. void collectnames(LinkedList& namelist);
  172. int menu(void);
  173. void displaynames(LinkedList& namelist);
  174. void stepforward(LinkedList& namelist);
  175. void stepbackward(LinkedList& namelist);
  176. string insertname(LinkedList& namelist);
  177.  
  178. void main(void)
  179. {
  180.     cout << "Enter some names followed by \"end\"\n";
  181.     // ------ a linked list of names 
  182.     LinkedList namelist;
  183.     collectnames(namelist);
  184.     int key = 0;
  185.     while (key != 6)    {
  186.         switch (key = menu())    {
  187.             case 1:
  188.                 displaynames(namelist);
  189.                 break;
  190.             case 2:
  191.                 stepforward(namelist);
  192.                 break;
  193.             case 3:
  194.                 stepbackward(namelist);
  195.                 break;
  196.             case 4:
  197.                 insertname(namelist);
  198.                 break;
  199.             case 5:
  200.                 namelist.delete_entry();
  201.                 break;
  202.             case 6:
  203.                 cout << "Quitting...";
  204.                 break;
  205.             default:
  206.                 break;
  207.         }
  208.     }
  209. }
  210.  
  211. void collectnames(LinkedList& namelist)
  212. {
  213.     // ------- until the user types "end" 
  214.     while (insertname(namelist) != "end")
  215.         ;
  216. }
  217.  
  218. int menu(void)
  219. {
  220.     cout << "\n1 = display the names";
  221.     cout << "\n2 = step forward through the names";
  222.     cout << "\n3 = step backward through the names";
  223.     cout << "\n4 = insert a name";
  224.     cout << "\n5 = delete the current name";
  225.     cout << "\n6 = quit";
  226.     cout << "\nEnter selection: ";
  227.     int key;
  228.     cin >> key;
  229.     return key;
  230. }
  231.  
  232. // ------ read the names in a list and display them 
  233. void displaynames(LinkedList& namelist)
  234. {
  235.     cout << "------ NAME LIST ------\n";
  236.     char *name = namelist.getfirst();
  237.     while (name != NULL)    {
  238.         cout << name << "\n";
  239.         name = namelist.getnext();
  240.     }
  241.     cout << "-----------------------\n";
  242. }
  243.  
  244. // ------- step forward through the list of names
  245. void stepforward(LinkedList& namelist)
  246. {
  247.     char *name = namelist.getnext();
  248.     cout << (name ? name : "-- End of list --") << "\n";
  249. }
  250.  
  251. // ------- step backwardward through the list of names
  252. void stepbackward(LinkedList& namelist)
  253. {
  254.     char *name = namelist.getprev();
  255.     cout << (name ? name : "-- Beginning of list --") << "\n";
  256. }
  257.  
  258. // ------- insert a name into the list
  259. string insertname(LinkedList& namelist)
  260. {
  261.     cout << "Enter a name: ";
  262.     // ----- a string to hold one name 
  263.     string name(80);
  264.     // ------- read a name 
  265.     cin >> name.stradr();
  266.     // ------- add it to the list
  267.     if (name != "end")
  268.         namelist.addentry(name.stradr(), name.length());
  269.     return name;
  270. }
  271.  
  272.  
  273.  
  274.  
  275.