How to Implement queue data Structure class in C++ Programming language with Source code ?
# include <iostream.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
# include <iomanip.h>
const int MAX=5;
class queue
{
private:
int que[MAX];
int head ;
int tail ;
int count ;
public:
queue():head(0),tail(0),count(0) //constructor
{
}
void put()
{
if (count==MAX)
{
cout << "\n ERROR: Queue full" ;
}
if (tail==MAX)
{
cout << "\n Returning to first element of an array: ";
tail=0;
}
cout << "\n Enter Data: " ;
cin >> que[tail++] ;
cout << "Tail is: " << tail ;
count++;
}
void get()
{
if (count==0)
{
cout << "\n Queue Empty... " ;
}
else
{
cout << "\n Required data item: " ;
cout << que[head++] ;
cout << "\n Count is: " << count ;
cout << "\n Head is : " << head ;
count--;
if(head==MAX)
{
cout << "Retreiving to first member of queue... ";
head=0;
}
}
}
};
void main()
{
queue line ;
char choice='x';
while (choice!='q' && choice!='Q')
{
cout << "\n (q) to quit";
cout << "\n (p) to put" ;
cout << "\n (g) to get" ;
cin >> choice ;
switch(choice)
{
case 'p':
case 'P':
line.put();
break;
case 'q':
case 'Q':
cout << "Exiting...." ;
break;
case 'g':
case 'G':
line.get();
break;
}
}
getch();
}
