求解释一下我的代码为什么部分错误在PTA上。谢谢亲们,实在是检查不出来哪里错误!我想知道我的代码哪里错了!
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(void);//这个是可能出错的函数
struct node *creat(void)
{
int i,m,n,ii;
struct node *h,*t,*p,*t2;
h=(struct node *)malloc(sizeof(struct node));
h->next=NULL;
t=h;
t2=h;
//对应两种情况
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&m);
p=(struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
if(m>i)//如果输入的数大于链表中的元素个数放在最后面
{
t=h;
while(t->next!=NULL)
{
t=t->next;
}
t->next=p;
p->next=NULL;
}
else//不大于的情况插在m元素后面
{
t2=h;
for(ii=0;ii<m;ii++)
{
t2=t2->next;
}
p->next=t2->next;
t2->next=NULL;
t2->next=p;
}
}
return h;
}
//这个不可能出错下面是打印链表
void print(struct node *head);
void print(struct node *head)
{
struct node *p;
int m=0;
p=head->next;
while(p)
{
m++;
if(m==1)
printf("%d",p->data);
else
{
printf(" %d",p->data);
}
p=p->next;
}
printf("\n");
}
//下面是主函数也不可能出错
int main (void)
{
struct node *h;
h=creat();
print(h);
return 0;
}