Search code examples
cloopsdata-structuresinfinite-loopdoubly-linked-list

Why is there an infinite loop here? (linked list printing)


I am doing a little practice with linked lists, these are the structures.

typedef struct roomList roomList;
typedef struct school school;
typedef struct studentList studentList;
roomList *getRoom(school* school, int class, int roomNr);

struct studentList{

    char *name;
    int class; 
    float grade;
    int roomNr;
    studentList *next;
    studentList *prev;
};


struct roomList{

    int nrOfStudents;
    int roomNr;
    studentList *students; //pointer to student list.
    roomList *next;
    roomList *prev; 
};



struct school{

    int totalStudents;
    roomList *Class[13]; //array of classes, each index contains rooms.
};

This is where the infinite loop is happening, it's a function to print all students within a room.

void printRoom(school *school, int class, int roomNr)
{
    roomList *room = getRoom(school, class, roomNr);
    studentList *student;

    if(room != NULL)
    {
        int i = 1;
        printf("Nr of students: %d\n", room->nrOfStudents);
        while(room->nrOfStudents != 0 && student != NULL)
        {
            student = room->students;
            printf("%d - \"%s\" ",i, student->name);
            student = student->next;
            i++;
        }
    }   
}

This is how I'm creating a student

studentList *createStudent(int class, char *name, int roomNr)
{
    studentList *newNode;
    newNode = (studentList*)calloc(1, sizeof(studentList));
    newNode->class  = class;
    newNode->name   = (char*)malloc(strlen(name)+1);
    strcpy(newNode->name, name);
    newNode->roomNr = roomNr;
    newNode->grade  = 0;
    newNode->next   = newNode->prev = NULL;

    return newNode;
}

And finally, this is how I'm inserting a student into a room.

void insertStudentToRoom(school* school, int class, int roomNr, char *name)
{
    roomList *room;
    room = getRoom(school, class, roomNr);
    studentList *newStudent;
    newStudent = createStudent(class, name, roomNr);

    if(room->students != NULL)
    {
        newStudent->next = room->students;
        room->students->prev = newStudent;
        room->students = newStudent;
        room->nrOfStudents++;
        school->totalStudents++;
    }
    else
    {
        room->students = newStudent;
        room->nrOfStudents++;
        school->totalStudents++;
    }
}

The infinite infinite loop only happens when I insert more than one student into a room, and exits fine when there's only one student, I've tried fumbling around with exit conditions for my while() to no avail.


Solution

  •     while(room->nrOfStudents != 0 && student != NULL)
        {
            student = room->students;
            printf("%d - \"%s\" ",i, student->name);
            student = student->next;
            i++;
        }
    

    Look closely. You never change room in the loop. So student = room->students; is going to set the very same value for student every single time in the loop. If it didn't break after the first time, it won't break any other time.

    You probably want to take student = room->students; out of the loop. You only want to point at the first student in the room once.