In: Computer Science
Redo Program 1 so that you have a template class, that works with a char, string, int, or double data type.
Program 1 is below:
#include <iostream>
#include <string>
#define CAPACITY 12
using namespace std;
class queue {
private:
string strArray[CAPACITY];
int frontIndex, endIndex;
int queueSize;
public:
queue()
{
frontIndex = endIndex = -1;
queueSize = 0;
}
void enqueue(string);
string dequeue();
int size()
{
return queueSize;
}
int front()
{
return frontIndex;
}
int end()
{
return endIndex;
}
};
void queue::enqueue(string data)
{
if (endIndex == -1) {
frontIndex = endIndex = 0;
strArray[frontIndex] = data;
queueSize++;
}
else {
int pos = (endIndex + 1) % CAPACITY;
if (pos == frontIndex) {
cout << "OVERFLOW" << endl;
return;
}
else {
endIndex = pos;
strArray[pos] = data;
queueSize++;
}
}
}
string queue::dequeue()
{
if (frontIndex == -1) {
cout << "UNDERFLOW" << endl;
return "UNDERFLOW";
}
else {
if (frontIndex == endIndex) {
string element = strArray[frontIndex];
frontIndex = endIndex = -1;
queueSize = 0;
return element;
}
else {
string element = strArray[frontIndex];
frontIndex = (frontIndex + 1) % CAPACITY;
queueSize--;
return element;
}
}
}
int main()
{
cout << "Cathleen Espinoza - 10/1/20" << endl;
queue myQ;
cout << myQ.size() << endl; \
myQ.dequeue();
myQ.enqueue("Fred");
myQ.enqueue("Liv");
myQ.enqueue("Julie");
myQ.enqueue("Rich");
myQ.enqueue("William");
myQ.enqueue("Olo");
myQ.enqueue("Xi");
myQ.enqueue("Chu");
myQ.enqueue("Annie");
myQ.enqueue("Carlos");
myQ.enqueue("Tuyet");
myQ.enqueue("Sue");
myQ.enqueue("Penny");
cout << myQ.front() << endl;
cout << myQ.end() << endl;
cout << myQ.size() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
myQ.enqueue("Olive");
myQ.enqueue("Jim");
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.front() << endl;
cout << myQ.end() << endl;
cout << myQ.size() << endl;
system("pause");
return 0;
}
Code After Modification:
#include <iostream>
#include <string>
#define CAPACITY 12
using namespace std;
template<class Type>
class queue {
private:
Type array[CAPACITY];
int frontIndex, endIndex;
int queueSize;
public:
queue()
{
frontIndex = endIndex = -1;
queueSize = 0;
}
void enqueue(Type);
Type dequeue();
int size()
{
return queueSize;
}
int front()
{
return frontIndex;
}
int end()
{
return endIndex;
}
};
template<class Type>
void queue<Type>::enqueue(Type data)
{
if (endIndex == -1) {
frontIndex = endIndex = 0;
array[frontIndex] = data;
queueSize++;
}
else {
int pos = (endIndex + 1) % CAPACITY;
if (pos == frontIndex) {
cout << "OVERFLOW" << endl;
return;
}
else {
endIndex = pos;
array[pos] = data;
queueSize++;
}
}
}
template<class Type>
Type queue<Type>::dequeue()
{
if (frontIndex == -1) {
Type element;
cout << "UNDERFLOW" << endl;
return element;
}
else {
if (frontIndex == endIndex) {
Type element = array[frontIndex];
frontIndex = endIndex = -1;
queueSize = 0;
return element;
}
else {
Type element = array[frontIndex];
frontIndex = (frontIndex + 1) % CAPACITY;
queueSize--;
return element;
}
}
}
int main()
{
cout << "Cathleen Espinoza - 10/1/20" << endl;
//create queue to hold type string
queue<string> myQ;
cout << myQ.size() << endl;
myQ.dequeue();
myQ.enqueue("Fred");
myQ.enqueue("Liv");
myQ.enqueue("Julie");
myQ.enqueue("Rich");
myQ.enqueue("William");
myQ.enqueue("Olo");
myQ.enqueue("Xi");
myQ.enqueue("Chu");
myQ.enqueue("Annie");
myQ.enqueue("Carlos");
myQ.enqueue("Tuyet");
myQ.enqueue("Sue");
myQ.enqueue("Penny");
cout << myQ.front() << endl;
cout << myQ.end() << endl;
cout << myQ.size() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
myQ.enqueue("Olive");
myQ.enqueue("Jim");
cout << myQ.dequeue() << endl;
cout << myQ.dequeue() << endl;
cout << myQ.front() << endl;
cout << myQ.end() << endl;
cout << myQ.size() << endl;
system("pause");
return 0;
}
Output Of Code:
Cathleen Espinoza - 10/1/20
0
UNDERFLOW
OVERFLOW
0
11
12
Fred
Liv
Julie
Rich
William
5
1
9
sh: 1: pause: not found
Images Of Code:
Image Of Output: