#include<iostream>
using namespace std;
const int maxqueue=10;
typedef int queue_entry;
enum Error_code{success,overflow,underflow};
class queue
{
public:
queue(void);
Error_code serve();
Error_code append(const queue_entry &item);
Error_code retrieve(queue_entry &item)const;
int size()const;
void clear();
protected:
int count;
int front,rear;
queue_entry entry[maxqueue];
};
queue::queue(void)
{
count = 0;
rear = maxqueue-1;
front=0;
}
Error_code queue::append(const queue_entry &item){
if(count>=maxqueue)
return overflow;
count++;
rear=((rear+1)==maxqueue)?0:(rear+1);
entry[rear]=item;
return success;}
Error_code queue::serve(){
if(count<=0)
return underflow;
count--;
front=((front+1)==maxqueue)?0:(front+1);
return success;}
Error_code queue::retrieve(queue_entry &item)const{
if(count<=0)
return underflow;
item=entry[front];
return success;}
int queue::size()const{
return count;}
void queue::clear(){
count=0;
rear=front;
}
void help();
void introduction();
char get_command();
bool do_command(char c,queue &test_queue);
int main(){
queue test_queue;
introduction();
while(do_command(get_command(),test_queue));}
void help(){
cout<<endl
<<"This program allows the user to enter one command"<<endl
<<"(but only one)on each input line."<<endl
<<"For example,if the command S is entered,then"<<endl
<<"the program will serve the front of the queue"<<endl
<<endl
<<"The valid commands are:"<<endl
<<"A - Append the next input character to the extended queue"<<endl
<<"S - Serve the front of the extended queue"<<endl
<<"R - Retrieve and print the front entry"<<endl
<<"# - The current size of the extended queue"<<endl
<<"C - Clear the extended queue (same as delete)"<<endl
<<"P - Print the extended queue"<<endl
<<"H - This help screen"<<endl
<<"Q - Quit"<<endl
<<"Press <Enter> to continue"<<flush;
char c;
do{
cin.get(c);
}while (c!='\n');}
void introduction(){
cout<<"press <H> for help"<<endl;
}
char get_command(){
char c;
cout<<"select command and press <enter>";
cin>>c;
if(c=='A'||c=='S'||c=='R'||c=='#'||c=='C'||c=='P'||c=='H'||c=='Q')
return c;
else
cout<<"please enter a valid command or <H> for help"<<endl;
}
bool do_command(char c,queue &test_queue){
bool continue_input=true;
queue_entry x;
switch(c){
case'A':
cout<<"enter new data to insert";
cin>>x;
if(test_queue.append(x)==overflow)
cout<<"queue is full"<<endl;
break;
case'S':
if(test_queue.serve()==underflow)
cout<<"queue is empty"<<endl;
break;
case'R':
if(test_queue.retrieve(x)==underflow)
cout<<"queue is empty"<<endl;
else
cout<<endl
<<"the first entry is:"<<x
<<endl;
break;
case'#':
test_queue.size();
break;
case'C':
test_queue.clear();
break;
case'P':
break;
case'H':
help();
break;
case'Q':
cout<<"extended queue demonstration finished."<<endl;
continue_input=false;
break;
}
return continue_input;
}