单链表(单链表的建立)

实现单链表的建立
链表节点的定义:
typedef struct node
{
int data;//节点内容
node *next;//下一个节点
}
创建单链表
node *Create()
{
int i=0;//链表中数据个数
node *head,*p,*q;
int x=0;
head=(node*)malloc(sizeof(node));//创建头结点
while(1)
{
printf("input the data:");
scanf("%d",&x);
if(x==0)
break;//Data为0时创建结束
p=(node*)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;//连接到head的后面
}
else
{
q->next=p;//连接到链表尾端
}
q=p;
q->next=NULL;//链表的最后一个指针为NULL
return head;
}
}
posted @ 2014-05-08 20:30  ZhangAihua  阅读(180)  评论(0编辑  收藏  举报