Queue with Array in c by Indranil Bhattacharjee

 


      Welcome Study Global

write a program Queue data structure in c programming by Indranil Bhattacharjee.

#include <stdio.h>
#include <stdlib.h>
#define N 5
int front = -1;
int rear = -1;
int array[N];
void enqueue(int v)
{
    if (rear == N - 1)
    {
        printf("Queue is overflow");
    }
    else if (front == -1 && rear == -1)
    {
        front = rear = 0;
        array[rear] = v;
    }
    else
    {
        rear++;
        array[rear] = v;
    }
}
void dequeue()
{
    if (front == -1 && rear == -1)
    {
        printf("Queue underflow");
    }
    else if (front == rear)
    {
        front = rear = -1;
    }
    else
    {
        front++;
    }
    printf("Deleted element is %d", array[front - 1]);
}
void peek()
{
    if (front == -1 && rear == -1)
    {
        printf("Queue is empty\n");
    }
    else
    {
        printf("peek elements is:- %d\n", array[front]);
    }
}
void display()
{
    if (front == -1 && rear == -1)
    {
        printf("Queue is empty\n");
    }
    int i;
    for (i = front; i <= rear; i++)
    {
        printf("%d ", array[i]);
    }
}
int main()
{
    int ch, value, choice;
    while (1)
    {
        printf("1 for Enqueue 2 for Dequeue 3 for Display 4 for peek 5 for exit the program:");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            while (1)
            {
                printf("Enter Queue elements:");
                scanf("%d", &value);
                enqueue(value);
                printf("Enter value continue hit 1 discontinue hit 0:");
                scanf("%d", &ch);
                if (ch == 0)
                {
                    break;
                }
            }
            break;
        case 2:
            dequeue();
            printf("\n");
            break;
        case 3:
            printf("Queue elements are:-");
            display();
            printf("\n");
            break;
        case 4:
            peek();
            break;
        case 5:
            printf("Exit the program");
            exit(0);
        default:
            printf("xxxxxxxx Invalid Choice xxxxxxxx");
        }
    }
}

Comments