This program is not giving “Queue Overflow” error message when its full. Why is that so? Is there something wrong with the qfull function? I even tried if(qfull(*r)==1). Even then its not working. It just keeps going on accepting numbers even when its full.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define QUEUE_SIZE 5
void insert_rear(int,int*,int*);
void delete_front(int*,int*,int*);
void display(int*,int,int);
int qempty(int,int);
int qfull(int);
void main()
{
int f=0,r=-1,q[10],item,choice;
for(;;)
{
clrscr();
printf("\t\t\t Ordinary Queue Operation\n\n");
printf("\t\t\t 1. Push\n");
printf("\t\t\t 2. Pop\n");
printf("\t\t\t 3. Display\n");
printf("\t\t\t 4. Exit\n\n");
printf("\t\t\t Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the item to be inserted: ");
scanf("%d",&item);
insert_rear(item,q,&r);
continue;
case 2:
delete_front(q,&f,&r);
break;
case 3:
display(q,f,r);
break;
case 4:
exit(0);
default:
printf("\t\t\t Invalid Input - Try Again");
}
getch();
}
}
void insert_rear(int item,int q[],int *r)
{
if(qfull(*r)==1)
{
printf("\t\t\t Queue Overflow\n");
return;
}
q[++(*r)]=item;
}
void delete_front(int q[],int *f,int *r)
{
if(qempty(*f,*r))
{
printf("\t\t\t Queue Underflow\n");
return;
}
printf("Pop successfull, item deleted = %d",q[(*f)++]);
if(*f>*r)
{
*f=0,*r=-1;
}
}
void display(int q[],int f,int r)
{
int i;
if(qempty(f,r))
{
printf("Queue is empty\n");
return;
}
printf("\t\t\t Queue Container\n\n");
for(i=f;i<=r;i++)
printf("\t\t\t |%5d|\n",q[i]);
}
int qempty(int f,int r)
{
return(f>r)?1:0;
}
int qfull(int r)
{
return(r==QUEUE_SIZE-1)?1:0;
}
The problem is that the call to
clrscris clearing the error message from the screen so you are just not seeing the message. If you remove theclrscrI am sure you will see the message before the new menu is drawn to the screen.You need to set an error flag indicating the problem and then display the error message when you display the menu.