Thread: debug linked list

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Unhappy debug linked list

    My program to read a list of students from a file
    and create a linked list.

    Each entry in the link list is to have the student's name, a pointer to the next
    student, and a pointer to linked list of scores.

    Can have up to four scores for each student.

    Can anyone help me fix this program?
    I don't know what wrong.
    Thank


    FILE

    Peter Perry
    John Smith
    Mary Lee
    =====================

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int readStudentFile();
    int getScores();
    int readLine(FILE *inFile, char *currentLine);
    int printScores();
    int freeStudents();

    typedef struct qNode
    {
    int score;
    struct qNode *link;
    };

    typedef struct sNode
    {
    char name[80];
    struct sNode *next;
    struct qNode *link;
    };

    struct sNode *student;

    int main()
    {
    readStudentFile();
    getScores();
    printScores();
    freeStudents();

    return 0;
    }

    /******************************** READ FILE***************************/

    int readStudentFile()
    {
    FILE *studentFile;

    char studentName[80] ;
    struct sNode *prev;
    struct sNode *studentPtr;

    if ((studentFile=(fopen("record.txt", "r")))==NULL)
    {
    printf("Exiting the program\n");
    exit(1);
    }


    prev = NULL;
    while (!feof(studentFile))
    {
    readLine(studentFile, studentName);


    if(!(studentPtr = (sNode*) malloc(sizeof sNode)))
    {
    printf("Out of memory");
    return 0;
    }

    (prev != NULL) ? (prev->next = studentPtr) : (student = studentPtr);

    strcpy(studentPtr->name,studentName);
    printf("%s\n", studentPtr->name );
    studentPtr->link = NULL;
    prev = studentPtr;
    studentPtr->next = NULL;
    };
    fclose(studentFile);
    return 0;
    }

    /**********************************GET SCORES**************************/

    int getScores()
    {
    int intScr = 0;
    int first;
    struct qNode *prevPtr;
    struct qNode *quizPtr;
    struct sNode *studentPtr;

    studentPtr = student;

    printf("Entering the Scores\n");
    printf("To jump to next student enter -1\n");


    while(studentPtr!=NULL)
    {
    first = 1;
    printf("%s's scores:\n", studentPtr->name);
    intScr=0;

    while (intScr !=-1)
    {
    scanf("%d", &intScr);

    if(intScr != -1)
    {
    if(!(quizPtr = (qNode*) malloc(sizeof qNode)))
    {
    printf("Out of memory");
    return 0;
    }
    quizPtr->score = intScr;
    quizPtr->link = NULL;

    (first == 1) ? (studentPtr->link = quizPtr prevPtr->link = quizPtr;

    prevPtr=quizPtr;

    first = 0;
    }
    }
    studentPtr = studentPtr->next;
    }
    return 1;
    }

    /*************************************READ LINE******************************/

    int readLine(FILE *inFile, char *currentLine)
    {
    int i=0;
    char ch;

    do
    {
    fscanf(inFile, "%c", &ch);

    ((ch !=10) && (!feof(inFile))) ? (*(currentLine+i) = ch) *(currentLine+i) = '\0');

    i++;
    }while ((ch !=10) && (!feof(inFile)));

    return 0;
    }

    /********************************PRINT SCORES*******************************/

    int printScores()
    {
    struct qNode *quizPtr;
    struct sNode *studentPtr;
    int total;
    int numGrades;
    int classTotal = 0;
    int classNumGrades = 0;

    studentPtr = student;

    while(studentPtr!=NULL)
    {
    printf("\n%s's scores:\n", studentPtr->name);
    numGrades = 0;
    total = 0;
    quizPtr = studentPtr->link;

    while (quizPtr!=NULL)
    {
    printf("%d\t", quizPtr->score);
    total= total + quizPtr->score;
    numGrades++;
    quizPtr = quizPtr->link;
    }

    printf("Ave= %d\tTotal= %d", total/numGrades, total);

    classTotal= classTotal + total;
    classNumGrades = classNumGrades + numGrades;
    studentPtr = studentPtr->next;
    }
    printf("\n");
    return 0;
    }

    /*********************************FREE************* ****************/

    int freeStudents()
    {
    int linkEnd, loopEnd=0;
    struct qNode *quizPtr;
    struct sNode *studentPtr;
    struct qNode *prevPtr;
    struct sNode *sPtr;

    studentPtr = student;

    while ((studentPtr!=NULL) && (!loopEnd))
    {
    if (studentPtr->link != NULL)
    {
    quizPtr = studentPtr->link;
    linkEnd = 0;

    while ((quizPtr!=NULL) && (!linkEnd))
    {
    prevPtr = quizPtr;

    (quizPtr->link != NULL) ? (quizPtr = quizPtr->link) : (linkEnd = 1);

    free (prevPtr);
    }
    }

    sPtr=studentPtr;

    (studentPtr->next != NULL) ? (studentPtr = studentPtr->next): (loopEnd = 1);

    free(sPtr);
    }
    printf("\n");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What errors are you getting?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    debug limked list

    Compiling...
    ls.c
    C:\Windows\Desktop\New Folder (3)\ls.c(58) : error C2065: 'sNode' : undeclared identifier
    C:\Windows\Desktop\New Folder (3)\ls.c(58) : error C2059: syntax error : ')'
    C:\Windows\Desktop\New Folder (3)\ls.c(79) : error C2059: syntax error : '}'
    Error executing cl.exe.

    ls.exe - 3 error(s), 0 warning(s)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Change your struct declarations to this and you'll have better luck. When using a typedef on a struct you need to create the unique identifier for the typedef if you want to omit the struct keyword when you create variables in your code.

    There are more errors, the ones you get are masking the other problems.
    Code:
    typedef struct qNode 
    { 
      int score; 
      struct qNode *link; 
    } QNODE; /* Unique identifier */
    >if(!(studentPtr = (sNode*) malloc(sizeof sNode)))
    This will cause a problem with invalid types due to the typedef, you also don't want to cast the return value of malloc when compiling in C. This will work better and won't bind the type as closely to malloc, which will aid in debugging as well as maintenance:

    if ( !( studentPtr = malloc ( sizeof *studentPtr ) ) )

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM