// ... something ...
//6 Separate implementation of class queueArr from main.cpp
// You do know the #include and what it does?
//7 create function prototype for display all
void displayall( /*???*/ );
int main(){
//1 create an array of students with the names of your favorite classmates
std::string students [5] = {"Christian", "Thirdy", "MJ", "Maverick", "Niemo"};
//2 create a queue
queueArr<std::string> stud( 42 );
//3 loop structure to enqueue all students
// enqueue from students into stud
for ( auto s : students ) {
// something
}
//4 show front
//5 show end
// You have member functions for these
//9 call display all
displayall( stud )
}
//8 define display all
void displayall( /*???*/ )
{
// whatever it takes
}
If you have to implement your own queue - rather than using std::queue - why not base it around a std::list? It removes all the memory handling/array stuff - unless that was a requirement of the exercise?
enqueue/dequeue are over-complicated. Also there is a memory issue as allocated memory isn't deleted in the class destructor and a copy-constructor and copy assignment hasn't been provided. displayFront() and displayBack() also need to consider if the stack is empty. Consider:
#include<iostream>
template <typename T>
class queueArr {
private:
T* q_array {};
size_t q_capacity {}, q_size {}, q_front {}, q_back {};
public:
queueArr(size_t newCapacity);
bool isEmpty() const;
bool isFull() const;
size_t qSize() const;
void displayFront() const;
void displayBack() const;
void enqueue(T newData);
void dequeue();
void clear();
void displayAll() const;
~queueArr() {
delete[] q_array;
}
// These need to be provided if needed
queueArr(const queueArr&) = delete;
queueArr& operator=(const queueArr&) = delete;
};
template <typename T>
queueArr<T>::queueArr(size_t newCapacity) : q_capacity { newCapacity }, q_array { new T[newCapacity] } {}
template <typename T>
bool queueArr<T>::isEmpty() const {
return q_size == 0;
}
template <typename T>
bool queueArr<T>::isFull() const {
return q_size == q_capacity;
}
template <typename T>
size_t queueArr<T>::qSize() const {
return q_size;
}
template <typename T>
void queueArr<T>::displayFront() const {
if (isEmpty())
std::cout << "Queue is empty\n";
else
std::cout << "Front: " << q_array[q_front] << '\n';
}
template <typename T>
void queueArr<T>::displayBack() const {
if (isEmpty())
std::cout << "Queue is empty\n";
else
std::cout << "Back: " << q_array[q_back ? (q_back - 1): q_capacity - 1] << '\n';
}
template <typename T>
void queueArr<T>::clear() {
q_size = 0;
q_front = 0;
q_back = 0;
}
template <typename T>
void queueArr<T>::enqueue(T newData) {
if (isFull())
std::cout << "Queue is Full.\n";
else {
q_array[q_back] = newData;
q_back = (q_back + 1) % q_capacity;
++q_size;
std::cout << newData << " was successfully enqueued.\n";
}
}
template<typename T>
void queueArr<T>::dequeue() {
if (isEmpty())
std::cout << "Queue is Empty.\n";
else {
constauto temp { q_array[q_front] };
q_front = (q_front + 1) % q_capacity;
--q_size;
std::cout << temp << " was successfully dequeued.\n";
}
}
template<typename T>
void queueArr<T>::displayAll() const {
//std::cout << "front: " << q_front << " back: " << q_back << '\n';
for (size_t i { q_front }, c {}; c < q_size; i = (i + 1) % q_capacity, ++c)
std::cout << q_array[i] << '\n';
std::cout << '\n';
}
int main() {
//1 create an array of students with the names of your favourite classmates
const std::string students[] { "Christian", "Thirdy", "MJ", "Maverick", "Niemo" };
//2 create a queue
queueArr<std::string> stud(5);
//3 loop structure to enqueue all students
for (constauto& s : students)
stud.enqueue(s);
//4 show front
stud.displayFront();
//5 show end
stud.displayBack();
//9 call display all
stud.displayAll();
stud.dequeue();
stud.dequeue();
stud.enqueue("qwer");
stud.enqueue("asd");
stud.dequeue();
stud.displayAll();
}
which displays:
Christian was successfully enqueued.
Thirdy was successfully enqueued.
MJ was successfully enqueued.
Maverick was successfully enqueued.
Niemo was successfully enqueued.
Front: Christian
Back: Niemo
Christian
Thirdy
MJ
Maverick
Niemo
Christian was successfully dequeued.
Thirdy was successfully dequeued.
qwer was successfully enqueued.
asd was successfully enqueued.
MJ was successfully dequeued.
Maverick
Niemo
qwer
asd