#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n,int a[])//头插法
{
struct node *head,*p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=a[i];
p->next=head->next;
head->next=p;
}
return head;
};
/*struct node *creat(int n,int a[])//尾插法
{
struct node *head,*p,*tail;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(int i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=a[i];
tail->next=p;
tail=p;
}
tail->next=NULL;
return head;
};*/
void show(struct node *head)
{
struct node *tail;
tail=head->next;
while(tail)
{
printf("%d\n",tail->data);
tail=tail->next;
}
}
int main()
{
int a[5]={1,2,3,4,5};
struct node *head=creat(5,a);
show(head);
return 0;
}