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
} 

I'm searching your code for the phrases "pop" and "push" and not finding them. You can't have a queue or a stack without "push" and "pop" functions. You probably have them in the code, but are calling them something different. I see "malloc" in the hireEmployee function, so my guess is that is "push". I see "free" in the "DeletePromotionRoster" function, so that might be "pop". It makes it harder to follow the code because I am expecting to see "push" and "pop".

http://www.cplusplus.com/reference/stl/queue/

Anyway, a queue is FIFO. A stack is LIFO. Sounds you accidentally made a stack from your description. If you want a queue, you do one of the following...

1) Insert at the Head and Delete at the Tail
2) Insert at the Tail and Delete at the Head

Subsitute the word "push" for "insert" and "pop" for "delete". Doing it as 2 makes more sense semantically. That's we've been doing all our leaves. The last person goes to the back of the line. The first person gets "served" or "popped" next. First In, First Out. Hence in "deletion" or "popping", the "Head" of the queue changes, just like in real life. The first person gets out of line and is served. The head of the line is the person who used to be the second person.

Now to your code. Closer inspection shows that DeletePromotionRoster is actually "quit" and deletes ALL nodes, not just one node. Hence that isn't "pop". I see a "promoteEmployee" function that has the word "free" in it. Maybe that's "pop". Upon looking closer, it certainly looks like "pop" and it looks like a good "pop" for a queue.

hireEmployee must be "push" then. I see the code NumEmp++ right BEFORE the call to hireEmployee. On the other hand, I see the code NumEmp-- INSIDE the promoteEmployee function. Consistency is vital. Stick NumEmp++ inside the hireEmployee function. It's part of the "push".

That isn't the actual problem though.

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;
}

Let's walk through this code. Let's assume that we have two employees and "Ralph Smith" is at the Head of the queue and "Tim Jones" is at the tail. We want to add "Bill Johnson" to the tail, BEHIND "Tim Jones". "Ralph Smith" remains at the head. The next person will still be "Tim Jones" and the NEW tail, which will be AFTER "Tim Jones" is Bill Johnson, who will be the NEW tail after we're done. We've done this all our lives. Bill Johnson sees a line and goes to the back. That's a queue and that's what needs to happen here.

First you create a new Employee, create storage, and copy the information into the new Employee. So far, so good. So you have pointer to a new Employee named Bill Johnson called TailPtr. Tail still points to the Employee Tim Jones. Now you get to this line...

TailPtr->Next=Tail;

In English, "The employee in line after Bill Johnson is Tim Jones." Presumably you see the problem with this logic. There should be no one in line after Bill Johnson. How do you say, "There should be no one in line after Bill Johnson" in C?

TailPtr->Next = /* fill in */

What you need to do have is "The employee in line after Tim Jones is Bill Johnson." How do you say that in C?

Tail->Next = /* fill in */

I am not guaranteeing that's the only problem, but it is definitely a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.