/** * @brief 实现带头结点的单链表的建立 * @return 返回单链表的头结点 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-03-08 */ #include <assert.h> #include <iostream> using namespace std; struct Node { int Data; struct Node* next; }; Node* CreateLink() { Node* head = new Node; assert(head); head->next = NULL; Node* p = head; bool bInput = true; int data = 0; while (bInput) { cin>>data; if (data != 0) { Node* node = new Node; assert(node); node->Data = data; p->next = node; p = node; } else { bInput = false; } } p->next = NULL; return head; } void show(Node* head) { if (head->next == NULL) { return; } Node* node = head->next; while (node != NULL) { cout<<node->Data<<endl; node = node->next; } } void main(void) { Node* head = NULL; head = CreateLink(); show(head); }
posted on 2009-03-08 11:00 张云临 阅读(122) 评论(0) 编辑 收藏 举报