Question


To save space, we use a circle array to represent a queue. How we decide a queue is empty or full. Set the size of the circle To save space, we use a circle array to represent a queue. How we decide a queue is empty or full. Set the size of the circle array is N (thr circle array has N positions)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer...

Full:

if((myBack==N-1 && myFront==0) || myFront==myBack+1){
       printf("Queue is full\n");
}

Empty:

if(myBack==-1){
       printf("Queue is empty\n");
   }

CODE:

#include <stdio.h>
#define N 5
int que[N],myFront=-1,myBack=-1;
void insert(){
   if((myBack==N-1 && myFront==0)||myFront==myBack+1){
       printf("Queue is full\n");
   }else if(myFront==-1&&myBack==-1){
       printf("Enter Data:");
       scanf("%d",&que[++myBack]);
       myFront++;
   }
   else if(myBack==N-1 && myFront!=0){
       myBack=0;
       printf("Enter data:");
       scanf("%d",&que[myBack]);
   }else{
       printf("Enter data:");
       scanf("%d",&que[++myBack]);
   }
}
void delete(){
   if(myBack==-1){
       printf("Queue is empty\n");
   }else{
       if(myFront==myBack){
           printf("%d is removed\n",que[myBack]);
           myBack=-1;
           myFront=-1;
       }else if(myFront==N-1){
           printf("%d is removed\n",que[myFront]);
           myFront=0;
       }else{
           printf("%d is removed\n",que[myFront++]);
       }
   }
}
void display(){
   int i;
   if(myFront==-1){
       printf("Queue is empty\n");
   }
   else if(myFront<=myBack){
       for(i=myFront;i<=myBack;i++){
           printf("%d\t",que[i]);
       }
       printf("\n");
   }else{
       for(i=myFront;i<=myBack;i++){
           printf("%d\t",que[i]);
       }
       for(i=0;i<=myBack;i++){
           printf("%d\t",que[i]);
       }
       printf("\n");
   }
}
main(){
   int ch;
   do{
       printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
       printf("Enter Choice:");
       scanf("%d",&ch);
       switch(ch){
           case 1:
               insert();
               break;
           case 2:
               delete();
               break;
           case 3:
               display();
               break;
           case 4:
               break;
           default:
               printf("You have entered wrong option\n");
               break;
       }
   }while(ch!=4);
}

OUTPUT:

C:\Users\sai\Desktop\circuler_queue.exe 1. Insert 2.Delete 3.Display 4.Exit Enter Choice:1 Enter Data:70 1.Insert 2.Delete 3.50 90 70 80 1.Insert 2.Delete 3.Display 4.Exit Enter Choice:1 Enter data: 100 1.Insert 2.Delete 3.Display 4. Exit Enter Choic

if you have any doubts please COMMENT...

If you understand the answer please give THUMBS UP....

Add a comment
Know the answer?
Add Answer to:
To save space, we use a circle array to represent a queue. How we decide a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Build and use your own minimal queue class using an array of type String of fixed...

    Build and use your own minimal queue class using an array of type String of fixed size to hold the queue. The array should have the default size of 5. The array size should be setable via a constructor. The following methods must be implemented: enqueue – inserts a value at the rear of the queue dequeue – returns the value at the front of the queue, removing the value from the queue isEmpty – returns true if the queue...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...

    QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueueBox: public class QueueBox<E> { private E[] elements = (ED))( new Object[5]); private int front_idx = 0; private int rear_idx = 0; private int count = 0; Hint: use the count variable to keep track of how many elements are in the queue (increment count when enqueing and decrement when dequeing). Makes it a lot easier to determine if the...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT