#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}List;
List *CreateNode(List *ptr,int data,int flag)
{
List *temp;
while(flag==1)
{
List *first=(List *)malloc(sizeof(List));
first->data=data;
first->next=NULL;
temp=first;
flag=0;
return first;
}
List *p=(List *)malloc(sizeof(List));
p->data=data;
temp->next=p;
p->next=NULL;
temp=p;
return temp;
}
void printflist(List *headnode)
{
List *p=headnode;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
int main()
{
int n,flag;
List *p=NULL;
printf("输入第一个数:");
scanf("%d",&n);
List *L1=CreateNode(p,n,1);//在此相当于建立了一个首元节点
List *headnode=L1;//设定一个变量定位首元节点 方便后面打印
L1=CreateNode(L1,1,0);
L1=CreateNode(L1,3,0);
printflist(headnode);
return 0;
}
*本方法关键以创造的第一个节点作为链表头*