#include<iostream>
using namespace std;
struct Node {
//write your code here
int data;
struct Node *next;
};
Node *createLink() //尾插法,rear为尾指针
{
//write your code here
Node *head = new Node();
if(!head){
exit(-2);
}
int num = 0;;
cin>>num;
Node * rear = head;
while(num!= -1){
Node *s = new Node();
s->data = num;
rear->next = s;
rear = s;
cin>>num;
}
rear->next = NULL;
return head;
}
void Print(Node *head)
{
head=head->next;
while (head!=NULL) {
cout << head->data;
head=head->next;
}
}
int main()
{
Node *h1;
h1=createLink();
Print(h1);
return 0;
}
单链表的尾插法(c++)
最新推荐文章于 2025-01-27 00:15:00 发布
本文介绍了一种使用尾插法创建链表的方法,并提供了一个简单的程序来实现这一过程。此外,还展示了如何遍历并打印链表中的元素。通过这个例子,读者可以更好地理解链表的基本操作。
468

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



