Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

x.Hm\'m confuse about the bubble sorting function, I don\'t know why if I change

ID: 3614813 • Letter: X

Question

x.Hm'm confuse about the bubble sorting function, I don't know why if I change the "gpnewnode pointer" as a global pointer it works fine but if declare inside the main then it does not print the hole data. Right now it is global and it works fine but I don't want it like that, it needs to be inside main. Can somebody fix it for me? Thanks (lifesaver)


004 13 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;
        int month;
        int day;
        char meetday;
        int meetweek;
        char memtype;
        int groupsize;
        float groupcost;
        int meetlength;
        struct activities *linkorgan;
} *gpnewnode;

void printallinput();
void bubblesortgroupname();
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! ");
                        return 0;
                }

                startgroup = (struct activities *)malloc(sizeof(struct activities));
                if(startgroup == NULL)
                {
                        printf(" Memory 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));
                }

&

Explanation / Answer

voidbubblesortgroupname(struct activities *newnode)
{

        struct activities*first,*ptr,temp,*l,*r;
              int swap;
              first = newnode;
              
              while(first != NULL )
              {
              ptr=newnode;
              while(ptr->linkorgan != NULL)
              {
               if(ptr -> groupcost > ptr->linkorgan->groupcost)
                 {
                 
                 /* save next pointers */
                  l=ptr->linkorgan;
                  r=ptr->linkorgan->linkorgan;
                  
                  /* swap d data */
                  temp = *ptr;
                  *ptr = *(ptr->linkorgan);
                  *(l) = temp;
                  /* store next pointer */
                  ptr->linkorgan = l;
                  ptr->linkorgan->linkorgan = r;
                  
                 }
                 ptr= ptr->linkorgan;
              }
              first=first->linkorgan;
              }
     
}