#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList L;//全局变量L,可以省略函数参数传递;
LinkList insert()//头插法
{
int x;
LNode *s;
L=(LinkList ) malloc(sizeof(LNode));
L->next=NULL; //建立头节点,初始为空
printf("please input data: \n");
scanf("%d",&x);
while (x != 999)
{
s=(LinkList) malloc(sizeof(LNode));//申请一个新节点
s->next=NULL;
s->data=x;
s->next= L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}
void print()//打印链表
{
L=L->next;//该链表有头节点,首节点为头节点的下一个
while( L ){
printf("%d ",L->data);
L=L->next;
}
}
int main(){
L = insert();
print();
return 0;
}