其实你的程序中q一直没有变的,始终指一个定值,我觉得升序排列函数可以这样修改:
void OrderList(LinkList *L,int n)
{
LinkList *p,*q;
int i,j,k;
q=L;
if(q!=NULL)
{
for(i=0;i<n-1;i++)
{
p=q->next;
for(j=i+1;j<n;j++)
{
if(q->data>p->data)
{
k=p->data;
p->data=q->data;
q->data=k;
}
p=p->next;
}
q=q->next;
}
}
}
附加一个测试主函数:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct Lnode
{
int data;
struct Lnode *next;
}LinkList;
LinkList *head;
void OrderList(LinkList *L,int n);
main()
{
int data,num=0;
char ch;
LinkList *p,*ptr;
printf("Creat a list!\n");
do
{
p=(LinkList*)malloc(sizeof(LinkList));
if(p==NULL)
{
printf("getting memery is failed\n");
exit(0);
}
printf("input the %d-th data of the node: ",++num);
scanf("%d",&data);
p->data=data;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
ptr->next=p;
}
ptr=p;
printf("There are %d data in the link\n",num);
printf("Whether to input agian:");
ch=getche();
printf("\n");
ch=tolower(ch);
}while(ch=='y');
ptr=head;
printf("\nThe source data order: ");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
OrderList(head,num);
ptr=head;
printf("\nThe sorted data order: ");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
我在VC6.0平台上运行的,这样修改没有问题,能得到正确结果!