Hello , I'm suppposed to make three queues and then insert,delete and display items in them according to a given priority
So, if the user enters priority as 1/2/3 then I'm supposed to insert/delete/display items from it .
This is what i got ..
But the problem is say when the priority is 1 and when I try to insert an item, r1(the rear position for queue 1) is not incremented at all after being passed through the insert() function. Why is this happening?Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define Q_SIZE 5
/*function declarations*/
int underflow(int , int );
int overflow(int );
void insert( int a[], int r );
void deleted(int a[], int f,int r);
void display(int a[],int f,int r);
/*variable declarations*/
int q1[Q_SIZE],q2[Q_SIZE],q3[Q_SIZE];
int f1=0,r1=-1,f2=0,r2=-1,f3=0,r3=-1;
void main()
{
clrscr();
int choice;
int prio;
for(;;)
{
printf("Que no.\n");
scanf("%d",&prio);
printf("ENTER 1 FOR INSERTION 2 FOR DELETION");
printf("ENTER 3 FOR DISPLAY 4 FOR EXIT\n");
printf("ENTER YOUR CHOICE\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(prio==1)
{
insert(q1, r1);
break;
}
if(prio==2)
{
insert( q2, r2 ) ;
break;
}
if(prio==3)
{
insert( q3, r3 );
break;
}
case 2:
if(prio==1)
{
deleted( q1, f1,r1 );
break;
}
if(prio==2)
{
deleted( q2, f2,r1 ) ;
break;
}
if(prio==3)
{
deleted( q3, f3,r1 );
break;
}
case 3:
if(prio==1)
{
display( q1,f1, r1 );
break;
}
if(prio==2)
{
display( q2,f2,r2 );
break;
}
if(prio==3)
{
display(q3,f3,r3);
break;
}
default:
exit(0);
}
}
}
/* overflow*/
int overflow(int r)
{
if(r>Q_SIZE-1)
{
return 1;
}
else
{
return 0;
}
}
/*underflow*/
int underflow(int f,int r)
{
if(f>r)
{
return 1;
}
else
{
return 0;
}
}
/*insert*/
void insert(int q[],int r)
{
if(overflow(r)==1)
{
printf("overflow reached");
return;
}
else
{
int item;
printf("enter the item to be inserted");
scanf("%d",&item);
q[++r]=item;
printf("%d",r1);/*confusion why is r1 =-1*/
}
}
/* delete*/
void deleted(int q[] ,int f, int r)
{
int item;
if(underflow(f,r)==1)
{
printf("underflow reached");
return;
}
else
{
item=(q[f++]);
printf("deleted item is ..%d\n",item);
}
}
/* display*/
void display(int a[], int f ,int r)
{
if(underflow(f,r)==1)
{
printf("underflow nothing to display\n");
return;
}
else
{
printf("elments in queue are \n");
for(int i=f;i<=r;i++)
printf("%d\n",a[i]);
}
}