C Program to implement Queue Operations using Array

Queue Operations using Array
     This is one of the important program in data structure, here we perform Queue operations like Insert, Display and Delete. below we have given program with comments at every important statements by which you can understand the working of program.

Also Read: Stack Operations Using Array

Program:

#include <stdio.h>
//Queue Operations using Array by Slashmycode.com
#define MAX 50
int queue_array[MAX];
int rear = - 1; //initial position of rear when Queue is empty
int front = - 1; //initial position of front when Queue is empty
main()
{
    int choice;
    printf("1.Insert element to queue \n");
        printf("2.Delete element from queue \n");
        printf("3.Display all elements of queue \n");
        printf("4.Quit \n");
    while (1)
    {

        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
            insert();  //function call
            break;
            case 2:
            delete();
            break;
            case 3:
            display();
            break;
            case 4:
            exit(1);
            default:
            printf("Wrong choice \n");
        } /*End of switch*/
    } /*End of while*/
} /*End of main()*/
insert()
{
    int add_item;
    if (rear == MAX - 1) //Condition to check array is full or not
    printf("Queue Overflow \n");
    else
    {
        if (front == - 1)
        /*If queue is initially empty */
        front = 0;
        printf("Inset the element in queue : ");
        scanf("%d", &add_item);
        rear = rear + 1;
        queue_array[rear] = add_item;
    }
} /*End of insert()*/

delete()
{
    if (front == - 1 || front > rear)
    {
        printf("Queue Underflow \n");
        return ;
    }
    else
    {
        printf("Element deleted from queue is : %d\n", queue_array[front]);
        front = front + 1;
    }
} /*End of delete() */
display()
{
    int i;
    if (front == - 1)
        printf("Queue is empty \n");
    else
    {
        printf("Queue is : \n");
        for (i = front; i <= rear; i++)
            printf("%d ", queue_array[i]);
        printf("\n");
    }//Queue Operations using Array by Slashmycode.com

} /*End of display() */


Output:

Here is the output where we have performed all Queue operations using array in C
output of Queue Operations using Array





No comments:

Post a Comment

Feel free to ask us any question regarding this post