Answer to Question #272295 in C for Neil

Question #272295

Write a program to implement four numbers of D-queues in an array.


1
Expert's answer
2021-11-28T11:45:13-0500
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();


int Q_arr[MAX];
int end = - 1;
int top = - 1;
int main()
{
    int option;
    while (1)
    {
        printf("\n1.Insert \n");
        printf("2.Delete \n");
        printf("3.Display\n");
        printf("4.Quit \n");
        printf("\nEnter your option: ");
        scanf("%d", &option);
        switch (option)
        {
            case 1:
            insert();
            break;
            case 2:
            delete();
            break;
            case 3:
            display();
            break;
            case 4:
            exit(1);
            default:
            printf("\nWrong option \n");
        } 
    } 


    return 0;


} 
void insert()
{
    int add_item;
    if (end == MAX - 1)
    printf("Queue Overflow \n");
    else
    {
        if (top == - 1)
        top = 0;
        printf("\nInset the element: ");
        scanf("%d", &add_item);
        end = end + 1;
        Q_arr[end] = add_item;
    }
}


void delete()
{
    if (top == - 1 || top > end)
    {
        printf("\nQueue Underflow \n");
        return ;
    }
    else
    {
        printf("\nElement deleted from queue is : %d\n", Q_arr[top]);
        top = top + 1;
    }
} 
void display()
{
    int i;
    if (top == - 1)
        printf("\nQueue is empty \n");
    else
    {
        printf("\nQueue is : ");
        for (i = top; i <= end; i++)
            printf("%d ", Q_arr[i]);
        printf("\n");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS