home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!haven.umd.edu!news.umbc.edu!umbc8.umbc.edu!reagle
- From: reagle@umbc8.umbc.edu (Mr. Joseph Reagle; MEYERHOFF (U))
- Subject: Do I need to worry about what the compiler will do?
- Message-ID: <1992Dec24.155526.20892@umbc3.umbc.edu>
- Sender: newspost@umbc3.umbc.edu (News posting account)
- Organization: University of Maryland, Baltimore County Campus
- Date: Thu, 24 Dec 1992 15:55:26 GMT
- Lines: 50
-
-
- This is the program I spoke of earlier concerning linked lists, and
- constructors. My question is since each node in the list is an object, is this
- inefficient? Is each function of each object repeated in the object or
- executable code?
-
-
- // A simple generic linked lijst.
- #include <iostream.h>
-
- template <class data_t>
- class list {
- data_t data;
- list *next;
- puclic:
- list (data_t d);
- void add(list *node) {node->next = this; next = 0;}
- list *getnext() {return next;}
- data_t getdata () {return data;}
- };
-
- template <class data_t>
- list<data_t>::list (data_t d)
- {
- data = d;
- next = 0;
- }
-
- main ()
- {
- list<char> start ('a');
- list<char> *p, *last;
- int i;
-
- // build a linked list
- last = &start;
- for (i=0; i <26, i++) {
- p = new list<char> ('a' + i );
- p->add(last);
- last = p;
- }
-
- // follow list
- p = &start;
- while (p) {
- cout << p->getdata();
- p = p->getnext();
- }
-
- return 0;
-