home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX07024.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  1.2 KB  |  59 lines

  1. // ex07024.cpp
  2. // static members and the Linked List
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. class ListEntry {
  7.     static ListEntry *lastentry; // static list head pointer
  8.     char *listvalue;
  9.     ListEntry *nextentry;
  10. public:
  11.     ListEntry();
  12.     ListEntry(char *);
  13.     ~ListEntry() { if (listvalue != NULL) delete listvalue;}
  14.     ListEntry *NextEntry() { return nextentry; };
  15.     void display() { cout << '\n' << listvalue; }
  16. };
  17.  
  18. ListEntry *ListEntry::lastentry;
  19.  
  20. ListEntry::ListEntry()
  21. {
  22.     listvalue = NULL;
  23.     nextentry = NULL;
  24.     lastentry = this;
  25. }
  26.  
  27. ListEntry::ListEntry(char *s)
  28. {
  29.     lastentry->nextentry = this;
  30.     lastentry = this;
  31.     listvalue = new char[strlen(s)+1];
  32.     strcpy(listvalue, s);
  33.     nextentry = NULL;
  34. }
  35.  
  36. main()
  37. {
  38.     ListEntry listhead;    // ---- this is the list head
  39.     // ---------- read in some names
  40.     while (1)    {
  41.         cout << "\nEnter a name: ";
  42.         char name[25];
  43.         cin >> name;
  44.         if (strncmp(name, "end", 3) == 0)
  45.             break;
  46.         // -------- make a list entry of the name
  47.         new ListEntry(name);
  48.     }
  49.     ListEntry *next = listhead.NextEntry();
  50.     // ------- display the names
  51.     while (next != NULL)    {
  52.         next->display();
  53.         ListEntry *hold = next;
  54.         next = next->NextEntry();
  55.         // -------- delete the ListEntry
  56.         delete hold;
  57.     }
  58. }
  59.