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)
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:


if you have any doubts please COMMENT...
If you understand the answer please give THUMBS UP....
To save space, we use a circle array to represent a queue. How we decide a...
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 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 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 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]; //...
Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...
// =================== 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 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...
Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
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 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...