|
Question Details:
Please I need help on bubble sorting the list on "group cost or group name" either one is fine. Please no "break" statement. The function I need help is the last function it works for sorting only "group num" and I don't understand why
004 132 10.00 Oozeball
132 24 2.00 Bed Races
12 32 10.00 Joint Council of Engineering Organizations
130 43 15.00 Freshman Leaders on Campus
133 41 5.00 Society of Hispanic Professional Engineers
135 18 4.00 National Society of Black Engineers
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ZERO 0
struct activities
{
int groupnum;
char *groupname;
float meettime;
char mthalfday;
char meetday;
int meetweek;
char nextdate[6];
char buildingloc[4];
char meetroom[5];
char purpose;
char memtype;
int groupsize;
float groupcost;
//enum national enumtype;
//union local unionlevel;
float meetlength;
struct activities *linkorgan;
};
void printallinput(struct activities *newnode);
void bubblesortgroupname(struct activities *newnode);
int main(void)
{
FILE *groupfile;
struct activities *startgroup;
struct activities *gpnewnode;
struct activities *tempnode;
int iindex = 0, jindex, kindex, count;
int intro, index, len;
int choose, xindex, xcount;
char *my_groupfile, *stringss;
int nbytes = 60, gbytes = 60;
int bytes_read, reads;
int i=1;
gpnewnode = NULL;
groupfile = fopen("xfil.dat", "r");
if(groupfile == NULL)
{
printf(" Error opening File!\n ");
return 0;
}
startgroup = (struct activities *)malloc(sizeof(struct activities));
if(startgroup == NULL)
{
printf("\nMemory allocation was unsuccesfull");
return 0;
}
while(fscanf(groupfile,"%d", &(startgroup->groupnum)) != EOF)
{
fscanf(groupfile, "%d", &(startgroup->groupsize));
fscanf(groupfile,"%f", &(startgroup->groupcost));
my_groupfile = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_groupfile, &nbytes, groupfile);
startgroup->groupname = (char *)malloc(bytes_read);
strncpy(startgroup->groupname, my_groupfile, (bytes_read-1));
startgroup->linkorgan = NULL;
if(gpnewnode == NULL)
gpnewnode = startgroup;
else
{
tempnode = gpnewnode;
while(tempnode->linkorgan != NULL)
tempnode = tempnode->linkorgan;
tempnode->linkorgan = startgroup;
}
startgroup = malloc(sizeof(struct activities));
}
printallinput(gpnewnode);
bubblesortgroupname(gpnewnode);
printf("AFTER SORTED\n\n\n");
printallinput(gpnewnode);
fclose(groupfile);
return(0);
}
void printallinput(struct activities *newnode)
{
struct activities *pnewnode, *pp;
int jindex, kindex;
pnewnode = newnode;
while(pnewnode != NULL)
{
printf("%d %d %.2f %s\n", pnewnode->groupnum, pnewnode->groupsize, pnewnode->groupcost, pnewnode->groupname);
pnewnode = pnewnode->linkorgan;
}
printf("\n");
}
void bubblesortgroupname(struct activities *newnode)
{
struct activities *ascend, *qsort;
struct activities *rsort, *ssort, *temp;
int a;
ssort = NULL;
while(ssort != newnode->linkorgan)
{
rsort = ascend = newnode;
qsort = ascend->linkorgan;
while(ascend != ssort)
{
if(ascend->groupcost > qsort->groupcost)
{
if(ascend == newnode)
{
temp = qsort->linkorgan;
qsort->linkorgan = ascend;
ascend->linkorgan = temp;
newnode = qsort;
rsort = qsort;
}
else
{
temp = qsort->linkorgan;
qsort->linkorgan = ascend;
ascend->linkorgan = temp;
rsort->linkorgan = qsort;
rsort = qsort;
}
}
else
{
rsort = ascend;
ascend = ascend->linkorgan;
}
qsort = ascend->linkorgan;
if(qsort == ssort)
ssort = ascend;
}
}
}
|