尾插法建立单链表
node* creat()
{
node* head = (node*)malloc(sizeof(node));
head->next = NULL;
node* s, *tail = head;//尾指针指向头结点
int x, cycle = 1;
while (cycle)
{
printf("请输入数据:\n");
scanf("%d", &x);
if (x != -1)
{
s = (node*)malloc(sizeof(node));
s->data = x;
tail->next = s;
tail = s;//指向新的结点
}
else
cycle = 0;
}
tail->next = NULL;
return head;
}
求表长
int Length(node* head)
{
node* p = head;
int j = 0;
while (p)
{
p = p->next;
j++;
}
return j;
}