if i giv 2 option before 1option in the first running of program, then it shud giv "no elements in queue" but its not giving. rather its showing
"value deleted from the queue = -1
the queue at this moment is as follows-
_"
nothing else. but the program works correctly wen 1 is chosen then 2 option. find out wats wrong, thanx. u better run the program first 2 better understand my program code
// program to display the functions of a queue
#include <iostream.h>
#include <conio.h>
#define max 15
void insert(int queue[],int&rear,int val);
int remove(int queue[], int&front);
void show(int queue[], int front, int rear);
int front=-1,rear=-1;
int queue[max];
void main()
{
int val;
int ch;
menu: // Label
clrscr();
cout<<"\n\n\n1> Insertion\n2> Deletion\n\n\n\t Enter ur choice: ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\n Enter the value 2 be inserted: ";
cin>>val;
insert(queue,rear,val);
show(queue,front,rear);
break;
case 2: val=remove(queue,front);
if(val!=-9999)
cout<<"\n The value deleted from queue = "<<val;
getch();
show(queue,front,rear);
break;
}
char ans;
cout<<"\n\n\t Want 2 continue (Y/N)?";
cin>>ans;
if(ans=='Y' || ans=='y')
goto menu;
else
{
cout<<"\n Thanks User, cya...";
getch();
}
} // End of main()
void insert(int queue[], int&rear,int val)
{
if (rear==max-1)
{
cout<<"\n Queue is full!!!";
getch();
}
else
{
front=0;
rear=rear 1;
queue[rear]=val;
cout<<"\n The value has been inserted at the "<<rear<<"th position";
getch();
}
}
int remove(int queue[],int&front)
{
int val;
if(front==rear 1)
{
val=-9999;
cout<<"\n Queue is empty!!! So no value can be deleted";
getch();
}
else
{
val=queue[front];
front=front 1;
}
return val;
}
void show(int queue[],int front,int rear)
{
if(front==-1)
{
cout<<"\n\n Nothing in the Queue!!!";
getch();
}
else
{
cout<<"\n\n The Queue at this moment is as follows- \n";
for (int i=front;i<=rear;i )
cout<<queue[i]<<"\n";
getch();
}
}
Tags: