Answer to Question #194506 in C++ for nasyendra rau

Question #194506

The following program shows queue implementation using Array. In the main( ) function, the queue is inserted 5 times, i.e. up to its maximum capacity. However, though the deQ( ) function has been executed, the program fails to insert a new element (value=21) into the queue. Modify the program so the queue can be filled up to its maximum capacity at any time. 



1
Expert's answer
2021-05-17T13:29:36-0400
#include <stdio.h> 
//declare our global variables 
#define MAX 5 
int qArray[MAX]; 
int head = 0; 
int tail = -1; 
//function to put new element into Q 
void enQ(int data) { 
	
	if(tail==MAX-1) 
		printf("The Q is full\n\n");
	else { 
		tail++;
		qArray[tail]=data; 
	} 
} 
//function to remove element from the Q 
int deQ() { 
	//declare our local variables 
	int temp; 
	if(tail==-1)
		printf("The Q is empty\n\n"); 
	else { 
		temp=qArray[head];
		for (int i = 0; i < tail - 1; i++){
              qArray[i] = qArray[i + 1];
        }
		tail--;
		return temp; 
	} 
} 
int main() { 
	//insert element into a;
	enQ(34);
	enQ(25); 
	enQ(99); 
	enQ(32); 
	enQ(45); 
	deQ(); 
	enQ(21); 


	getchar();
	getchar();
	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