I don\'t fully understand what this assignment needs. I\'m basing my code off an
ID: 3803223 • Letter: I
Question
I don't fully understand what this assignment needs. I'm basing my code off another lab we did in class but it used Queue and Arrays so I'm not sure how to properly convert it over. Here is the assignment:
Queue wrapper with list
4. 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.
Here is what I have:
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.fack();
}
};
Main.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<stdlib.h>
#include<process.h>
#include<fstream.h>
#include<cstdlib.h>
#define default_value 16
using namespace std;
template< class T >
class Queueee
{
public:
Queueee(int = default_val);//default constructor
~Queue()//destructor
{
del [] val;
}
bool enQueueee( T );
T deQueue();
bool isNull();
bool isFull();
private:
int size;
T *values;
int front;
int back;
};
template< class T > Queueee<T>::Queueee(int x):
size(x),//ctor
values(new T[size]),
front(0),
back(0)
{
template< class T > bool Queue<T>::isFull()
{
if((back + 1) % size == front )
return 1;
else
return 0;
}
template< class T > bool Queue<T>::enQueue(T x)
{
bool b = 0;
if(!Queue<T>::isFull())
{
values[back] = x;
back = (back + 1) % size;
b = 1;
}
return b;
}
template< class T > bool Queuee<T>::isEmpty()
{
if( back == front )//is empty
return 1;
else
return 0; //is not empty
}
template< class T > T Queue<T>::deQueue()
{
T val = -1;
if(!Queue<T>::isEmpty())
{
val = values[front];
front = ( front + 1 ) % size;
}
else
{
cerr << "Queue is Empty : ";
}
return val;
}
int main()
{
if(qu1.enQueue(i*x))
cout << endl << i*x;
else
cout << " Queue (qu1) is full: ";
}
cout << " DeQueued values from qu1 : ";
for(int j = 1 ; j < 10 ; j++)
cout << qu1.deQueue() << endl;
cout << endl << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.