4

Node

  1. #include <iostream>
  2.  
  3. class Node {
  4. public:
  5.     Node( int d, Node *p = 0 ) : data( d ), next( p ) { ++counter; }
  6.     ~Node( ) { --counter; }
  7.     static unsigned length( ) { return counter; }
  8.  
  9.     int data;
  10.     Node *next;
  11. private:
  12.     static unsigned counter;        // declaration
  13. };
  14.  
  15. unsigned Node::counter = 0;     // definition
  16.  
  17. int main()
  18. {
  19.     using std::cin;
  20.     using std::cout;
  21.     using std::endl;
  22.  
  23.     int n;
  24.  
  25.     Node *p = 0, *q = 0;
  26.  
  27.     cout << "Enter a series of integers and a ^D|^Z:\n";
  28.  
  29.     while ( cin >> n )
  30.     {
  31.         p = new Node( n, q );
  32.         q = p;
  33.     }
  34.  
  35. //  cout << "You have created " << Node::counter << " nodes:" << endl;  // if public
  36. //  cout << "You have created " << p->length() << " nodes:" << endl;    // if private
  37.     cout << "You have created " << Node::length() << " nodes:" << endl; // if static
  38.  
  39.     for ( ; p; p = p->next )
  40.         cout << p->data << " -> ";
  41.     cout << "*" << endl;
  42.  
  43.     return 0;
  44. }
$ make
g++ -DDEBUG -g -c -o node.o node.cpp
g++ node.o -o node
$ node
Enter a series of integers and a ^D|^Z:
3 4 5 0 -1 -2 2 1
You have created 8 nodes:
1 -> 2 -> -2 -> -1 -> 0 -> 5 -> 4 -> 3 -> *

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].