Q BgQuestion:

      
Apprentice
Karma Points: 133
Respect (94%):
posted by  dorsty on 11/7/2009 11:43:46 AM  |  status: Live  |  Earned Karma: 133

bubble sort (linklist)

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A 11/10/2009 at 1:00:00 PM
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;
             }
      }
}
Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
Mentor
Karma Points: 419
posted by Mephisto on 11/7/2009 3:18:21 PM  |  status: Live
Asker's Rating: N/A   
Response Details:
The only line you should need to change is the following:
                        if(ascend->groupcost > qsort->groupcost)

You may have tried the following unsuccessful attempt:
                        if(ascend->groupname > qsort->groupname)

That doesn't work because it compares the addresses of the first characters of the names.
Instead, the names themselves should be compared, and that's where function strcmp comes in
                        if(strcmp(ascend->groupname, qsort->groupname) > 0)

If you have a question about my response, feel free to message me.
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »