#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++)
最新推荐文章于 2024-06-25 07:11:52 发布