我想使用链表快速排序

编写一个函数,通过引用获取两个学生记录结构,并交换除下一个指针之外的所有内容。 使用您的函数实现冒泡排序算法以对链表进行排序(不要使用数组)。

#include  #include  #include  /*defined a structure for date of birth*/ struct birth{ int date; int month; int year; }; struct studentrecord{ char name[64]; struct birth dob; int height; float weight; struct studentrecord *next; struct studentrecord *prev; }; struct studentrecord* printlist(struct studentrecord* p){ if(p==NULL) return 0; printf("%s\t%2d%2d%4d\t%d\t%.2f\n",p->name,(p->dob).date,(p->dob).month,(p- >dob).year,p->height,p->weight); printlist(p->next); } /*swapped all the contents of 2 student records*/ void swap(struct studentrecord *c,struct studentrecord *d){ char temp[32]; strcpy(temp,c->name); strcpy(c->name,d->name); strcpy(d->name,temp); int f; f=(c->dob).date; (c->dob).date=(d->dob).date; (d->dob).date=f; int m; m=(c->dob).month; (c->dob).month=(d->dob).month; (d->dob).month=m; int y; y=(c->dob).year; (c->dob).year=(d->dob).year; (d->dob).year=y; int h; h=c->height; c->height=d->height; d->height=h; float w; w=c->weight; c->weight=d->weight; d->weight=w; } /*comparing the age of 2 students*/ int compareage(struct studentrecord *a, struct studentrecord *b){ if (a->dob.yeardob.year) return 0; else if(a->dob.year>b->dob.year) return 1; else if (a->dob.monthdob.month) return 0; else if(a->dob.month>b->dob.month) return 1; else if (a->dob.datedob.date) return 0; else return 1; } /*Here is where i think the problem starts*/ struct studentrecord *partition(struct studentrecord *l, struct studentrecord *h) { // set pivot as h element int x = h->dob.year*10000+h->dob.month*100+h->dob.date; // similar to i = l-1 for array implementation struct studentrecord *i = l->prev; struct studentrecord *j ; // Similar to "for (int j = l; j next) { if ((j->dob.year*10000+j->dob.month*100+j->dob.date)next; swap(i, j); } } if(i == NULL) i=l; else i=i->next; // Similar to i++ swap(i, h); return i; } void quicksort(struct studentrecord *start,struct studentrecord *last){ if(last != NULL && start != last && start != last->next){ struct studentrecord *pi; pi = partition(start,last); quicksort(start,pi->prev); quicksort(pi->next,last); } } struct studentrecord *read(int n){ struct studentrecord *s; if(n==0){ return NULL; } s=(struct studentrecord*)malloc(sizeof(struct studentrecord)); scanf("%s%d%d%d%d%f",s->name,&(s->dob.date),&(s->dob.month),&(s- >dob.year),&s->height,&s->weight); s->next=read(n-1); return s; } int main(){ struct studentrecord *k, *temp1,*temp2; int n, i; printf("please enter the number of student records\n"); scanf("%d",&n); k=read(n); temp2=k; k->prev=NULL; while(k->next!=NULL){ temp1=k; k=k->next; k->prev=temp1; } temp1=k; quicksort(temp2,temp1); printlist(temp2); } 

我认为我的分区和快速排序function存在一些问题(因此主要function)。 我认为我没有正确地做到这一点,并且对如何对列表进行排序感到有些困惑。 任何forms的帮助将不胜感激。 谢谢!