typedef struct node
{
int data;
node *next;
} node;
//单链表的创建
node *create()
{
int i = 0; //链表中数据的个数
node *head, *p,*P
int x = 0;
head = (node *)malloc(sizeof(node)); //创建头结点
while(1)
{
printf("Please input the data:");
scanf("%d",&x);
if(x == 0) //data 为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;
}
编程实现一个单链表的建立
最新推荐文章于 2022-10-21 23:21:58 发布