Thread: Need help with linked list sorting function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    Need help with linked list sorting function

    I'm at the limit of my sanity. The program I'm writing accepts the name, ssn, and other bits of information from a regular txt file, inserts it all into structs, and has (or would later have had) the ability to accept more info from the user.

    That's the background, and all of it works. The only issue I'm having is with why in holy hell my sorting function won't work.

    I've looked at other ways to sort link lists in C, but they all use a type of linked list different from the kind my instructor taught us. Mine is a kind that adds entries into the beginning of the list instead of the end. If you want to know exactly how, the addToList function has the details.

    My sorting uses three what-I-call "platforms" to move around the nodes connecting each struct to one another. I realize this way is immensely confusing, yet I am (or was) rather confident that the concept should have worked.

    If it's any help, through some testing using a printf, I found that my function seems to go into an infinite loop in the while control structure (not the do while), but I can't see why. Wouldn't my "platform" reach the NULL at SOME point?

    Here's the entire code, the function in question is at the bottom in bold, although anything directly related to it is bold as well:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    struct xrac {
        char name[16];
        char ssn[12];
        char gend;
        float assign;
        float quiz;
        float parti;
        float midT;
        float final;
        char grade;
        struct xrac * next;
    }temp;
    
    struct xrac * addToList (struct xrac * first, struct xrac add);
    void calcNumGrades (struct xrac * first);
    void printAllList (struct xrac * first);
    void printLList (struct xrac * first);
    void printAList (struct xrac * first);
    void printFList (struct xrac * first);
    struct xrac * sortList (struct xrac * first);
    
    void main ()
    {
        char cmd; int numGrade;
        FILE * infile = fopen("scores.txt", "r");
        struct xrac * first = NULL; struct xrac * plat1; struct xrac * plat2;
    
        while ( fscanf(infile, "%s %s %c %f %f %f %f %f", temp.name,
        temp.ssn, &temp.gend, &temp.assign, &temp.quiz, &temp.parti,
        &temp.midT, &temp.final ) != EOF){
            temp.name[15] = '\0';
            temp.ssn[11] = '\0';
            temp.grade = 'N';
    
            /*Link-List Test Prt1/two
            printf("Temp: %s %s %c %4.2f %4.2f %4.2f %4.2f %4.2f\n", temp.name,
            temp.ssn, temp.gend, temp.assign, temp.quiz, temp.parti, temp.midT, temp.final);*/
    
            first = addToList (first, temp);
            calcNumGrades (first);
    
            /*LinkList Test Prt2/two
            printf("Link-List: %s %s %c %4.2f %4.2f %4.2f %4.2f %4.2f\n", (*first).name,
            (*first).ssn, (*first).gend, (*first).assign, (*first).quiz,
            (*first).parti, (*first).midT, (*first).final);*/
        }
    
        printf("Menu: \n");
        printf("1. Print list of students.\n2. Print list of students with letter grade.\n");
        printf("3. Print list of those who made an A grade.\n4. Print list of those who made an F grade.\n");
        printf("5. Print list of students sorted by name.\n6. Add student to the class list.\n");
        printf("7. Delete student from class list.\n8. Show Menu again.\nX. Quit.\n");
    
        while ((cmd != 'X') && (cmd != 'x')){
            fflush(stdin);
            printf("\nEnter number: "); scanf("%c", &cmd);
            if(cmd == '1')
            {
                printf("\nAll students: \n");
                printAllList (first);
                printf("\n");
            }
            else if(cmd == '2')
            {
                printf("\nStudents with letter grade: \n");
                printLList (first);
                printf("\n");
            }
            else if(cmd == '3')
            {
                printf("\nStudents with an A grade: \n");
                printAList (first);
                printf("\n");
            }
            else if(cmd == '4')
            {
                printf("\nStudents with an F grade: \n");
                printFList (first);
                printf("\n");
            }
            else if(cmd == '5')
            {
               printf("\nStudents in alphabetical order: \n");
                first = sortList (first);
                printAllList (first);
                printf("\n");
            }
            else if(cmd == '6')
            {
                printf("\nEnter student's name, SSN, and grades. ");
                printf("\nIf student has no grades, enter 0's in their place: ");
            }
            else if(cmd == '7')
            {
                printf("\nEnter student's name: ");
            }
            else if(cmd == '8')
            {
                printf("Menu: \n");
                printf("1. Print list of students.\n2. Print list of students with letter grade.\n");
                printf("3. Print list of those who made an A grade.\n4. Print list of those who made an F grade.\n");
                printf("5. Print list of students sorted by name.\n6. Add student to the class list.\n");
                printf("7. Delete student from class list.\n8. Show Menu.\nX. Quit.\n");
            }
        }
        fclose(infile);
    
        /*printf("\n\n");
        system("PAUSE");*/
    }
    
    struct xrac * addToList (struct xrac * first, struct xrac add)
    {
        add.next = first;
        first = (struct xrac *) malloc (sizeof(struct xrac));
        (*first) = add;
    
        return first;
    }
    
    void calcNumGrades (struct xrac * first)
    {
        int numGrade = ((*first).assign * 0.40) + ((*first).quiz * 0.15) + ((*first).midT * 0.15)
                + ((*first).parti * 0.10) + ((*first).final * 0.20);
    
        if (numGrade >= 90)
            (*first).grade = 'A';
        else if (numGrade >= 80)
            (*first).grade = 'B';
        else if (numGrade >= 70)
            (*first).grade = 'C';
        else if (numGrade >= 60)
            (*first).grade = 'D';
        else if (numGrade < 60)
            (*first).grade = 'F';
        else if (numGrade == 0)
            (*first).grade = 'N';
    }
    
    void printAllList (struct xrac * first)
    {
        struct xrac * plat1;
    
        plat1 = first;
        while (plat1 != NULL){
            printf("%s\n", (*plat1).name);
            plat1 = (*plat1).next;
        }
    }
    
    void printLList (struct xrac * first)
    {
        struct xrac * plat1; int i = 0;
    
        plat1 = first;
        while (plat1 != NULL){
            if ((*plat1).grade != 'N')
            {
                printf("%s\n", (*plat1).name);
                i++;
            }
            plat1 = (*plat1).next;
        }
        if (i == 0)
            printf("No studentd have a letter grade.\n");
    }
    
    void printAList (struct xrac * first)
    {
        struct xrac * plat1; int i = 0;
    
        plat1 = first;
        while (plat1 != NULL){
            if ((*plat1).grade == 'A')
            {
                printf("%s\n", (*plat1).name);
                i++;
            }
            plat1 = (*plat1).next;
        }
        if (i == 0)
            printf("No students have an A grade.\n");
    }
    
    void printFList (struct xrac * first)
    {
        struct xrac * plat1; int i = 0;
    
        plat1 = first;
        while (plat1 != NULL){
            if ((*plat1).grade == 'F')
            {
                printf("%s\n", (*plat1).name);
                i++;
            }
            plat1 = (*plat1).next;
        }
        if (i == 0)
            printf("No students have an F grade.\n");
    }
    
    struct xrac * sortList (struct xrac * first)
    {
        struct xrac * plat1; struct xrac * plat2; struct xrac * plat3; struct xrac * safeFirst; int i, n = 0;
    
        first;
    
        do {
            i = 0; n = 0;
            plat1 = first; plat2 = (*plat1).next; plat3 = first;
            while (plat2 != NULL) {
                if ( strcmp((*plat1).name, (*plat2).name) == 1 )
                {
                    (*plat1).next = (*plat2).next;
                    (*plat2).next = plat1;
                    if (n == 0) /*Ensures code can only happen at the beginning of a new round of sorting.*/
                    {
                        first = plat2;
                        (*plat3).next = plat1;
                    }
                    else
                        (*plat3).next = plat2;
                    i++; /*Will increase if there has been a sorting.*/
                }
                else
                {
                    if (n != 0) /*Unnecessary if, but appropriate.*/
                        plat3 = plat1;
                    plat1 = plat2;
                }
                plat2 = (*plat1).next;
                n++;
            }
        } while (i != 0); /*Will continue until no sorting has occurred.*/
    
        return first;
    }
    Here is the information in the txt file:

    Oswald 333-3333-33 M 75 75 75 75 75
    Oscar 111-1111-11 M 100 100 100 100 100
    Lucy 222-2222-22 F 50 50 50 50 50
    Dan 555-5555-55 M 0 0 0 0 0
    Kenny 666-6666-66 M 0 0 0 0 0
    Alice 444-4444-44 F 85 85 85 85 85
    Satan 777-7777-77 M 0 0 0 0 0

    I'm sorry if I'm asking too much, and I really can't blame anyone for not wanting to try to figure out how the hell my sorting function would have actually worked. If anything, can anyone introduce me to a sorting function that would work with this type of linked list?
    Last edited by Jaggid1x; 05-31-2009 at 10:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM

Tags for this Thread