Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include typedef struct { int iPriority; /* Priority of the student to be enrolled */ int iStudentID; /* ID of the student */

image text in transcribed

#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 ; iiNumEnrolled; 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 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(" "); } 

Can it be in C language please. This is skeleton code and I must use it and fill in the code where its necessary.

1. Implementing a Priority Queue as a Sorted Doubly Linked List (15 points) This is a program for simulating enrollment of students in a set of courses. Included in this is a waitlist which allows students to queue up for possible free space in the course. This free space may come from additional seats being added to the course or from students choosing to drop the course. The waitlist queue is a priority queue where some students are given a higher priority for being added to the course (e.g., seniors may be given a higher priority than freshman or majors given preference over non-majors) The priority queue is represented using a sorted doubly linked list. Higher numbers are associated with higher priorities. The higher the priority a student has the closer to the front of the queue they should be. In the case of students with the same priority, the one who entered the queue first should be given preference. There are two data files used for this assignment. The code for reading from these files has already been provided (see the main of the provided code). Students are specified by a 3 digit id and courses are specified by a 4 digit id. The "a3_EnrollData.txt" file specifies a sequence of three types of operations to be performed on the courses/students. You are responsible for implementing functions which execute these three operations (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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions