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.
#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;
}
Comments
Leave a comment