1 #include <iostream> 2 3 using namespace std; 4 5 struct Node{ 6 int data; 7 Node *next; 8 }; 9 10 Node* InitNodeList(){ 11 Node *node=0;//定义一个空指针 12 return node; 13 } 14 15 Node* addNode(Node *nodeList,Node *node) 16 { 17 if(nodeList == 0) 18 { 19 nodeList=node; 20 }else{ 21 nodeList->next = node; 22 } 23 node->next=0; 24 25 return nodeList; 26 } 27 28 void printNodeList(Node *nodeList) 29 { 30 Node *p,*q; 31 q=p=nodeList; 32 while(q->next!=0) 33 { 34 cout << p->data << " "; 35 q=p; 36 p=p->next; 37 } 38 cout << endl; 39 } 40 41 int main(void){ 42 Node *node=InitNodeList(); 43 44 Node *a=new Node; 45 a->data = 1; 46 Node *b = new Node; 47 b->data = 2; 48 49 node = addNode(node,a); 50 node = addNode(node,b); 51 52 printNodeList(node); 53 return 0; 54 }
原文:http://www.cnblogs.com/xxdfly/p/4372603.html