Description
建立正序链表并输出结果
Input
输入一组数字以0结束
Output
输出链表内容
Sample Input
1 2 3 0
Sample Output
1 2 3
上代码:
#include<iostream>
using namespace std;
struct node{
int data;
node *next=NULL;
}
void createnode(node*& head){
node *p=NULL;
node *s=NULL;
s= new node;
cin>>s->data;
while(s->data!=0){
if(head==NULL){
head=s;
}
else
p->next=s;
p=s;
s=new node;
cin>>s->data;
}
p->next=NULL;
delete s;
return;
}
void showlist(node* head){
while(head){
cout<<head->data<<" ";
head=head->next;
}
cout<<endl;
}
int main(){
node *head=NULL;
createnode(head);
showlist(head);
}