Search code examples
cpointersdata-structureslinked-listdynamic-memory-allocation

Allocating Data Structures and passing info into a Data Structure


How do I make this program with no warnings...Keeps saying tail uninitialized for a warning. Trying to make a loop that prints out all my data instead of having to call the function every time with a different parameter. If I set tail = to COP3330, then it doesn't print out the COP3330 info since it's then = to NULL. Thank you!

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

typedef struct UCF_Classes
{
   char *ClassIdentifier, *ClassName, *Department;
   int Credits;
   struct UCF_Classes *next;
}Info;

Info *CreateList(char *ClassNumber, char *NameOfClass, char *DepartmentName, int NumOfCredits)
{
    Info *NewClass;

    NewClass = (Info *) malloc(sizeof(Info));

    NewClass->ClassIdentifier = ClassNumber;
    NewClass->ClassName = NameOfClass;
    NewClass->Department = DepartmentName;
    NewClass->Credits = NumOfCredits;

   return NewClass;
}

void WalkListAndDisplay(Info *walker)
{
   printf("%s\n", walker->ClassIdentifier);
   printf("%s\n", walker->ClassName);
   printf("%s\n", walker->Department);
   printf("%d\n\n", walker->Credits);
}

int main()
{
   Info *COP3223, *COP3502C, *COP3503C, *COP3330, *head, *tail;

   COP3223 = CreateList("COP3223", "Intro to Programming with C", "College of Engineering and Computer Science", 3);
   COP3502C = CreateList("COP3502C", "Computer Science I", "College of Engineering and Computer Science", 3);
   COP3503C = CreateList("COP3503C", "Computer Science II", "College of Engineering and Computer Science", 3);
   COP3330 = CreateList("COP3330", "Object Oriented Programming", "College of Engineering and Computer Science", 3);

   head = COP3223;

   COP3223->next = COP3502C;
   COP3502C->next = COP3503C;
   COP3503C->next = COP3330;
   COP3330->next = tail;
   tail->next = NULL;

   while(head->next != NULL)
   {
       WalkListAndDisplay(head);
       head = head->next;
   }

   return 0;
}

Solution

  • Your while loop is not checking the current node for null before trying to print it, it's checking the next node. If you think about the order here, it's like it's saying, "The next node is available, so print the current one." This means that when it gets to the end of the list, it thinks it's done one node early because there is no node after it. It should instead say, "The current node is available, so print it and then step forward."

    Your tail variable, as SSC pointed out, is not needed, and was actually hiding this bug by making the last node in your list have something besides NULL to point to. This made the flawed while condition true for the last node. Your code would have crashed with an empty list though because head->next would have dereferenced a NULL pointer.

    Remove tail, and change your while statement to this:

    while(head != NULL)
    {
        WalkListAndDisplay(head);
        head = head->next;
    }