这一段单链表有问题,不知道哪里错了,求教~~
#include<stdio.h>#include<malloc.h>
typedef struct node
{
int data;
int site;
struct node *link;
}NODE;
NODE *creat()
{
NODE *p,*q,*head,*tail;
int num,i=0;
head=tail=NULL;
printf("int put num end with -1:\n");
scanf("%d",&num);
while(num!=-1)
{
i++;
p=(NODE *)malloc(sizeof(NODE));
if(p==NULL)
{
printf("fail");
return NULL;
}
p->data=num;
p->site=i;
p->link=0;
if(head==NULL)
{
head=p;
}
else
{
tail->link=p;
}
tail=p;
scanf("%d",&num);
}
return head;
}
int countlist(NODE *head)
{
int i=0;
while (head)
{
i++;
head=head->link;
}
return i;
}
NODE *insertlist(NODE *head,int num,int i)
{
NODE *p,*q;
int j;
p=q=NULL;
p=head;
for(j=0;;j++)
{
if(p->site!=i)
{
p=p->link;
continue;
}
else
{
q->data=num;
q->site=p->site+1;
q->link=p->link;
p->link=q;
return head;
}
}
}
void printlist(NODE *head)
{
while (head)
{
printf("%d",head->data);
head=head->link;
}
return;
}
main()
{
NODE *a;
a=creat();
insertlist(a,88,2);
printlist(a);
}