单链表的创建
单链表的创建一般用到结构体
struct node
{
int data;
node* next;
}
node* create()
{
int i=0; //链表节点计数器
node* head,* p,*q;//head头节点,p为循环中的临时节点,q末节点
int x=0;
head=(node*) malloc(sizeof(node));
q=(node*)malloc(sizeof(node));//因为可能用到所以提前开辟好内存
while(1)
{
printf("请输入x的值\n");
scanf("%d",x);
if(x==0)
{
break;
}
p=(node*)malloc(sizeof(node));
p.data=x;
if(++i==1)
{
head->next=p;//个节点要绑定在头结点上
}
else
{
q->next=p; //之后节点要绑定在末节点上
}
q=p;//p临时节点重新变成末节点, 被q指向
}
q->next=NULL; //末节点next置空
return head;
}