#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; //插入函数 struct node* insert(struct node*head,int n){ struct node *p1,*p2;//新结点,旧结点迭代 p1=(struct node*)malloc(sizeof(struct node)); head=p1; int len=0;//定义链表长度,每输入一个数+1 scanf("%d",&p1->data); while(len<n){ p2=p1;//让p2前移 p1=(struct node*)malloc(sizeof(struct node));//p1定义一个新的空间 p2->next=p1;//让p2与p1连接 scanf("%d",&p1->data); len++; } p2->next=NULL; return head; } void print(struct node* head){ struct node *temp; temp=head; while(temp!=NULL){ printf("%d ",temp->data); temp=temp->next; } } int main(){ struct node *head=NULL; int n; scanf("%d",&n); head=insert(head,n); print(head); return 0; }