Code:
#include <iostream.h>
void enqueue (char queue[maxqueue], int& front, int& rear, char item)
{
rear=(rear+1)%maxqueue;
if(front!=rear)
{
queue[rear]=item;
}
else
{
cout<<"OVERFLOW\n";
rear=rear-1;
if (rear<0)
rear=rear+maxqueue;
};
};
void dequeue (char queue[maxqueue], int& front, int& rear, char& item)
{
if (front!=rear)
{
front=(front+1)%maxqueue;
item=queue[front];
//queue[front]=' ';
}
else
{
cout<<"UNDERFLOW--FILLERUP\n";
item='0';
};
};
//lists items on a sequential queue
void list (char queue[maxqueue], int front, int rear)
{
int count=0,where;
//from front to rear and cout
where=front;
while (where!=rear)
{
where=(where+1)%maxqueue;
cout<<queue[where];
count++;
};
/*//rearplus=(rear+1)%maxqueue;
for (where=(front+1)%maxqueue;where!=rearplus;where=(where+1)%maxqueue)
{
count++;
cout<<queue[where];
}; */
cout<<endl<<count<<" items on queue"<<endl;
};
//menu for queue routines
void queuedriver(char queue[maxqueue], int& front, int& rear);
void main ()
{
char answer,table[maxqueue];
int front,rear;
cout<<"We will maintain a queue for you.\n";
cout<<"At your instruction, we will enqueue, dequeue, list, or halt.\n";
do//The whole program as often as needed
{
//initialize counter for task
front=0;
rear=0;
//basic menu driver
queuedriver(table,front,rear);
//allows repeat of whole program
cout<<"Would you like to do another queue simulation('Y' to continue)?\n";
cin>>answer;
}while ((answer=='Y') || (answer=='y'));
};
//menu for queue routines
void queuedriver(char queue[maxqueue], int& front, int& rear)
{
const int maxcommand=4;
char command[maxcommand], thing;
cout<<"the options are as follows:\n";
cout<<"1. enqueue\n";
cout<<"2. dequeue \n";
cout<<"3. list\n";
cout<<"4. halt\n";
do//queue menu options
{
cout<<"type command:(en,de,li,ha)\n";
cin>>command;
if ((strcmp(command,"enqu")==0) || (strcmp(command,"en")==0) ||
(strcmp(command,"1")==0) || (strcmp(command,"e")==0) )
{
cout<<"item to put on queue:";
cin>>thing;
enqueue(queue,front,rear, thing);
cout<<thing<<" enqueued on queue \n";
}
else if ((strcmp(command,"dequeue")==0) || (strcmp(command,"de")==0) ||
(strcmp(command,"2")==0) || (strcmp(command,"d")==0) )
{
dequeue(queue,front,rear,thing);
cout<<thing<<" dequeued from queue\n"<<endl;
}
else if ((strcmp(command,"list")==0) || (strcmp(command,"li")==0) ||
(strcmp(command,"3")==0) || (strcmp(command,"l")==0) )
{
list(queue,front,rear);
}
else if ((strcmp(command,"halt")==0) || (strcmp(command,"ha")==0) ||
(strcmp(command,"4")==0) || (strcmp(command,"h")==0) )
{
cout<<"goodbye\n";
strcpy(command,"h");
}
else
cout<<"unrecognized command\n";
} while (strcmp(command,"h")!=0);
};