I want to quick sort using linked lists

為{幸葍}努か 提交于 2019-12-11 15:21:43

问题


Write a function that takes two student record structures by reference and swaps all their contents except their next pointers. Use your functions to implement bubble sort algorithm to sort the linked list (do not use arrays).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*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.year<b->dob.year)
    return 0;
else if(a->dob.year>b->dob.year)
    return 1;
else if (a->dob.month<b->dob.month)
    return 0;
else if(a->dob.month>b->dob.month)
    return 1;
else if (a->dob.date<b->dob.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 <= h- 1; j++)"
for (j= l; j != h; j = j->next)
{
    if ((j->dob.year*10000+j->dob.month*100+j->dob.date)<= x)
    {
        // Similar to i++ for array
        if(i == NULL)
            i=l;
        else
            i=i->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);
}

I think there is some problem with my partition and quicksort functions(and hence the main function). I don't think i am doing it correctly and am getting a bit confused on how to sort the list. Any sort of help would be appreciated. Thanks!

来源:https://stackoverflow.com/questions/47458338/i-want-to-quick-sort-using-linked-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!