Data Structure Queue Implementation

Queue Implementation program is given below:

//Code for Implementation of Queue
#include <stdio.h>
#include <stdlib.h>
#define SIZE 2
int count=0;
int Q[SIZE];
int FRONT = -1;
int REAR = -1;
void insertq(int data)
{
    count = (REAR-FRONT)+1;
    if (count == SIZE)
    {
        printf("queue is full.");
    }
    else
    {
        if (FRONT == -1)
        {
            FRONT = REAR = 0;
            Q[REAR] = data;
        }
        else
        {
            REAR++;
            Q[REAR] = data;
        }
    }
}
int deleteq()
{
    int data;
    if (FRONT == -1)
    {
        printf("queue is empty");
    }
    else
    {
        if (FRONT == REAR)
        {
            data = Q[FRONT];
            FRONT = REAR = -1;
        }
        else
        {
            data = Q[FRONT];
            FRONT++;
        }
        return data;
    }
}
void main()
{
    int choice, c, data;
    do
    {
        printf("press \n 1. insert\n 2. delete\n 3. display\n 0. exit\n");
        fflush(stdin);
        scanf(" %d", &choice);
        switch (choice)
        {
        case 1:
            printf("Enter the data to insert ");
            scanf(" %d", &data);
            insertq(data);
            break;
        case 2:
            data = deleteq();
            printf("deleted data is %d", data);
            break;
        case 3:
            if (FRONT == -1)
            {
                printf("queue is empty");
            }
            else
            {
                for (int i = FRONT; i <= REAR; i++)
                {
                    printf(" %d ", Q[i]);
                }
            }
            break;
        default:
            exit(1);
        }
        printf(" press 1 to continue  ");
        fflush(stdin);
        scanf(" %d", &c);
    }while(c==1);
}

Output

Enter the size of queue: 3
Enter the data to insert: 22
Enter any number to insert and 0 to exit: 1
Enter the data to insert: 11
Enter any number to insert and 0 to exit: 1
Enter the data to insert: 44
Enter any number to insert and 0 to exit: 0
------MENU-----
1.Insert
2.Delete
3.Display
4.Size of queue
0.EXIT
Enter the choice: 3
The Queue is: 
22 11 44 
------MENU-----
1.Insert
2.Delete
3.Display
4.Size of queue
0.EXIT
Enter the choice: 4
Current size of queue is:3

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin