上面是运行结果,帮忙看看哪里出现了逻辑错误
#include "stdio.h"
typedef int ElemType;
typedef struct node
{ElemType data;
struct node *next;
}slink;
slink *creslink(int n)
{ slink *head,*p,*s;
int i;
if(n<1)return NULL;
p=head=(slink *)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink *)malloc(sizeof(slink));
scanf("%d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
return head;
}
void sort(slink *L)
{slink *p,*q,*end;
ElemType r;
end=L->next;
while(end->next!=NULL)end=end->next;
p=L->next;
while(L->next->next!=end)
{p=L->next;q=p->next;
while(q!=end)
{if(p->data>q->data)
{r=p->data;p->data=q->data;q->data=r;}
p=q;q=q->next;
}
end=p;
}
}
void list(slink *head)
{slink *p;
p=head->next;
while(p!=NULL)
{ printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink *L;
L=creslink(7);
list(L);
sort(L);
list(L);
getch();
}