user8812634
user8812634

Reputation: 15

How do I implement a linked list with multi-element struct?

After several hours of reading, I think I can clarify my question. This is an assignment-related question but I am looking for help on a concept; not a solution to my exact code. My code is included for visualization purposes.

I have the following data in a text file that is read into my program:

HIS1043.002 MH2.102   MWF   1:00-1:50pm     120 35.00
GEO1013.005 MB1.101   TR   12:30-1:45pm       5 35.00
MAT1214.003 MS1.02.03 TR    2:00-3:15pm       1 35.00
CS1713.002  NPB1.202  MWF   1:00-1:50pm       0 50.00
MAT3013.001 MS1.02.07 TR    2:00-3:15pm       1 35.00
ENG1023.001 MH2.202   MWF  10:00-10:50am     15 35.00

And here are the two typedef structs I am using:

// Course Definition
typedef struct
{
    StudentNode *pWaitlistHead;    
    char szCourseId[12];  
    char szRoom[15];
    char szDays[15];
    char szTimes[15];  
    int  iAvailSeats; 
    double dFee; 
} Course;


// Node for course list
typedef struct CourseNode {
    struct CourseNode* pNext;
    Course course;
} CourseNode;

Here is the function that reads in the data from the file. Within the function, I call two other functions: one to allocate space for the course node and one to add the data to the list.

int getCourses(Course courseM[])
{
    char szInputBuffer2[100];
    int i = 0;

    while(fgets(szInputBuffer2, 100, pFileCourses) != NULL)
    {
        sscanf(szInputBuffer2, "%12s  %15s %8s %15s %d %lf", 
                          courseM[i].szCourseId, courseM[i].szRoom, 
                          courseM[i].szDays, courseM[i].szTimes, 
                          &courseM[i].iAvailSeats, &courseM[i].dFee);
    }
    //allocate space for linked-list
    pNew = allocateNodeC(courseM); //???

    //add courses to linked list
    insertN2CourseList(&pHead, &pNew);  

    return i;
}

My question is concerning how to handle the course description data in the list implementation. Do I need to address each element individually in the allocation? What about when reading in all the data from the file?

    //allocate space for CourseNode linked list
CourseNode *allocateNodeC(Course courseM[])
{
    CourseNode *pNew = malloc(sizeof(CourseNode));
    pNew->pNext = NULL;
    pNew->course.szCourseId = courseM[i].szCourseId; //?????
    pNew->course.szRoom = courseM[i].szRoom; //?????
    pNew->course.szDays = courseM[i].szDays; //?????
    pNew->course.szTimes = courseM[i].szTimes; //?????
    pNew->course.iAvailSeats = courseM[i].iAvailSeats; //?????
    pNew->course.dFee = courseM[i].dFee; //?????
    return pNew;
}
//add courses to linked list
CourseNode insertN2CourseList(CourseNode **ppHead, CourseNode *ppNew)
{
    CourseNode p*;
    if(*ppHead == NULL)
    {
        *ppHead = pNew;
        return;
    }
    for(p = *ppHead; p->pNext != NULL; p = p->pNext);
}

Upvotes: 1

Views: 97

Answers (3)

user8812634
user8812634

Reputation: 15

This is what I needed to do to allocate memory for the linked course list:

CourseNode *allocateNode(Course course, CourseNode *pNext)
{
    CourseNode *pNew = malloc(sizeof(CourseNode));
    pNew->course = course;
    pNew->pNext = NULL;
}

And this is how I added the data from the array into the linked list:

void insertIntoList(CourseNode **ppHead, CourseNode *pNew)
{
    CourseNode *p;
    if(*ppHead == NULL) {
        *ppHead = pNew
        return;
    }

    for(p = *ppHead; p->pNext != NULL; p = p->pNext){
        //do nothing
    }
    p->pNext = pNew;
}

The calls for each function were placed inside of a loop that increased after each course description had been read into the program and then inserted into/add to the linked list before going to the next course description.

Upvotes: 0

H.S.
H.S.

Reputation: 12634

szCourseId, szRoom, szDays and szTimes are of type char [] which is used to store string read from file:

        sscanf(szInputBuffer2, "%12s  %15s %8s %15s %d %lf", 
                      courseM[i].szCourseId, courseM[i].szRoom, 
                      courseM[i].szDays, courseM[i].szTimes, 
                      &courseM[i].iAvailSeats, &courseM[i].dFee);

and in the allocateNodeC(), you are trying to assign to array:

    pNew->course.szCourseId = courseM[i].szCourseId; //?????
    pNew->course.szRoom = courseM[i].szRoom; //?????
    pNew->course.szDays = courseM[i].szDays; //?????
    pNew->course.szTimes = courseM[i].szTimes; //?????

In C, arrays are not assignable. Instead, you should copy the content of members of courseM to the new created node using strcpy(), like this:

    strcpy (pNew->course.szCourseId, courseM[i].szCourseId);
    strcpy (pNew->course.szRoom, courseM[i].szRoom);
    strcpy (pNew->course.szDays, courseM[i].szDays);
    strcpy (pNew->course.szTimes, courseM[i].szTimes);

Also, this doesn't seem to be actual code because in the function allocateNodeC() there is no declaration if i and you are accessing courseM[i].

Upvotes: 1

reece
reece

Reputation: 8145

This is a single linked list -- i.e. a list that only points in one direction (next) instead of both directions (next, previous).

Using --> to indicate pointers, and [...|next] to indicate an item in the list...

For the empty case, you have:

head --> NULL

After adding a single item, you have:

head --> [1|next] --> NULL

After adding the second item, you can have either:

head --> [2|next] --> [1|next] --> NULL

or:

head --> [1|next] --> [2|next] --> NULL

depending on whether you iterated to the end of the list (the second example), or just updated from the head pointer (the first example). Adding other items work in the same way as adding the second item.

Upvotes: 0

Related Questions