#include <iostream> using namespace std; class List { public: List(int d) { data = d; #if 0 /*可以优化*/ if(NULL == head) { this->next = NULL; head = this; } else { this->next = head; head = this; } #endif this->next = head; head = this; } static void traverseList() { List *ph = head; while(ph != NULL) { cout<<ph->data<<endl; ph = ph->next; } } private: int data; List * next; static List * head; }; List * List::head = NULL; int main(int argc, char *argv[]) { List a(1),b(2),c(3); //栈空间 for(int i=5; i<10; i++) new List(i); //堆空间 链表堆和栈空间混用 List::traverseList(); return 0; }