/** * @brief 实现带头结点的单链表的建立 * @return 返回单链表的头结点 * @author wlq_729@163.com * http://blog.youkuaiyun.com/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); }
单链表的建立
最新推荐文章于 2025-12-05 18:30:00 发布
本文介绍了一种实现带头结点单链表的方法,并提供了完整的C++代码示例。该方法通过不断追加节点来构建链表,并最终遍历显示链表中的所有元素。
365

被折叠的 条评论
为什么被折叠?



