#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
<<"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;
}