home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18391 < prev    next >
Encoding:
Text File  |  1992-12-24  |  1.3 KB  |  61 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!news.umbc.edu!umbc8.umbc.edu!reagle
  3. From: reagle@umbc8.umbc.edu (Mr. Joseph Reagle; MEYERHOFF (U))
  4. Subject: Do I need to worry about what the compiler will do?
  5. Message-ID: <1992Dec24.155526.20892@umbc3.umbc.edu>
  6. Sender: newspost@umbc3.umbc.edu (News posting account)
  7. Organization: University of Maryland, Baltimore County Campus
  8. Date: Thu, 24 Dec 1992 15:55:26 GMT
  9. Lines: 50
  10.  
  11.  
  12.     This is the program I spoke of earlier concerning linked lists, and 
  13. constructors.  My question is since each node in the list is an object, is this
  14. inefficient?  Is each function of each object repeated in the object or 
  15. executable code?
  16.  
  17.  
  18. // A simple generic linked lijst.
  19. #include <iostream.h>
  20.  
  21. template <class data_t>
  22. class list {
  23.     data_t    data;
  24.     list    *next;
  25. puclic:
  26.     list (data_t d);
  27.     void add(list *node) {node->next = this; next = 0;}
  28.     list *getnext() {return next;}
  29.     data_t getdata () {return data;}
  30. };
  31.  
  32. template <class data_t>
  33. list<data_t>::list (data_t d)
  34. {
  35.     data = d;
  36.     next = 0;
  37. }
  38.  
  39. main ()
  40. {
  41.     list<char> start ('a');
  42.     list<char> *p, *last;
  43.     int i;
  44.  
  45.     // build a linked list
  46.     last = &start;
  47.     for (i=0; i <26, i++) {
  48.         p = new list<char> ('a' + i );
  49.         p->add(last);
  50.         last = p;
  51.     }
  52.  
  53.     // follow list
  54.     p = &start;
  55.     while (p) {
  56.         cout << p->getdata();
  57.         p = p->getnext();
  58.     }
  59.  
  60.     return 0;
  61.