Hey,
Im having a play around with making queues I have a problem though. While my code appears to successfully add elements but when I run the display function I keep receiving "The queue is empty ". Some problem with my for loop I guess any ideas?
#include <iostream>
#include "conio.h"
#include <stdlib.h>
usingnamespace std;
class queue
{
int queuel[500],rear,front;
public:
queue()
{
front = rear = -1;
}
void insert(int x)
{
if(rear>500)
{
cout <<"The Que is FULL";//arry full
front = rear = -1;// when full and empty value is -1
}
queuel[1+(-1)];// iff not empty icremint rear value by 1 value stored in 0 position of array.
cout<< "sucessfully inserted :" <<x;
}
void del()
{
if (front==rear) //if its empty value cannot be deleted.
{
cout<< "The queue is empty";
return;
}
cout<<"Sucessfully deleted: "<<queuel[++front]; //incriment front as value has been removed
}
void display()
{
if(front==rear)
{
cout<< "The queue is empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout<<queuel[i]<<"";//displays all elements
}
};
int main ()
{
int ch;
queue qu;
while(1)
{
cout<<"\n press 1 to INSERT \n Press 2 to DELETE \ press 3 to DISPLAY \n Press 4 to EXIT";
cin>>ch;
switch(ch)//value given by user is worked on
{
case 1:cout<<"Enter the Element to INSERT:";
cin>>ch;
qu.insert (ch);
break;
case 2:qu.del();
break;
case 3:qu.display();
break;
case 4:exit(0);
break;
}
}
_getch();
return (0);
}