Program to implement a Static Circular Queue using Array

Program:

#include <stdio.h>
#include <stdlib.h>
int front = -1;
int rear = -1;
int qu[5];

void enqueue()
{
    if ((front == 0 && rear == 4) || front == rear + 1)
        printf("Overflow: Queue is Full. You cannot add more items");
    else
    {
        if (front == -1)
            front = rear = 0;
        else if (rear == 4)
            rear = 0;
        else
            rear = rear + 1;
        printf("\nenter value");
        scanf("%d", &qu[rear]);
    }
}
void dequeue()
{
    if (front == -1)
        printf("underflow!!!. Queue is empty. No item to delete");
    else
    {
        printf("Deleted item is : %d", qu[front]);
        if (front == rear)
            front = rear = -1;
        else if (front == 4)
            front = 0;
        else
            front = front + 1;
    }
}
void peek()
{
    if (front == -1)
        printf("Queue is empty. No item to display");
    else
    {
        printf("Item at the Front is :%d", qu[front]);
    }
}
void display()
{
    if (front == -1)
        printf("Queue is empty. No item to display");
    else
    {
        printf("\nInformation of Queue is : \n");
        if ( front<=rear)
        for (int i = front; i <= rear; i++)
            printf("\n%d", qu[i]);
        else
            {
                for (int i = front; i <5; i++)
                printf("\n%d", qu[i]);
                for (int i = 0; i <=rear; i++)
                printf("\n%d", qu[i]);
            }
    }
}
void main()
{
    int ch;
    printf("****************************************\n");
    printf("****************************************\n");
    printf("**  Static Circular Queue Using Array **\n");
    printf("**   Program Created By Sheetal Garg  **\n");
    printf("**         Assistant professor        **\n");
    printf("**               9467863365           **\n");
    printf("****************************************\n");
    printf("****************************************\n");
    while (1)
    {
        printf("\nOptions Available are\n");
        printf("1. Enqueue \n");
        printf("2. Dequeue \n");
        printf("3. Peek \n");
        printf("4. Display \n");
        printf("5. Exit \n");
        printf("\nEnter your choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            enqueue();
            break;
        case 2:
            dequeue();
            break;
        case 3:
            peek();
            break;
        case 4:
            display();
            break;
        case 5:
            exit(0);
        default:
            printf("Wrong choice entered");
        }
    }
}

Output:

****************************************
****************************************
**  Static Circular Queue Using Array **
**   Program Created By Sheetal Garg  **
**         Assistant professor        **
**               9467863365           **
****************************************
****************************************

Options Available are
1. Enqueue 
2. Dequeue 
3. Peek 
4. Display 
5. Exit 

Enter your choice : 1

enter value10

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value20

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value30

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value40

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value50

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1
Overflow: Queue is Full. You cannot add more items
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 10
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 20
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

30
40
50
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value100

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 1

enter value200

Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

30
40
50
100
200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 3
Item at the Front is :30
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 30
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

40
50
100
200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 40
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

50
100
200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 50
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

100
200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 100
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4

Information of Queue is :

200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
Deleted item is : 200
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 4
Queue is empty. No item to display
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 2
underflow!!!. Queue is empty. No item to delete
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice : 5
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.