#include
#include
#include
struct list{
int num;
int n; //快排中的序号,比如第一个,第二个
char ch[20];
list *left,*right;
};
int size=sizeof(struct list);
void qsort(struct list *x,struct list *y);
void main()
{
list *head,*p,*begin,*end;
int number,i;
char c[20];
head=NULL;
i=0;
printf("pleade input a number:\n");
scanf("%d",&number);
while (number)
{
if (number==0)
break;
scanf("%s",c);
i++;
p=(struct list *) malloc(size);
p->num=number;
p->n=i;
strcpy(p->ch,c);
p->left=head;
p->right=NULL;
head=p;
number--;
} //建立链表
begin=end=head;
while ((begin->left)!=NULL)
begin=begin->left;//头指针和尾指针
qsort(begin,end);
while ((begin->right)!=NULL)
{
printf("%d %s",begin->num,begin->ch);
begin=begin->right;
}
}
void qsort(struct list *x,struct list *y)//这是递归的快排
{
int mid;
struct list *i,*j,*temp;
temp = (struct list *) malloc(size);
mid=x->num;
i=x;j=y;
do
{
while((i->num
while((j->num>mid)&&(j!=NULL))
j=j->right;
if ((j->n)>=(i->n))
{
strcpy(temp->ch,i->ch);//感觉是这里错。。。。
strcpy(i->ch,j->ch);
strcpy(j->ch,temp->ch);
temp->n=j->n;j->n=i->n;i->n=temp->n;
}
if (i->n<=y->n)
qsort(i,y);
if (j->n>=x->n)
qsort(x,j);
}while ((j->n)>=(i->n));
free(temp);
}
struct list{
int num;
int n; //快排中的序号,比如第一个,第二个
char ch[20];
list *left,*right;
};
typedef struct list{
int num;
int n; //快排中的序号,比如第一个,第二个
char ch[20];
struct list *left,*right;
};
你自己的函数qsort 和stdlib里的库函数也是同名的,所以把qsort换个名