Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.5 kB
1
Indexable
Never
Code:

// Used for input & output functions
#include <stdio.h>

// Used for exit function
#include <stdlib.h>

// Used for string function
#include <string.h>

#define MAX 50

// Function used to enter Customer in queue
void customerEntry();

// Function used to exit Customer in queue
void customerExit();

// Function used to print Waiting in queue
void customerWaiting();

// Array to store customer name
char queue[MAX][MAX];

// Intialize from & rear to -1
int rear = - 1;
int front = - 1;

// Main Function
int main()
{
    // Used to store choice
    int choice;
  
    // Infinite loop till user exit
    while (1)
    {
        // Print meu
        printf("\n1.Customer Entry\n");
        printf("2.Customer Exit\n");
        printf("3.Waiting Customers\n");
        printf("4.Exit\n");
      
        // Take choice
        printf("\nEnter your choice : ");
        scanf("%d", &choice);
      
        // Perform based on choice
        switch(choice)
        {
            // If choice is 1
            case 1:
                    // Call customerEntry()
                    customerEntry();
                    break;
                  
            // If choice is 2
            case 2:
                  
                    // Call customerExit()
                    customerExit();
                    break;
                  
          
            // If choice is 3
            case 3:
                  
                    // Call customerWaiting()
                    customerWaiting();
                    break;
                  
          
            // If choice is 4
            case 4:
                    exit(1);
                  
          
            // If any other choice
            default:
                    printf("Error: Invalid Choice.");
        }
    }
}

// Function used to enter Customer in queue
void customerEntry()
{
    // Used to store name
    char custName[MAX];
  
    // If Queue is full
    if(rear == MAX - 1)
      
        // Print warning
        printf("Customer Queue is already full... Please wait for sometime");
  
    // If queue is not full  
    else
    {
        // If queue is empty
        if(front== - 1)
          
            // Set front to 0
            front = 0;
          
        // Read newline
        getchar();
      
        // Take name  
        printf("Enter the customer name: ");
        scanf("%[^\n]s", custName);
      
        // Increase rear by 1
        rear = rear + 1;
      
        // Add customer at rear of queue
        strcpy(queue[rear], custName);
      
        printf("Customer Successfully Enter in Queue\n");
    }
}

// Function used to exit Customer in queue
void customerExit()
{
    // If qqueue is empty
    if(front == - 1 || front > rear)
    {
        printf("Queue is already empty");
        return;
    }
    // If queue is not empty
    else
    {
        printf("Customer exit from queue : %s\n", queue[front]);
        front = front + 1;
    }
}

// Function used to print Waiting in queue
void customerWaiting()
{
    int i;
  
    // If queue is empty
    if(front == - 1)
  
        printf("Queue is qlready empty");
      
    // If Queue is not empty
    else
    {
        printf("\nWating list of Customer: ");
      
        // Loop for each Customer
        for(i = front; i <= rear; i++)
        {
            // Print name
            printf("%s ", queue[i]);
          
            if(i != rear)
          
                printf(" --> ");
        }
      
        printf("\n");
    }
}