Program to implement a Static Circular Queue using Array of Structure

Program:

#include <stdio.h>
#include <stdlib.h>
struct student
{
    int rollno;
    char name[10];
    float fee;
};
int front = -1;
int rear = -1;
struct student 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 rollno");
        scanf("%d", &qu[rear].rollno);
        printf("\nenter name");
        fflush(stdin);
        gets(qu[rear].name);
        printf("\nenter fee");
        scanf("%f", &qu[rear].fee);
    }
}
void dequeue()
{
    if (front == -1)
        printf("underflow!!!. Queue is empty. No item to delete");
    else
    {
        printf("Deleted item is : %d\t%s\t%f", qu[front].rollno, qu[front].name, qu[front].fee);
        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\t%s\t%f", qu[front].rollno, qu[front].name, qu[front].fee);
    }
}
void display()
{
    if (front == -1)
        printf("Queue is empty. No item to display");
    else
    {
        int i;
        printf("\nInformation of Queue is : \n");
        if (front <= rear)
        {
            for (i = front; i <= rear; i++)
                printf("\n%d\t%s\t%f", qu[i].rollno, qu[i].name, qu[i].fee);
        }
        else
        {
            for (i = front; i < 5; i++)
                printf("\n%d\t%s\t%f", qu[i].rollno, qu[i].name, qu[i].fee);
            for (i = 0; i <= rear; i++)
                printf("\n%d\t%s\t%f", qu[i].rollno, qu[i].name, qu[i].fee);
        }
    }
}
void main()
{
    int ch;
    printf("****************************************\n");
    printf("****************************************\n");
    printf("**       Static Circular Queue        **\n");
    printf("**       Using Array of Structure     **\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 of Structure     **
**   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 rollno1

enter nameSheetal Garg

enter fee1000

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

Enter your choice : 1

enter rollno2

enter nameAmit Goyal

enter fee2000

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

Enter your choice : 1

enter rollno3

enter nameNidhi Gupta

enter fee3000

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

Enter your choice : 1

enter rollno4

enter nameAnsh Garg

enter fee5000

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

Enter your choice : 1

enter rollno5

enter nameSaksham Garg

enter fee2000

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 : 4

Information of Queue is :

1       Sheetal Garg    1000.000000
2       Amit Goyal      2000.000000
3       Nidhi Gupta     3000.000000
4       Ansh Garg       5000.000000
5       Saksham Garg    2000.000000
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

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

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

Enter your choice : 4

Information of Queue is :

2       Amit Goyal      2000.000000
3       Nidhi Gupta     3000.000000
4       Ansh Garg       5000.000000
5       Saksham Garg    2000.000000
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

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

Enter your choice : 4

Information of Queue is :

3       Nidhi Gupta     3000.000000
4       Ansh Garg       5000.000000
5       Saksham Garg    2000.000000
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

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

Enter your choice : 4

Information of Queue is :

4       Ansh Garg       5000.000000
5       Saksham Garg    2000.000000
Options Available are
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

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

Enter your choice : 4

Information of Queue is :

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

Enter your choice : 2
Deleted item is : 5     Saksham Garg    2000.000000
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.