Question
I needed help coding the functions in bold in the code given below. (1) enroll - Attempt to enroll the student in the course (2)
I needed help coding the functions in bold in the code given below.
(1) enroll - Attempt to enroll the student in the course
(2) addSeats - Add the specified number of seats to the course and adds students from the waitlist to fill these seats
(3) drop - Drop the student from the course and fill the empty seat from the waitlist
Some code has been provided for you to help in creating functions to execute the above three operations. In addition to the above you also need to implement your priority queue using a doubly linked list. Specifically,
(1) enqueue - Add a new waitlist element to the priority queue. This element needs to be inserted in sorted order.
(2) dequeue - Remove and return the front element from the LL.
Code Provideed:
#include#include #include typedef struct{ int iPriority; /* Priority of the student to be enrolled */ int iStudentID; /* ID of the student */ } WaitlistEntry; typedef struct PQNode { WaitlistEntry info; /* WaitlistEntry stored in the node (sorted with largest priority first) */ struct PQNode* pNext; /* Pointer to next node in the LL */ struct PQNode* pPrev; /* Pointer to previous node in the LL */ } PQNode; typedef struct{ int iCourseNumber; /* Course number of the course */ int* iStudentIDs; /* Array of IDs of students enrolled in the course */ int iNumEnrolled; /* Number of Students currently enrolled in course */ int iMaxEnrolled; /* Max number of Students that can enroll in the course */ PQNode* pFront; /* Priority queue representing the waitlist for the course */ } Course; typedef enum { FALSE, TRUE } bool; /* PQ function prototypes */ void printQueue( PQNode* pFront ); /* To be completed by you */ WaitlistEntry dequeue( PQNode** ppFront ); void enqueue( PQNode** ppFront, WaitlistEntry info ); /* function prototypes for course management */ void enrollStudent( Course* pCourse, int iStudentID ); void waitlistStudent( Course* pCourse, WaitlistEntry wl ); int searchCourses( Course* pCourses, int iNumCourses, int iCourseID ); int findStudent( Course* pCourse, int iStudentID ); /* To be completed by you */ void addSeats( Course* pCourse, int NumAdded ); void attemptEnrollment( Course* pCourse, int iStudentID, int iPriority ); void dropStudent( Course* pCourse, int iStudentID ); /* provided code - DO NOT CHANGE * main simulates the waitlist for a number of courses by calling your functions */ int main( ) { char courseFileName[] = "a3_CourseData.txt"; char enrollFileName[] = "a3_EnrollData.txt"; char szCommand[256]; FILE *in_file = fopen(courseFileName, "r"); Course *pCourses; int i; int iNumCourses; int iCourseNumber; int iCourse; int iStudent; int iPriority; int iNumAdd; printf("CS 2123 Assignment 3 "); if (in_file == NULL) { printf("File %s not found. ", courseFileName); return -1; } /* read the number of courses from file and allocate space to store them */ fscanf( in_file, "%d", &iNumCourses ); pCourses = (Course*) malloc( iNumCourses*sizeof(Course) ); /* initialize course data */ for( i=0 ; i iNumEnrolled; i++ ){ if( pCourse->iStudentIDs[i]==iStudentID ) return i; } return -1; } /* * addSeats increases the maximum enrollment of the course by NumAdded. * Use realloc to increase the number of student IDs which can be storexd in pCourse. * dequeue students from the waitlist call enrollStudent on them to add them to the * course until the course is full or the waitlist is empty. * * precondition: NumAdded > 0 */ void addSeats( Course* pCourse, int NumAdded ){ /* increase the maximum enrollment of pCourse */ /* Print that seats have been added to the course (no changes needed to this line) */ printf("Added Seats:\t\t%d seats added to the course %d ", NumAdded, pCourse->iCourseNumber ); /* add students to the course until it is full or the waitlist is empty */ } /* * attemptEnrollment attempts to enroll the given student in the course pCourse. * If there is room left in the course call enrollStudent to add the student. * Otherwise call waitlistStudent to add the student to the course's waitlist. */ void attemptEnrollment( Course* pCourse, int iStudentID, int iPriority ){ } /* provided code - DO NOT CHANGE * enrolls the given student in the given course * * precondition: the course has room left in it to enroll the student */ void enrollStudent( Course* pCourse, int iStudentID ){ if( pCourse->iMaxEnrolled <= pCourse->iNumEnrolled ){ printf("ERROR - enrollStudent called with a full course %d ", pCourse->iCourseNumber ); return; } pCourse->iStudentIDs[pCourse->iNumEnrolled] = iStudentID; pCourse->iNumEnrolled++; printf("Enrolled:\t\t%d in %d ", iStudentID, pCourse->iCourseNumber ); } /* provided code - DO NOT CHANGE * adds the given student to the courses waitlist * * precondition: the course is full */ void waitlistStudent( Course* pCourse, WaitlistEntry wl ){ if( pCourse->iMaxEnrolled > pCourse->iNumEnrolled ){ printf("ERROR - waitlistStudent called with a non-full course %d ", pCourse->iCourseNumber ); return; } enqueue( &pCourse->pFront, wl ); printf("Waitlisted:\t\t%d in %d ", wl.iStudentID, pCourse->iCourseNumber ); printf("Current waitlist:\t" ); printQueue( pCourse->pFront ); } /* * dropStudent drops the given student from the course. * If the waitlist is not empty you should dequeue the first student and enroll them in the now non-full course. * It should print an error if the student to be dropped is not enrolled in the course. */ void dropStudent( Course* pCourse, int iStudentID ){ /* Use findStudent to determine where student is in iStudentIDs array */ if( TRUE /* replace this condition */ ){ printf("ERROR - dropStudent called to drop non-enrolled student %d from %d ", iStudentID, pCourse->iCourseNumber ); return; } /* Use a loop to shift down students in iStudentIDs array and fill gap from the dropped student */ /* Print that student has been dropped from the course (no changes needed to this line) */ printf("Dropped:\t\t%d from %d ", iStudentID, pCourse->iCourseNumber ); /* Add student if the waitlist is not empty */ } /* * insert a new node which contains info into the priority queue contained in the linked list pointed to by ppFront */ void enqueue( PQNode** ppFront, WaitlistEntry info ){ /* create a new node to store the info */ /* check if the LL is empty and add the new node to the front if it is */ /* check if the new node should come before the first node in the LL */ /* walk back through the previous nodes in the LL until the correct insertion point is found */ /* remember to also check whether the previous node is NULL */ /* insert the new node into the place you found with your loop */ /* note you may need a special case for when the previous node is NULL */ } /* * remove the node at the front of the priority queue and return its info * * precondition: the queue is not empty */ WaitlistEntry dequeue( PQNode** ppFront ){ WaitlistEntry ret; return ret; } /* provided code - DO NOT CHANGE * print the contents of the priority queue contained in the linked list pointed to by ppFront */ void printQueue( PQNode* pFront ){ while( pFront!=NULL ){ printf("%d ", pFront->info.iStudentID); pFront = pFront->pPrev; } printf(" "); }
data in a3_CourseData.txt
2123 5 2233 5 3333 5 3343 0 data in a3_EnrollData.txt
enroll 2123 100 1 enroll 2123 101 4 enroll 2123 102 2 enroll 2123 103 3 enroll 2123 104 1 enroll 2123 109 1 enroll 2123 105 4 enroll 2123 108 2 enroll 2123 106 4 enroll 2123 107 3 addSeats 2123 1 addSeats 2123 2 drop 2123 101 drop 2123 100 drop 2123 103 enroll 3343 100 1 enroll 3343 101 4 enroll 3343 102 2 addSeats 3343 5 enroll 3343 103 3 addSeats 3343 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started