Answer to Question #248039 in C++ for Myname

Question #248039
Write a program to implement circular queue using Arrays.
1
Expert's answer
2021-10-07T14:00:36-0400
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int circular_queue[10];
int frontQueue = -1, back = -1, n=10;
void enqueue(int item){
    if ((frontQueue == 0 && back == n-1) || (frontQueue == back+1)) {
      printf("The size of the queue exceeded \n");
      return;
   }
   if (frontQueue == -1) {
      frontQueue = 0;
      back = 0;
   }
   else {
      if (back == n - 1)
         back = 0;
      else
         back = back + 1;
   }
   circular_queue[back] = item ;
}
void dequeue(){
    if (frontQueue == -1) {
       printf("Queue Underflow\n");
      return ;
   }
    printf("Element deleted is : %d ", circular_queue[frontQueue]);
   if (frontQueue == back) {
      frontQueue = -1;
      back = -1;
   }
   else {
      if (frontQueue == n - 1)
         frontQueue = 0;
      else
         frontQueue = frontQueue + 1;
   }
}
 
void display(){
   int f = frontQueue, r = back;
   if (frontQueue == -1) {
       printf("No item in the queue");
      return;
   }
   printf("Queue Contents :\n");
   if (f <= r) {
      while (f <= r){
          printf("%d", circular_queue[f]);
         f++;
      }
   }
   else {
      while (f <= n - 1) {
          printf("%d",circular_queue[f]);
         f++;
      }
      f = 0;
      while (f <= r) {
          printf("%d",circular_queue[f]);
         f++;
      }
   }
   
}  
   
int menu(){
    int choice;
     printf("\n Select an option: \t");
    printf("\n 1.Add an item to the queue");
    printf("\n 2. Delete an item from the queue");
    printf("\n 3. Display the queue");
    printf("\n 4. exit\n");
   
    scanf("%d",&choice);
    return(choice);
}
int main(){
    int value;
    while(1){
        switch(menu()){
            case 1:
           printf("Addition of an item to the queue selected: ");
            scanf("%d",&value);
                enqueue(value);
                break;
            case 2:
                dequeue();
                break;
            case 3:
                display();
                break;
            case 4:
                exit(0);
            default:
                printf("Wrong option");
        }
    }
    getch();
    return 0;
}

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