I\'m not sure I am on the right track with this assignment. Assignent: Write a t
ID: 3804137 • Letter: I
Question
I'm not sure I am on the right track with this assignment.
Assignent:
Write a template queue class as defined below:
private data member: a STL list
public member functions:
-empty
-size
-enqueue
-deque
-front
-back
Then write a driver to test the above queue class.
My code:
Header.h
#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;
template <class T>
class Queue
{
private:
int Front, Back, count;
list<T> QList;
public:
Queue()
{
Front = 0;
Back = 0;
count = 0;
}
bool empty()
{
return QList.empty();
}
int size()
{
QList.size();
}
bool enqueue(const T & x)
{
list.push_back(x);
rear = rear + 1;
count = count + 1;
return true;
}
bool dequeue(const T & x)
{
if (!empty())
{
QList.pop_front();
count--;
Front = Front + 1;
return true;
}
return false;
}
typedef int QueueElement front() const
{
return QList.front();
}
typedef int QueueElement back() const
{
return QList.back();
}
};
Source.cpp
#include "Header.h"
#include <iostream>
#include <cctype>
using namespace std;
template<class T>
void ShowCommands()
{
cout << "Use the following commands to test the Queue class:" << endl;
cout << "a---add an element to the queue" << endl;
cout << "d---display contents of queue" << endl;
cout << "e---test whether a queue is empty" << endl;
cout << "f---retrieve the item at the front of the queue" << endl;
cout << "r---remove item from front of the queue" << endl;
cout << "s---display the size of queue" << endl;
cout << "b---retrieve the item at the back of the queue" << endl;
cout << "q---quit testing" << endl;
}
template<class T>
int main()
{
typedef int QueueElement item;
char command;
list<T> q;
ShowCommands();
do
{
cout << "Command? ";
cin >> command;
if (isupper(command)) command = tolower(command);
switch (command)
{
case 'a':
cout << "Enter item to add to queue: ";
cin >> item;
q.enqueue(item);
cout << "--> " << item << " added ";
break;
case 'd':
cout << "--> Queue contents: " << q << endl;
break;
case 'e':
cout << "--> Queue " << (q.empty() ? "is" : "is not")
<< " empty ";
break;
case 'f':
cout << "--> " << q.front() << " is at the front ";
break;
case 'r':
q.dequeue();
cout << "--> Front element removed ";
break;
case 's':
cout << "--> the size of the queue is: " << q.size() << endl;
break;
case 'b':
cout << "--> " << q.back() << " is at the back ";
break;
case 'q':
cout << "--> End of test ";
break;
default:
cout << "Illegal command: " << command << endl;
}
} while (command != 'q');
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;
template <class T>
class Queue
{
private:
int Front, Back, count;
list<T> QList;
public:
Queue()
{
Front = 0;
Back = 0;
count = 0;
}
bool empty()
{
return QList.empty();
}
int size()
{
QList.size();
}
bool enqueue(const T & x)
{
list.push_back(x);
rear = rear + 1;
count = count + 1;
return true;
}
bool dequeue(const T & x)
{
if (!empty())
{
QList.pop_front();
count--;
Front = Front + 1;
return true;
}
return false;
}
typedef int QueueElement front() const
{
return QList.front();
}
typedef int QueueElement back() const
{
return QList.back();
}
};
Source.cpp
#include "Header.h"
#include <iostream>
#include <cctype>
using namespace std;
template<class T>
void ShowCommands()
{
cout << "Use the following commands to test the Queue class:" << endl;
cout << "a---add an element to the queue" << endl;
cout << "d---display contents of queue" << endl;
cout << "e---test whether a queue is empty" << endl;
cout << "f---retrieve the item at the front of the queue" << endl;
cout << "r---remove item from front of the queue" << endl;
cout << "s---display the size of queue" << endl;
cout << "b---retrieve the item at the back of the queue" << endl;
cout << "q---quit testing" << endl;
}
template<class T>
int main()
{
typedef int QueueElement item;
char command;
list<T> q;
ShowCommands();
do
{
cout << "Command? ";
cin >> command;
if (isupper(command)) command = tolower(command);
switch (command)
{
case 'a':
cout << "Enter item to add to queue: ";
cin >> item;
q.enqueue(item);
cout << "--> " << item << " added ";
break;
case 'd':
cout << "--> Queue contents: " << q << endl;
break;
case 'e':
cout << "--> Queue " << (q.empty() ? "is" : "is not")
<< " empty ";
break;
case 'f':
cout << "--> " << q.front() << " is at the front ";
break;
case 'r':
q.dequeue();
cout << "--> Front element removed ";
break;
case 's':
cout << "--> the size of the queue is: " << q.size() << endl;
break;
case 'b':
cout << "--> " << q.back() << " is at the back ";
break;
case 'q':
cout << "--> End of test ";
break;
default:
cout << "Illegal command: " << command << endl;
}
} while (command != 'q');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.