5
Node
- #include <iostream>
- class Node {
- public:
- Node( int d, Node *p = 0 ) : data( d ), next( p ) { ++counter; }
- ~Node( ) { --counter; }
- static unsigned length( ) { return counter; }
- int data;
- Node *next;
- private:
- static unsigned counter; // declaration
- };
- unsigned Node::counter = 0; // definition
- int main()
- {
- using std::cin;
- using std::cout;
- using std::endl;
- int n;
- Node *p = 0, *q = 0;
- cout << "Enter a series of integers and a ^D|^Z:\n";
- while ( cin >> n )
- {
- p = new Node( n, q );
- q = p;
- }
- // cout << "You have created " << Node::counter << " nodes:" << endl; // if public
- // cout << "You have created " << p->length() << " nodes:" << endl; // if private
- cout << "You have created " << Node::length() << " nodes:" << endl; // if static
- for ( ; p; p = p->next )
- cout << p->data << " -> ";
- cout << "*" << endl;
- return 0;
- }
$ 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 -> *
Commentaires