QUEUE using dynamic memory allocation

/*
    Author : PATEL DIVYAKUMAR BHARATBHAI
    Program : Queue code - linear Queue
    Date : 01/10/2022
*/

#include <stdio.h>
#include <stdlib.h>

// Global variable declaration // 
int *queue;
int front, end, queueSize;

// Function declaration // 
void addQueue();
void deleteQueue();
void displayQueue();

int main()
{
    int choice;
    printf("Enter size of Queue: ");
    scanf("%d", &queueSize);

    queue = (int *)malloc(queueSize * sizeof(queueSize));

    front = end = (-1) ;

    printf("1. ADD \n2. DELETE \n3. DISPLAY \n4. EXIT \n");
    while (choice != 4)
    {
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            addQueue();
            break;
        
        case 2:
            deleteQueue();
            break;
            
        case 3:
            displayQueue();
            break;

        default:
            printf("BYE... ");
            exit(0);
            break;
        }
    }
    return 0;
}

// Function defination //

// Addition function to add element in queue //
void addQueue()
{
    if (end == (queueSize-1))
    {
        printf("Queue is FULL \n");
    }
    else
    {   
        int val;
        end = end + 1 ;
        printf("Enter the value: ");
        scanf("%d", &val); // we can dirctly done by replace val to queue[end] ... //
        queue[end] = val ;
    }
}

// Delation function to delete element from queue // 
void deleteQueue()
{
    if (front == end)
    {
        printf("Queue is EMPTY \n");
        front = end = (-1);
    }
    else
    {
        front = front + 1;
        printf("The deleted value is %d \n", queue[front]);
    }  
}

// Display function to display the element of queue // 
void displayQueue()
{
    if (front == end)
    {
        printf("Queue is EMPTY \n");
        front = end = (-1);
    }
    else
    {
        for (int i = (front+1); i <= end; i++)
        {
            printf("%d \n", queue[i]);
        }
    }
} 

Post a Comment

0 Comments