I am implementing a priority queue waitlist using a doubly linked list. My method creates a new node (priority and student id). Depending on the node priority the method will sort the node into the queue.
what I get is what I should get
Waitlisted: 109 in 2123 | Waitlisted: 109 in 2123
Current waitlist: 109 | Current waitlist: 109
|
Waitlisted: 105 in 2123 | Waitlisted: 105 in 2123
Current waitlist: 105 | Current waitlist: 105 109
|
Waitlisted: 108 in 2123 | Waitlisted: 108 in 2123
Current waitlist: 109 105 | Current waitlist: 105 108 109
|
Waitlisted: 106 in 2123 | Waitlisted: 106 in 2123
Current waitlist: 106 | Current waitlist: 105 106 108 109
|
Waitlisted: 107 in 2123 | Waitlisted: 107 in 2123
Current waitlist: 109 106 | Current waitlist: 105 106 107 108 109
I am able to insert a new node when the queue is empty on the first loop. Starting from the 2nd run the return values of the queue are wrong.
Code
void enqueue( PQNode** ppFront, WaitlistEntry info ){
/* create a new node to store the info */
PQNode *temp = (PQNode*)malloc(sizeof(PQNode)); //create a new node to store the info
temp->info = info;
temp->pNext = NULL;
temp->pPrev = NULL;
/* check if the LL is empty and add the new node to the front if it is */
if(*ppFront == NULL){
*ppFront = temp;
return;
}
/* check if the new node should come before the first node in the LL */
if(temp->info.iPriority > (*ppFront)->info.iPriority){
temp->pNext = *ppFront;
(*ppFront)->pPrev = temp;
*ppFront = temp;
return;
}
/* 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 */
while((*ppFront)->pNext != NULL && temp->info.iPriority <= (*ppFront)->info.iPriority){
*ppFront = (*ppFront)->pNext;
}
/* 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 */
if((*ppFront)->pNext == NULL){
(*ppFront)->pNext = temp;
temp->pPrev = *ppFront;
return;
}
temp->pPrev = *ppFront;
temp->pNext = (*ppFront)->pNext;
(*ppFront)->pNext->pPrev = temp;
(*ppFront)->pNext = temp;
return;
}
Structs
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;
I've managed to fix up the code some, but I'm still stuck.
enqueuelogic, but it is presumed that there is an error in the display function without relation to it. - BLUEPIXYconnect(prev, current, next)anddisconnect(prev, next)functions because the logic can be confusing when dealing with NULL lists, NULL prev/next pointers, etc. Similarly, finding the insertion point can be put into its own function as well. See this paste for an example of what I'm talking about. The main functions to look at areFindPriorityEndandEnqueue. - user539810