I don\'t understand what my professor wants. Here is the assignment: Queue wrapp
ID: 3803162 • Letter: I
Question
I don't understand what my professor wants. 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.
What I have so far:
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 Front = Back;
}
int size()
{
return (Back - Front + capacity) % capacity;
}
bool enqueue(const T & x)
{
const int capacity = 100;
if (count != capacity)
{
count++;
Back = (Back + 1) % capacity;
}
}
bool dequeue(const T & x)
{
if (!empty())
{
count--;
Front = (Front + 1) % capacity;
}
}
typedef int QueueElement front() const
{
}
typedef int QueueElement back() const;
{
}
};
Am I on the right track or am I just putting myself in the wrong direction?
Explanation / Answer
You are going good, but not on the right track, and your implementations has few errors
1. bool empty()
{
return Front = Back; // Must use '==' or you could just use return QList.empty();
}
2. int size()
{
return (Back - Front + capacity) % capacity; // This code will be need for arrays of fixed size, not for list implementation of Queue
}
Here, just return the value of count, since you are storing the number of elements in "count". And moreover, "capacity" is not defined anywhere in the code, so it will throw error. Use the following one
int size()
{
return count; // can also use QList.size();
}
The following would have been the valid implementaion if you were using arrays of fixed size. but since you are not using arrays, there is no need of that at all
int size()
{
if(empty()) {
return 0;
}
else
{
retun Front>Back ? (capacity - Front + Rear + 1) : (Rear - Front + 1);
}
}
3. bool enqueue(const T & x)
{
const int capacity = 100;
if (count != capacity)
{
count++;
Back = (Back + 1) % capacity;
}
}
The above code is correct, but it's not an array, so there is no concept of circular queue here and moreover, I don't see where you are inserting the element into the list! Just use the following
bool enqueue(const T & x)
{
list.push_back(x);
rear = rear + 1;
count = count+1;
return true;
}
4. bool dequeue(const T & x)
{
if (!empty())
{
count--;
Front = (Front + 1) % capacity;
}
}
Use the following code instead
bool dequeue(const T & x)
{
if (!empty())
{
QList.pop_front();
count--;
Front = Front + 1;
return true;
}
return false;
}
5.
typedef int QueueElement front() const
{
return QList.front();
//if it were an circular queue using array, you could have used, arr[Front];
}
typedef int QueueElement back() const;
{
return QList.back();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.