/*链表节点的定义*/
typedef struct node
{
int data; /*节点内容*/
struct node *next; /*下一个节点*/
}node;
/*单链表的创建*/
node *create()
{
int i = 0; /*链表中数据的个数*/
node *head , *p , *q;
int x = 0; /*存储数据*/
head = (node *)malloc(sizeof(node)); /*创建头节点*/
while(1)
{
printf("please input the data:\n");
scanf("%d" , &x);
if(x == 0) /*数据为0时表示插入数据结束*/
break;
p = (node *)malloc(sizeof(node)); /*创建一个新节点*/
p->data = x;
if(++i == 1) /*链表只有一个元素*/
{
head->next = p; /*链接到head后面*/
}
else
{
q->next = p; /*链接到链表尾端*/
}
q = p; /*q指向末节点*/
}
q->next = NULL; /*链表的最后一个指针为NULL*/
return head;
}