I am still new with c programming, I need to create queue with first in first out but it is going last in first out. Can someone give me advice in how to make it work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 30
#define NUMOFEMPLOYEE 15
typedef struct EmployeeStruct
{
char LastName[MAX_LENGTH];
char FirstName[MAX_LENGTH];
int EmployeeNumber; // Holds the employee's ID. This value is
// equal to the number of employees
struct EmployeeStruct *Next; // Pointer to the next most recently hired Employee
} Employee;
Employee* hireEmployee(Employee* Tail, char LastName[MAX_LENGTH], char FirstName[MAX_LENGTH]);
Employee* promoteEmployee(Employee* Head);
Employee* getnextPromotionCandidate(Employee* Head);
void printEmployees(Employee* Head);
void DeletePromotionRoster(Employee* Head);
int NumEmp=0;
char getAnswer;
int main()
{
Employee* Tail;
Employee* Head=NULL;
int choice;
char LastName[MAX_LENGTH];
char FirstName[MAX_LENGTH];
do
{
printf("\n Welcome to the promotional roster program.");
printf("\n Please choose to any of the following option to let the program works.");
printf("\n Press [H] to hire an employeee.");
printf("\n Press [P] to promote an employee.");
printf("\n Press [N] to get the next promotional employee.");
printf("\n Press [D] to display all employee currently hired.");
printf("\n Press [Q] to quit.");
printf("\n Enter choice: ");
scanf(" %s", &choice);
switch(toupper(choice))
{
case 'H':
if(NumEmp<NUMOFEMPLOYEE-1) //If NumEmp is less than the max num of employee
{ //Then you hire an employee to get scanf
printf("Please enter the last name of the employee.");
printf("\nLast Name: ");
scanf(" %s", LastName);
printf("Please enter the first name of the employee. ");
printf("\nFirst Name: ");
scanf(" %s", FirstName);
NumEmp++;
Head= hireEmployee(Head, LastName, FirstName);
}
else
{
printf(" There are no more slots left in the program. ");
}
break;
case 'P':
if(Head!=NULL);
{
printf(" The person you promoted is: "); //The person you promoted is promoted to head
printf(" %s\n", Head->LastName);
printf(" %s\n", Head->FirstName);
printf("The employee id is: %d\n", Head->EmployeeNumber);
}
Head=promoteEmployee(Head);
printf("\n");
break;
case 'N':
Head=getnextPromotionCandidate(Head);
if(Head!=NULL)
{
printf("\n The next candidate is: ");
printf(" %s", Head->LastName);
printf(" %s", Head->FirstName);
printf(" Employee Id: %d", Head->EmployeeNumber);
}
printf("\n");
break;
case 'D':
printEmployees(Head);
printf("\n");
break;
case 'Q':
printf(" Thank you for using the roster program");
printf(" we hope to see you again.");
DeletePromotionRoster(Head);
break;
default:
printf(" You did not choose any of the option above.");
printf(" Please try again.");
}
}while(toupper(choice)!='Q');
return(0);
}
Employee* hireEmployee(Employee* Tail, char LastName[MAX_LENGTH], char FirstName[MAX_LENGTH])
{
Employee*TailPtr; //Declares pointer for Employee call newNode
TailPtr=(Employee*)malloc(sizeof(Employee)); //newNode is memory allocated
strcpy(TailPtr->LastName, LastName); //String copy newNode to scan last name
strcpy(TailPtr->FirstName, FirstName); //String copy newNode to scan first name
TailPtr->EmployeeNumber=NumEmp; //Set EmployeeNumber=NumEmp
TailPtr->Next=Tail;
Tail=TailPtr;
return TailPtr;
}
Employee* promoteEmployee(Employee* Head)
{
if(Head==NULL) //If Head is NULL
{
printf(" All employee have been promoted.");
}
else
{
Employee*TmpPointer=Head; //A temp pointer set for Employee equal to Head;
Head=TmpPointer->Next;
free(TmpPointer); //deallocate the Head of the employee
NumEmp--; //Decrement the employee number
}
return Head;
}
Employee* getnextPromotionCandidate(Employee* Head)
{
if(Head==NULL)
{
printf(" There are employee to promote.");
return NULL;
}
else
{
return Head;
}
}
void printEmployees(Employee* Head)
{
Employee*Ptr=Head;
if(Head==NULL)
{
printf(" The employee roster is empty.");
}
while(Ptr!=NULL)
{
printf("\n Employee Name: ");
printf(" %s", Ptr->LastName);
printf(" %s ", Ptr->FirstName);
Ptr=Ptr->Next; //Ptr now points to the next person
}
printf("\n");
}
void DeletePromotionRoster(Employee* Head)
{
Employee *newNode= Head; //
while(Head!=NULL)
{
Head=newNode->Next;
free(Head);
newNode=Head;
}
NumEmp=0; //Set employee number to zero
}