hello friend,
I encountered a problem: memory error occurred sorting structure.
My code is as follows:
void bubble_sort(struct Student *h) // On the "Total" to sort.
{
struct Student *temp=h,*p;
int swapped;
int T_stu_id;
char T_name[15];
float T_score[CURRICULUM_MAX];
float T_Average;
float T_Total;
/*
if(h!=NULL){
do
{
swapped=0;
temp=h;
for (;temp->next!=NULL;)
{
p=temp->next;
if(temp->Total>p->Total){
T_stu_id=temp->stu_id;
strcpy(T_name,temp->name);
T_score[CURRICULUM_MAX]=temp->score[CURRICULUM_MAX];
T_Average=temp->Average;
T_Total=temp->Total;
temp->stu_id=p->stu_id;
strcpy(temp->name,p->name);
temp->score[CURRICULUM_MAX]=p->score[CURRICULUM_MAX];
temp->Average=p->Average;
temp->Total=p->Total;
p->stu_id=T_stu_id;
strcpy(p->name,T_name);
p->score[CURRICULUM_MAX]=T_score[CURRICULUM_MAX];
p->Average=T_Average;
p->Total=T_Total;
swapped=1;
}
temp=temp->next;
}
} while (swapped);
}
print(h);
*/
}
I do not want to use the above algorithm, because it will change the original position.
Note: Do not exchange element, they only change the pointer field.
Annex has my complete code.
Thank very much. :)
Loading

VulpesPosted Dec 16, 2013, 5:45 PM
Rather than spend 'hours' trying to figure these out, it's generally best to find an example algorithm to follow - preferably from some academic institution.
Here's one I found which gives a choice of iterative or recursive algorithms:
http://www.sal.ksu.edu/faculty/tim/CMST302/study_guide/topic7/bubble.html
Ken HPosted Dec 18, 2013, 10:24 PM
for (;temp->next!=NULL;){
p=temp->next;
if(temp->Total>p->Total){
T_stu_id=temp->stu_id;
strcpy(T_name,temp->name);
//printf("%s\n", temp->name);
T_score[CURRICULUM_MAX]=temp->score[CURRICULUM_MAX];
T_Average=temp->Average;
T_Total=temp->Total;
temp->stu_id=p->stu_id;
strcpy(temp->name,p->name);
temp->score[CURRICULUM_MAX]=p>score[CURRICULUM_MAX];
temp->Average=p->Average;
temp->Total=p->Total;
p->stu_id=T_stu_id;
strcpy(p->name,T_name);// This,'T_name' lost value.
p->score[CURRICULUM_MAX]=T_score[CURRICULUM_MAX];
p->Average=T_Average;
p->Total=T_Total;
swapped=1;
}
temp=temp->next;
}
VulpesPosted Dec 18, 2013, 6:32 AM
printf("%s\n", temp->name);
just before the offending line to see if you can isolate what temp->name is when the error occurs.
Ken HPosted Dec 17, 2013, 9:59 PM
Also, I'm sure 'temp->name' not empty.
VulpesPosted Dec 17, 2013, 6:54 PM
The only reason I can think of why an error would occur is if temp->name is null.
So what's the value of temp->name when the error occurs and the full error message?
Ken HPosted Dec 16, 2013, 9:18 PM
Thank for your helped.
Also,I have a question,The following bold red font:
/*
if(h!=NULL){
do
{
swapped=0;
temp=h;
for (;temp->next!=NULL;)
{
p=temp->next;
if(temp->Total>p->Total){
T_stu_id=temp->stu_id;
strcpy(T_name,temp->name); // The temp->name can't copy to T_name.
T_score[CURRICULUM_MAX]=temp->score[CURRICULUM_MAX];
T_Average=temp->Average;
T_Total=temp->Total;
temp->stu_id=p->stu_id;
strcpy(temp->name,p->name);
temp->score[CURRICULUM_MAX]=p->score[CURRICULUM_MAX];
temp->Average=p->Average;
temp->Total=p->Total;
p->stu_id=T_stu_id;
strcpy(p->name,T_name);
p->score[CURRICULUM_MAX]=T_score[CURRICULUM_MAX];
p->Average=T_Average;
p->Total=T_Total;
swapped=1;
}
temp=temp->next;
}
} while (swapped);
}
print(h);
*/
Ken HPosted Dec 15, 2013, 9:45 PM
It is based on the "Total" to sort.
In addition, even though I used the method of copy function, I can't succeed, because the copy function can not be executed properly.
strcpy(T_name,temp->name); // The temp->name can't copy to T_name.
My idea is this(Attachment with flow charts) :
VulpesPosted Dec 15, 2013, 4:06 PM
What are you trying to sort here?
1. The score array of a given Student struct;
2. The linked list of Student structs by name (say); or
3. Both;
and am I right in thinking that you only want this to be a temporary sort i.e. you don't want to change the original order?