Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C language typedef struct StudentStruct { char LastName[MAX_LENGTH]; char FirstN

ID: 3625620 • Letter: C

Question

C language

typedef struct StudentStruct
{
char LastName[MAX_LENGTH];
char FirstName[MAX_LENGTH];
int StudentNumber; // Holds the student's ID. This value is
// equal to the number of student
struct StudentStruct *Next; // Pointer to the next most recently new Student
} Student;

Student* promoteStudent(Student* Head);
// Precondition: A pointer to the front of queue is passed in as a parameter,
// and at least one student remains with the company.
// Postcondition: The student with the highest seniority is removed from the student
// roster and an appropriate statement about who was promoted is printed
// to the screen. A pointer to the new front of the queue is returned
// to the calling function.

What I have so far. I tried it and it still promoting the recent one where it should be the oldest one. Can someone help make a correct version.

Student* promoteStudent(Student* Head)
{
Student*TmpPointer=Head; //A temp pointer set for Student equal to Head;
if(Head==NULL) //If Head is NULL
{
printf(" All student have been promoted.");
return NULL;
}
else if(Head->Next==NULL)
{
printf(" The student has been promoted ");
return NULL;
}
else
{
TmpPointer=Head->Next;
Head=TmpPointer;
free(TmpPointer); //deallocate the Head of the student
NumEmp--; //Decrement the student number
}
return Head;

Explanation / Answer

So here's where your problem could be: In your last else statement, you are setting the TmpPointer to be Head->Next. You change Head to be Head->Next (via the TmpPointer) Now you are trying to free the TmpPointer, which is still Head->Next. Aren't you supposed to be trying to free the "Head"? A proposed solution would be to just set Head = Head->Next; and then free(TmpPointer) because in the very beginning of your are already setting TmpPointer = Head;